Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm working on a project that involves Japanese text encoded in Shift-JIS. I'm trying to decode useful characters from arrays of bytes like so:
Encoding encoding = Encoding.GetEncoding(932);
string test = new string(encoding.GetChars(byteArray));

This works for 99% of the data but I'm getting weird results for just a handful of characters. The character '¨', for example. If I obtain the bytes corresponding to that character like this:
byte[] getBytesTest = encoding.GetBytes("¨");

The results are exactly what I expect. Two bytes, 0x81 and 0x4E, which is what that character is supposed to be in Shift-JIS.
If I try to convert it back to a string, though:
string getCharsTest = new string(encoding.GetChars(getBytesTest));

...I get the default error output "?".

So tl;dr, I'm trying to figure out why this:
byte[] getBytesTest = encoding.GetBytes("¨");
string getCharsTest = new string(encoding.GetChars(getBytesTest));

yields "?" instead of "¨" as a result.

What I have tried:

Passing the results of GetBytes() to GetChars() and manually passing the bytes in like this:
encoding.GetChars(new byte[] {0x81, 0x4E})

both fail to produce the expected result.

Not sure where to go with this.
Posted
Updated 14-May-18 23:45pm

Problem here is encoding which console doesn't understands. If you will write the content in file instead of console, you'll get the text as expected.

File.WriteAllText("file.txt", getCharsTest);
 
Share this answer
 
Your code is ok, when I try this on Win 10 in VS2017, it works:
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(932);
byte[] getBytesTest = encoding.GetBytes("¨");
string getCharsTest = new string(encoding.GetChars(getBytesTest));
Maybe you need to update your Windows ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900