• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5493
  • Last Modified:

C# Convert Byte Array to String

Hi,
I'm having trouble converting a byte array to a string.  I've looked up and tried several examples on how this is supposed to work, and none of them seem to be working.
I have a program called UltraEdit, which allows me to read the characters of my binary file.

The first line (first 16 bytes) of my file is:
05 59 1D 59 CD 08 05 C1 1C 0C 0C 08 F1 9D 59 B9

When I step through the program, I can see that the file is being read in correctly, as the byte array "binData" holds the correct values.
However, the data is NOT being converted correctly when I use the string conversion methods shown in the code snipet below:

string sData = Encoding.GetEncoding(1252).GetString(binData) is producing:
05 59 1D 59 CD 5C 62 05 C1 1C 5C 66 5C 66 5C 62

string sData = Endocing.ASCII.GetString(binData) is producing:
05 59 1D 59 3F 5C 62 05 3F 1C 5C 66 5C 66 5C 62

Please Help.
string binFile = "C:\\Documents and Settings\\User\\Desktop\\TestBin.bin";
byte[] binData = File.ReadAllBytes(binFile);
// Ways I have tried:
string sData = Encoding.GetEncoding(1252).GetString(binData);
string sData = Endocing.ASCII.GetString(binData);

Open in new window

0
P1ST0LPETE
Asked:
P1ST0LPETE
  • 2
  • 2
  • 2
  • +1
1 Solution
 
AllamzCommented:
Hello P1ST0LPETE,

would you please try the following and let me know.
and check if the string is appearing correctly.
base64String = 
            System.Convert.ToBase64String(binData, 
                                          0,
                                          binData.Length);

Open in new window

0
 
P1ST0LPETEAuthor Commented:

Convert.ToBase64String() isn't working either.  First line (first 16 bytes) is converting to:
42 56 6B 64 57 63 30 49 42 63 45 63 44 41 77 49
Which is quite a ways off, from the correct line of:
05 59 1D 59 CD 08 05 C1 1C 0C 0C 08 F1 9D 59 B9

I have however found out a bit more information.  I decided to create my own binary to string method as shown in code snippet below:
By putting a breakpoint on the "ascii += charVal" line and then watching the variables "charVal" and "hexVal" I could watch what the system was doing and see were the errors were coming into play.
The first error happens on the hex value of "08" which signifies a (backspace) to a computer and is not a printable character.  To place a (backspace) in a string, you would use the escape character "\b".  So when C# see's a "08" in hex, it converts it to a "\b" which is itself 2 characters.  This in turn causes the string to actually hold a "5C" for "\" and a "62" for "b"; which is where the conversion problems are coming into play.  So in short "08" is being converted into "5C 62", and giving me incorrect binary data.

So now I know the problem, I just don't know how to fix it.  Any ideas?
private static string ConvertBytesToString(byte[] binaryData)
{
    string ascii = string.Empty;
    char charVal;
 
    foreach (byte hexVal in binaryData)
    {
        charVal = Convert.ToChar(hexVal);
        ascii += charVal;
    }
 
    return ascii.ToString();
}

Open in new window

0
 
AllamzCommented:
would you please attach the file and let me know the expected output
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Anurag ThakurCommented:
i think you are doing it correctly
first converting the hex values to char and then converting it to string

the following links shows the same way but in a little different way - you might want to go through it
http://bytes.com/groups/net-c/274209-byte-array-string
0
 
P1ST0LPETEAuthor Commented:
After further examination, I don't think it is possible to percisely convert a binary array to a string.  However I was able so solve my root issue, which was sending a combo of xml and binary data over a network connection.  Up until I ran into this problem, I had only been sending xml strings, and so I was in the closed mind thinking that I had to send my message as a string.  So to solve the issue, I am still using the following line of code to convert my binary data to a string:

string sData = Encoding.GetEncoding(1252).GetString(binData);

Then I add the string containing the binary data to the xml string I am wanting to send:

string xmlMessage = "<message><data>" + sData + "</data></message>";

Then before I send the message, I convert the entire string back to a byte array, and then send the byte array instead of a string.  When I convert the whole thing back to a byte array, it fixes the issues I was having that I mentioned in my two previous posts:

byte[] msgBytes = Encoding.GetEncoding(1252).GetBytes(xmlMessage);
0
 
srivmanish007Commented:

 byte[] dTestBytes ={05,59,18,59,45,78,25};
 
   
 
      string binFile = Server.MapPath("TextFile.bin");
      byte[] binData = File.ReadAllBytes(binFile);
      string str = ConvertBytesToString(binData);
 
 
      string base64String =
           System.Convert.ToBase64String(binData,
                                         0,
                                         binData.Length);
 
      string sData = Encoding.GetEncoding(1252).GetString(binData);
 
 
        //System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
        //iInt = Convert.ToInt16(System.Text.Encoding.ASCII.GetString(dBytes)); 
      Response.Write(str);
      Response.Write("<br>");
      Response.Write(base64String);
      Response.Write(sData);

Open in new window

0
 
srivmanish007Commented:
the above code has been pasted by mistake.
Pls delete it.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now