开发者

Weird behavior when converting byte from text box to byte array to characters?

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 ?


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"


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.


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++.


It works for me:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜