1

I am converting the btye array of "1" "0" "0" "0" to string.

Encoding.UTF8.Getstring(myByteArray,0,4); and the result is "\0\0\0"

The result I was expecting was "1". What is that I am doing wrong?

0

1 Answer 1

3

In UTF8 encoding character 1 maps to byte 49(decimal) which is equal to 31(Hexadecimal) refer UTF8 table.

but you have some other data, Not sure why you expect "1" as the result. If at all you expect "1" as result your byte array should contain single byte value 49.

var result = Encoding.UTF8.GetString(new byte[] { 49 });//result is 1

I believe the data you have is not a UTF-8 encoded bytes, it is something else(may be some other encoding!).

You can't just convert arbitrary bytes to string using UTF8. In order to do so, bytes must have to be encoded with UTF8 in first place. So find what encoding the data is in, then you can use appropriate encoding to reverse it.

1
  • Alright. I have some data which is UTF8 and some are just numbers. I see that those numbers are not UTF8 coded. But I am just looping with foreach so I can't see which one is which. There may be a way to encode both maybe.
    – Zgrkpnr__
    Commented Nov 3, 2014 at 11:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.