Zgrkpnr__
Zgrkpnr__

Reputation: 319

Converting byte[] to string of UTF8

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?

Upvotes: 1

Views: 2580

Answers (1)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

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.

Upvotes: 3

Related Questions