C# GetString method from Encoding's Unicode class

  • Context: C# 
  • Thread starter Thread starter Silicon Waffle
  • Start date Start date
  • Tags Tags
    Class Method
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 3K views
Silicon Waffle
Messages
160
Reaction score
202
I have a byte array (each character consumes 4 bytes) of size 64 for example,
Now I decode it using
Code:
byte[] bytes={'z','\0','\0','\0','ý','\0','\0','\0','ó','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0',...};
string s=Encoding.Unicode.GetString(bytes);

Amazingly, after the code is executed, s="zýó";

But if my bytes contains string e.g "劉三好" (byte array should be all numbers representing these Chinese characters), then after I execute the above code line I get this string too. How can that be done ?
 
Physics news on Phys.org
Silicon Waffle said:
I have a byte array (each character consumes 4 bytes) of size 64 for example
A byte is an 8-bit unsigned integer (see https://msdn.microsoft.com/en-us/library/system.byte(v=vs.100).aspx). The characters you show in your byte array are one byte each, not 4 bytes. Also, 4 bytes isn't 64 bits, it's 32 bits.
Silicon Waffle said:
,
Now I decode it using
Code:
byte[] bytes={'z','\0','\0','\0','ý','\0','\0','\0','ó','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0',...};
string s=Encoding.Unicode.GetString(bytes);

Amazingly, after the code is executed, s="zýó";

But if my bytes contains string e.g "劉三好" (byte array should be all numbers representing these Chinese characters), then after I execute the above code line I get this string too. How can that be done ?
 
Silicon Waffle said:
I have a byte array (each character consumes 4 bytes) of size 64 for example,
Now I decode it using
Code:
byte[] bytes={'z','\0','\0','\0','ý','\0','\0','\0','ó','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0',...};
string s=Encoding.Unicode.GetString(bytes);

Amazingly, after the code is executed, s="zýó";

But if my bytes contains string e.g "劉三好" (byte array should be all numbers representing these Chinese characters), then after I execute the above code line I get this string too. How can that be done ?
This code do not compile. The correct way to initialize a byte array is
Code:
byte[] bytes={(byte)'z', (byte)'\0',(byte)'\0',(byte)'\0',(byte)'ý',...
string s=Encoding.Unicode.GetString(bytes);
Such a call will return s="z\0ý\0......" and not what you said.
I suppose the bytes comes from a file. If this is UTF32 (Not Unicode / UTF16) you should use
Code:
string s=Encoding.UTF32.GetString(bytes)
Note that this string is tailed by \0 that probably needs cleaning...