I need very fast conversion from byte array to string. Byte array is Unicode string.
3 Answers
From byte[]
array to string
var mystring = Encoding.Unicode.GetString(myarray);
From string
to byte[]
var myarray2 = Encoding.Unicode.GetBytes(mystring);
-
-
7@Alex This is C#, not Javascript.
var
isn't a type. It's an abbreviation for "the type of the right-hand expression" (sostring
) (see for example stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp )– xanatosCommented 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.– saboteroCommented Nov 22, 2013 at 10:53 -
I had to use
Encoding.ASCII.GetString
instead ofEncoding.Unicode.GetString
in my case to get it to work.– SNagCommented 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
Try this
System.Text.UnicodeEncoding.Unicode.GetString
-
If I use this then it returns diamonds in the result. Where as this works:
Convert.ToBase64String
- why is that?– variableCommented Jan 5, 2022 at 11:15
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);
:)
-
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