C# Byte[] Byte array to Unicode string

56,807

Solution 1

From byte[] array to string

 var mystring = Encoding.Unicode.GetString(myarray);

From string to byte[]

 var myarray2 = Encoding.Unicode.GetBytes(mystring);

Solution 2

Try this

System.Text.UnicodeEncoding.Unicode.GetString

Solution 3

UTF8 (I think you mean "UTF8" instead of "Unicode"). Because, U'll get just Chinese Symbols. ;)

Maybe it helps to change...

var mystring = Encoding.Unicode.GetString(myarray);

...to...

var mystring = Encoding.UTF8.GetString(myarray);

:)

Share:
56,807
Hooch
Author by

Hooch

Updated on July 09, 2022

Comments

  • Hooch
    Hooch almost 2 years

    I need very fast conversion from byte array to string. Byte array is Unicode string.


    enter image description here

  • Alex
    Alex about 11 years
    @xanatos why do you use var type?
  • xanatos
    xanatos about 11 years
    @Alex This is C#, not Javascript. var isn't a type. It's an abbreviation for "the type of the right-hand expression" (so string) (see for example stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sh‌​arp )
  • Alex
    Alex about 11 years
    @xanatos Sorry, didn't know that. Thanks.
  • sabotero
    sabotero over 10 years
    @xanatos, @alex technically var in JavaScript is not a type like you're saying. var in JavaScript is a keyword meaning I'm wanting to declare a variable here which type is inferred later.
  • SNag
    SNag over 8 years
    I had to use Encoding.ASCII.GetString instead of Encoding.Unicode.GetString in my case to get it to work.
  • Dmitry Gusarov
    Dmitry Gusarov almost 6 years
    @SNag, avoid usages of ASCII, this is only for the last resort, when you are absolutely known what are you doing and why. 1) By default first thing to play with must be UTF8 encoding 2) If for some reason you are dealing with an old non-unicode data you can try Encoding.Default 3) Only as a fallback if you explicitly going to stick with 0-127 byte values range and Latin chars only you can try ASCII (7bit that fits into 8bit) In the author's case it is clear that data is UTF16 encoded and this is why Encoding.Unicode is correct here. This is mostly used by inmemory blobs.
  • Dmitry Gusarov
    Dmitry Gusarov almost 6 years
    No, his data is clearly UTF16 (Encoding.Unicode) encoded.A\0n\0o\0n\0i\0... Both UTF8 and Unicode (UTF16) perfectly works with Chinese and any other symbol in Universal Coded Character Set (UCS). UTF8 is the default in most cases indeed, for data serialization, persisting and transfer. UTF16 is mostly in use by OS and frameworks for inmemory data to reduce encoding costs.
  • variable
    variable over 2 years
    If I use this then it returns diamonds in the result. Where as this works: Convert.ToBase64String - why is that?
  • variable
    variable over 2 years
    If I use this then it returns diamonds in the result. Where as this works: Convert.ToBase64String - why is that?
  • xanatos
    xanatos over 2 years
    @variable It all depends on how the string was encoded in the byte array. There are many ways to encode it, and for each way to encode there is a single way to decode.