Click here to Skip to main content
15,851,915 members
Home / Discussions / C#
   

C#

 
QuestionConverting Back from Decimal to Byte Pin
computerpublic12-Feb-14 11:03
computerpublic12-Feb-14 11:03 
AnswerRe: Converting Back from Decimal to Byte Pin
Richard Deeming12-Feb-14 12:07
mveRichard Deeming12-Feb-14 12:07 
GeneralRe: Converting Back from Decimal to Byte Pin
computerpublic12-Feb-14 12:29
computerpublic12-Feb-14 12:29 
GeneralRe: Converting Back from Decimal to Byte Pin
Richard Deeming13-Feb-14 1:37
mveRichard Deeming13-Feb-14 1:37 
GeneralRe: Converting Back from Decimal to Byte Pin
computerpublic13-Feb-14 10:15
computerpublic13-Feb-14 10:15 
GeneralRe: Converting Back from Decimal to Byte Pin
Richard Deeming13-Feb-14 11:35
mveRichard Deeming13-Feb-14 11:35 
GeneralRe: Converting Back from Decimal to Byte Pin
computerpublic13-Feb-14 11:48
computerpublic13-Feb-14 11:48 
GeneralRe: Converting Back from Decimal to Byte Pin
computerpublic16-Feb-14 7:00
computerpublic16-Feb-14 7:00 
C#
<pre>/*
I AM NOW TRYING TO CONVERT BYTE TO STRING AND ALSO CONVERTING BACK FROM STRING TO BYTE. 
I ALSO WANT TO SEE THE OUTPUT.
I AM NOT UNDERSTING WHY I AM GETTING AN ERROR.
THE OUTPUT INFORMATION IS BELOW::

Total Bytes = 8228730 bytes

Unhandled Exception: System.InvalidOperationException: Data has changed at index
 310
   at Applica.Program.Main(String[] args) in C:\Documents and Settings\shampro\D
esktop\Program.cs:line 41
Press any key to continue . . .
*/

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Applica
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo da = new DirectoryInfo("C:\\Folder");
            FileInfo[] Arr = da.GetFiles();
            if (Arr.Length == 0)
            {
                throw new InvalidOperationException("No files found.");
            }
            // No need to loop through the array just to get the last item:
            FileInfo ap = Arr[Arr.Length - 1];
            long Totbyte = ap.Length;
            string filePath = ap.FullName;
            Console.WriteLine("Total Bytes = {0} bytes", Totbyte);
            // GetTempFileName *creates* the file, so it always exists:
            string temPath = Path.GetTempFileName();
            byte[] data = File.ReadAllBytes(filePath);
            File.WriteAllBytes(temPath, data);
            // Convert the bytes to string:
            string arry = ASCIIEncoding.ASCII.GetString(data);
            Console.WriteLine(arry);
            // Convert the string back to bytes:
            byte[] data2 = Encoding.ASCII.GetBytes(arry);
            foreach (byte element in data2)
            {
                Console.WriteLine("{0}={1}",element ,(char)element);
            }
            // Just to prove they're the same:
            if (data2.Length != data.Length)
            {
                throw new InvalidOperationException("Wrong length!");
            }
            for (int index = 0; index < data.Length; index++)
            {
                if (data[index] != data2[index])
                {
                    throw new InvalidOperationException("Data has changed at index " + index);
                }
            }
            // Write the bytes back to the file:
            string filePath2 = Path.Combine("C:\\check", Path.GetFileName(filePath));
            File.WriteAllBytes(filePath2, data2);
            // To prove they're still the same:
            data = File.ReadAllBytes(temPath);
            data2 = File.ReadAllBytes(filePath);
            if (data2.Length != data.Length)
            {
                throw new InvalidOperationException("Wrong length!");
            }
            for (int index = 0; index < data.Length; index++)
            {
                if (data[index] != data2[index])
                {
                    throw new InvalidOperationException("Data has changed at index " + index);
                }
            }

        }
    }
}



GeneralRe: Converting Back from Decimal to Byte Pin
computerpublic16-Feb-14 7:03
computerpublic16-Feb-14 7:03 
GeneralRe: Converting Back from Decimal to Byte Pin
Richard Deeming17-Feb-14 2:46
mveRichard Deeming17-Feb-14 2:46 
GeneralRe: Converting Back from Decimal to Byte Pin
computerpublic18-Feb-14 23:51
computerpublic18-Feb-14 23:51 
GeneralRe: Converting Back from Decimal to Byte Pin
computerpublic19-Feb-14 0:03
computerpublic19-Feb-14 0:03 
GeneralRe: Converting Back from Decimal to Byte Pin
Richard Deeming19-Feb-14 2:59
mveRichard Deeming19-Feb-14 2:59 
GeneralRe: Converting Back from Bit to Byte Pin
computerpublic13-Mar-14 9:16
computerpublic13-Mar-14 9:16 
AnswerRe: Converting Back from Decimal to Byte Pin
Eddy Vluggen12-Feb-14 12:21
professionalEddy Vluggen12-Feb-14 12:21 
QuestionRe: Converting Back from Decimal to Byte Pin
Richard MacCutchan12-Feb-14 22:38
mveRichard MacCutchan12-Feb-14 22:38 
Questiontreeview and database Pin
danmor49812-Feb-14 7:45
danmor49812-Feb-14 7:45 
QuestionRe: treeview and database Pin
Ravi Bhavnani12-Feb-14 8:28
professionalRavi Bhavnani12-Feb-14 8:28 
AnswerRe: treeview and database Pin
danmor49812-Feb-14 8:52
danmor49812-Feb-14 8:52 
QuestionRe: treeview and database Pin
Richard MacCutchan12-Feb-14 22:27
mveRichard MacCutchan12-Feb-14 22:27 
AnswerRe: treeview and database Pin
danmor49813-Feb-14 9:58
danmor49813-Feb-14 9:58 
GeneralRe: treeview and database Pin
Richard MacCutchan13-Feb-14 22:45
mveRichard MacCutchan13-Feb-14 22:45 
QuestionASP.NET Project Files Pin
Sandhya Bandar12-Feb-14 3:29
Sandhya Bandar12-Feb-14 3:29 
AnswerRe: ASP.NET Project Files Pin
Richard MacCutchan12-Feb-14 5:42
mveRichard MacCutchan12-Feb-14 5:42 
QuestionThe file version maintain utility Pin
sahani from india12-Feb-14 2:56
sahani from india12-Feb-14 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.