Encoding problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mats-Lennart Hansson

    Encoding problem

    Hi,
    I want to get the ANSI value (0-255) of a character. Assume I create a
    character like this:

    string s = new string((char) 151, 1);

    Now I want to get the ANSI value (151) for this character. How do I do this?

    When using ASCII I, of course get the wrong value:

    byte[] arr = Encoding.ASCII. GetBytes(s); --> [63]

    When using Unicode, I get two bytes where the first one is 151

    byte[] arr = Encoding.Unicod e.GetBytes(s); --> [151, 0]

    Any help is appreciated!

    Thanks,

    Mats-Lennart


  • Jon Skeet [C# MVP]

    #2
    Re: Encoding problem

    Mats-Lennart Hansson <ap_skallen@hot mail.com> wrote:[color=blue]
    > I want to get the ANSI value (0-255) of a character. Assume I create a
    > character like this:
    >
    > string s = new string((char) 151, 1);[/color]

    When you say "the ANSI value", which ANSI encoding are you talking
    about?

    The new string you've given above has the *Unicode* character 151 in
    it.

    I think you need to reconsider your problem with a bit more encoding
    knowledge. See

    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


    I suspect that Encoding.Defaul t is going to be useful to you, but you
    need to understand a few more things first. The above should help you.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Morten Wennevik

      #3
      Re: Encoding problem

      Hi Mats-Lennart,

      ASCII is 7-bit, so 151 will be too high (not sure why it returns 63 and
      not 23 as I would expect, maybe someone can clarify).

      Unicode returns the 16-bit value of the caracter, the most significant
      byte first, which in your case holds the 8-bit character.


      Happy coding!
      Morten Wennevik [C# MVP]

      Comment

      Working...