Character conversion in .NET

  • Thread starter Thread starter Paul Selormey
  • Start date Start date
P

Paul Selormey

I have an integer, say 0x819A, which represents the Japanese shift-jis
coded star character.
How do I convert this to the System.Char? (that is the unicode encoding).

Best regards,
Paul.
 
Paul,
How do I convert this to the System.Char? (that is the unicode encoding).

Depends on which language you're using. In C# you simply cast the
number to a char. In VB.NET you use the ChrW function.



Mattias
 
Mattias,
Thanks for the response. Both the C# cast and the ChrW
function gave the same result, which is wrong since both are
base on the fact that the number is a unicode number. However,
in my case the number is shift-jis coded (not unicode).

However, your VB.NET pointer gave me the lead. Chr does the
job correctly so I will start from there to look for the C# equivalent,
don't you just love VB.NET? I am off to bring the VB.NET to my
C# level :-)

Best regards,
Paul.
 
Paul Selormey said:
Thanks for the response. Both the C# cast and the ChrW
function gave the same result, which is wrong since both are
base on the fact that the number is a unicode number. However,
in my case the number is shift-jis coded (not unicode).

However, your VB.NET pointer gave me the lead. Chr does the
job correctly so I will start from there to look for the C# equivalent,
don't you just love VB.NET? I am off to bring the VB.NET to my
C# level :-)

There's no equivalent to Chr in C#. (Which is right, IMO - it's not for
the language to do character conversion.)

What you want is the Encoding class in the System.Text namespace. Use
Encoding.GetEncoding ("Shift-JIS").
 
Hello Jon,
Thanks for the response and the support.
There's no equivalent to Chr in C#. (Which is right, IMO - it's not for
the language to do character conversion.)

Frankly, you thought I was going to now read the documents for a C#
version of Chr?
If you spend half a day just playing Encoding/BitConverter etc just hoping
one will make you convert a number to char you will understand why a
language conversion is necessary.
It might not have being a problem if you have a non-PInvoke access to
conversion functions, after all this is what we have being doing before the
..NET.
What you want is the Encoding class in the System.Text namespace. Use
Encoding.GetEncoding ("Shift-JIS").

Hello Jon, I work here in Japan and therefore use that on daily basis :-)
Anyway, since Reflector tells me what is cooking under Chr, I completed my
day successfully :-), but will ever be thankful to the hardworking VB.NET
team for saving the day.

Best regards,
Paul.
 
Paul Selormey said:
Frankly, you thought I was going to now read the documents for a C#
version of Chr?
If you spend half a day just playing Encoding/BitConverter etc just hoping
one will make you convert a number to char you will understand why a
language conversion is necessary.

Do you mean a conversion tool from VB.NET to C#? If so, I somewhat
agree. If you mean that conversion should be part of the language, I
still disagree entirely.
It might not have being a problem if you have a non-PInvoke access to
conversion functions, after all this is what we have being doing
before the .NET.

But Encoding *gives* you the appropriate conversion...
Hello Jon, I work here in Japan and therefore use that on daily basis :-)

Hmmm... I'm somewhat confused as to why you asked the question then.
Your original question just cries out for the answer "use an encoding".
Anyway, since Reflector tells me what is cooking under Chr, I completed my
day successfully :-), but will ever be thankful to the hardworking VB.NET
team for saving the day.

Could you tell me what Chr is doing for you that Encoding doesn't,
aside from converting the integer into a byte array?
 
Back
Top