3

I have a textbox that I use to convert things like:

74 00 65 00 73 00 74 00

Back into a string, the above says "test" but for some reason when I click the convert button it will display only the first letter "t" 74 00 and other byte arrays work just as expected, the entire text is converted.

Here is the 2 codes I have tried which produce the same behavior of not properly converting the entire byte array back to word:

byte[] bArray = ByteStrToByteArray(iSequence.Text);
ASCIIEncoding enc = new ASCIIEncoding();
string word = enc.GetString(bArray);
iResult.Text = word + Environment.NewLine;

which uses the function:

private byte[] ByteStrToByteArray(string byteString)
{
    byteString = byteString.Replace(" ", string.Empty);
    byte[] buffer = new byte[byteString.Length / 2];
    for (int i = 0; i < byteString.Length; i += 2)
        buffer[i / 2] = (byte)Convert.ToByte(byteString.Substring(i, 2), 16);
    return buffer;
}

another way I was using is:

string str = iSequence.Text.Replace(" ", "");
byte[] bArray = Enumerable.Range(0, str.Length)
                            .Where(x => x % 2 == 0)
                            .Select(x => Convert.ToByte(str.Substring(x, 2), 16))
                            .ToArray();
ASCIIEncoding enc = new ASCIIEncoding();
string word = enc.GetString(bArray);
iResult.Text = word + Environment.NewLine;

Tried checking for the lengths to see if it was iterating thru and it was ...

Don't really know how to debug why this is happenning to the above byte array but all the other byte arrays seemed to be working just fine only this one is outputing only the first letter of it.

Have I done something wrong that could produce this behavior some how ? What could I try in order to find out what is wrong ?

4 Answers 4

9

If you have the byte sequence

var bytes = new byte[] { 0x74, 0x00, 0x65, 0x00, 0x73, 0x00, 0x74, 0x00 };

and you decode it to a string using ASCII encoding (Encoding.ASCII), then you get

var result = Encoding.ASCII.GetString(bytes);
// result == "\x74\x00\x65\x00\x73\x00\x74\x00" == "t\0e\0s\0t\0"

Notice the Null \0 characters? When you display such a string in a textbox, only the part of the string until the first Null character is displayed.

Since you say the result should read "test", the input is actually not encoded in ASCII but in UTF-16LE (Encoding.Unicode).

var result = Encoding.Unicode.GetString(bytes);
// result == "\u0074\u0065\u0073\u0074" == "test"
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot that explains what I was trying to do and why it was not working.
1

your converting a unicode string to ascii , your not specifying the codepage on your machine to convert from. System.Text.Encoding.GetEncoding("codepage").GetString() if my memory serves me correct. Also to note, any control in .NET is unicode ... Soooooo.... what your trying to stick in the text box (if the conversion isent correct) could be an end of line character .. or eof, or any kind of control character. all depends on your codepage.

Comments

0

I tried debugging the first program using breakpoints in VS2010. I found out that the line

string word = enc.GetString(bArray);

output word as "t\0e\0s\0t".

The last line

iResult.Text = word + Environment.NewLine;

gives iResult.Text as simply "t".

So I was thinking since \0 is not a valid escape sequence, the compiler ignored everything after it. Could be wrong though but try removing all occurrences of 00 in the input string.

I'm not really into C#. I'm only suggesting this because it looks like C++.

1 Comment

\0 is a valid escape sequence in C#. It's not the compiler that's ignoring something at compile-time, but the TextBox when displaying the string at runtime. Removing the \0 characters only fixes the symptoms, not the cause (which is a charset problem). Sorry, but your answer is almost completely wrong.
0

It works for me:

string outputText = "t\0e\0s\0t";
outputText = outputText.Replace("\0", " ");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.