35

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


enter image description here

1
  • 1
    "fastest" would be in-place, but that is impossible under .net (or so I think)... So second-best is "with no conversion, a simple copy array to array (where the second array is the "internal" array-of-chars of the string)".
    – xanatos
    Commented Feb 20, 2011 at 10:20

3 Answers 3

83

From byte[] array to string

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

From string to byte[]

 var myarray2 = Encoding.Unicode.GetBytes(mystring);
7
  • @xanatos why do you use var type?
    – Alex
    Commented Apr 9, 2013 at 11:30
  • 7
    @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-sharp )
    – xanatos
    Commented Apr 9, 2013 at 13:21
  • 3
    @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.
    – sabotero
    Commented Nov 22, 2013 at 10:53
  • I had to use Encoding.ASCII.GetString instead of Encoding.Unicode.GetString in my case to get it to work.
    – SNag
    Commented Jan 7, 2016 at 17:43
  • 1
    @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. Commented May 16, 2018 at 10:19
9

Try this

System.Text.UnicodeEncoding.Unicode.GetString
1
  • If I use this then it returns diamonds in the result. Where as this works: Convert.ToBase64String - why is that?
    – variable
    Commented Jan 5, 2022 at 11:15
1

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);

:)

1
  • 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. Commented May 16, 2018 at 10:27

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