Byte array to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pkumar

    Byte array to string

    How to convert this byte array to string
    byte[] b=new byte[100];

    Is there any function or I need read one by one and build the string

    thanks


  • Mythran

    #2
    Re: Byte array to string


    "pkumar" <pkumar@discuss ions.microsoft. com.> wrote in message
    news:AE8C1B69-5F0B-427B-B6E7-629650ACF597@mi crosoft.com...[color=blue]
    > How to convert this byte array to string
    > byte[] b=new byte[100];
    >
    > Is there any function or I need read one by one and build the string
    >
    > thanks
    >
    >[/color]

    byte[] b = new byte[100];
    string s = System.Text.ASC IIEncoding.ASCI I.GetString(b);


    HTH,
    Mythran

    Comment

    • Franco, Gustavo

      #3
      Re: Byte array to string

      Be carrefull with System.Text.ASC IIEncoding.ASCI I.GetString(b);

      If your codepage is different than English then the conversion could have
      problems (not always).

      I had this problem where I had a program running on Korea where the codepage
      where different and then the range of Extended ASCII characters supported
      are from 0 to 239, instead 0 to 255.

      I experimented this problem with some Windows installed on different
      languages.

      If you want get a string back you can use something like this.

      System.Text.Enc oding.GetEncodi ng(1251).GetStr ing(b); (1251 is English)

      You can see how the conversion fail if you go to Control Panel, Regional
      Settings, Advanced and on "Language for non-Unicode programs" put Japanese.

      The strange thing is: clearly it says: "Language for non-Unicode programs",
      I know .net is full Unicode.

      Now, why using System.Text.ASC IIEncoding.ASCI I fail?, I don't know that...

      Really doesn't fail, but if you Encode a string with extended characters
      into bytes and Decoded again into string, you will get different results. I
      guess .net map the extended character to the near one supported for the
      codepage.

      Gustavo.


      "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
      news:OAsWuAulFH A.3380@TK2MSFTN GP12.phx.gbl...[color=blue]
      >
      > "pkumar" <pkumar@discuss ions.microsoft. com.> wrote in message
      > news:AE8C1B69-5F0B-427B-B6E7-629650ACF597@mi crosoft.com...[color=green]
      >> How to convert this byte array to string
      >> byte[] b=new byte[100];
      >>
      >> Is there any function or I need read one by one and build the string
      >>
      >> thanks
      >>
      >>[/color]
      >
      > byte[] b = new byte[100];
      > string s = System.Text.ASC IIEncoding.ASCI I.GetString(b);
      >
      >
      > HTH,
      > Mythran
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Byte array to string

        Franco, Gustavo <gustavo_fran co[REMOVEIT]@hotmail.com> wrote:[color=blue]
        > Be carrefull with System.Text.ASC IIEncoding.ASCI I.GetString(b);
        >
        > If your codepage is different than English then the conversion could have
        > problems (not always).
        >
        > I had this problem where I had a program running on Korea where the codepage
        > where different and then the range of Extended ASCII characters supported
        > are from 0 to 239, instead 0 to 255.[/color]

        Careful here - there's no such encoding as "Extended ASCII". There are
        various character encodings which *are* extensions to ASCII, but no one
        "extended ASCII".
        [color=blue]
        > I experimented this problem with some Windows installed on different
        > languages.
        >
        > If you want get a string back you can use something like this.
        >
        > System.Text.Enc oding.GetEncodi ng(1251).GetStr ing(b); (1251 is English)[/color]

        1251 is just *one* code page...
        [color=blue]
        > You can see how the conversion fail if you go to Control Panel, Regional
        > Settings, Advanced and on "Language for non-Unicode programs" put Japanese.
        >
        > The strange thing is: clearly it says: "Language for non-Unicode programs",
        > I know .net is full Unicode.[/color]

        Yes, but the point is that a byte array isn't an array of characters.
        You need to know what encoding the bytes represent characters in, in
        order to get from them to Unicode.
        [color=blue]
        > Now, why using System.Text.ASC IIEncoding.ASCI I fail?, I don't know that...[/color]

        Because any byte values greater than 127 aren't ASCII.
        [color=blue]
        > Really doesn't fail, but if you Encode a string with extended characters
        > into bytes and Decoded again into string, you will get different results. I
        > guess .net map the extended character to the near one supported for the
        > codepage.[/color]

        You're asking the encoding to deal with bytes which it can't handle -
        that's why things go wrong.

        See http://www.pobox.com/~skeet/csharp/unicode.html for more
        information.

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

        Working...