Click here to Skip to main content
14,241,414 members
Rate this:
Please Sign up or sign in to 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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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 ?
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100