copye char[] to byte[]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John A Grandy

    copye char[] to byte[]

    How to copy an char[] to a byte[] , where the char[] holds unicode
    characters ?


  • Spectre

    #2
    RE: copye char[] to byte[]

    public static byte[] CharToByte(char[] c)
    {
    byte[] b = new byte[c.Length];
    for(int i = 0; i < c.Length; i++)
    {
    b[i] = (byte) c[i];
    }
    return b;
    }

    Simple as that :)

    "John A Grandy" wrote:
    [color=blue]
    > How to copy an char[] to a byte[] , where the char[] holds unicode
    > characters ?
    >
    >
    >[/color]

    Comment

    • Lau Lei Cheong

      #3
      Re: copye char[] to byte[]

      Use System.Text.Enc oding.Unicode.g etBytes().

      "John A Grandy" <johnagrandy-at-yahoo-dot-com> ¼¶¼g©ó¶l¥ó·s»D: %23ZR9LcFVGHA.1 96@TK2MSFTNGP10 .phx.gbl...[color=blue]
      > How to copy an char[] to a byte[] , where the char[] holds unicode
      > characters ?
      >[/color]


      Comment

      • Spectre

        #4
        Re: copye char[] to byte[]

        I don't beleive you can pass a char[] to that method.

        "Lau Lei Cheong" wrote:
        [color=blue]
        > Use System.Text.Enc oding.Unicode.g etBytes().
        >
        > "John A Grandy" <johnagrandy-at-yahoo-dot-com> ¼¶¼g©ó¶l ¥Ã³Â·s»D:%23ZR 9LcFVGHA.196@TK 2MSFTNGP10.phx. gbl...[color=green]
        > > How to copy an char[] to a byte[] , where the char[] holds unicode
        > > characters ?
        > >[/color]
        >
        >
        >[/color]

        Comment

        • Lau Lei Cheong

          #5
          Re: copye char[] to byte[]

          Copied from MSDN:
          ms-help://MS.MSDNQTR.2005 JUL.1033/cpref/html/frlrfSystemText EncodingClassGe tBytesTopic.htm
          ms-help://MS.MSDNQTR.2005 JUL.1033/cpref/html/frlrfsystemtext encodingclassge tbytestopic3.ht m
          Encodes a range of characters from a character array into a byte array.

          Supported by the .NET Compact Framework.

          [Visual Basic] Overloads Public Overridable Function GetBytes(Char() ,
          Integer, Integer) As Byte()
          [C#] public virtual byte[] GetBytes(char[], int, int);
          [C++] public: virtual unsigned char GetBytes(__wcha r_t __gc[], int, int)
          __gc[];
          [JScript] public function GetBytes(Char[], int, int) : Byte[];
          -- End of Copy --

          If there is error, I suspect you really means
          System.Text.Enc oding.UTF8.GetB ytes().

          "Spectre" <Spectre@discus sions.microsoft .com> ¼¶¼g©ó¶l¥ó·s»D: 3B19F3B7-1C76-42E4-B035-04F4E8DCCE70@mi crosoft.com...[color=blue]
          >I don't beleive you can pass a char[] to that method.
          >
          > "Lau Lei Cheong" wrote:
          >[color=green]
          >> Use System.Text.Enc oding.Unicode.g etBytes().
          >>
          >> "John A Grandy" <johnagrandy-at-yahoo-dot-com> ???gco?l¢Do¡Ps? D:%23ZR9LcFVGHA .196@TK2MSFTNGP 10.phx.gbl...
          >>[color=darkred]
          >> > How to copy an char[] to a byte[] , where the char[] holds unicode
          >> > characters ?
          >> >[/color]
          >>
          >>
          >>[/color][/color]


          Comment

          • Helge Jensen

            #6
            Re: copye char[] to byte[]



            Spectre wrote:[color=blue]
            > public static byte[] CharToByte(char[] c)
            > {
            > byte[] b = new byte[c.Length];
            > for(int i = 0; i < c.Length; i++)
            > {
            > b[i] = (byte) c[i];
            > }
            > return b;
            > }
            >
            > Simple as that :)[/color]

            Perhaps OP should have stated that he wated some usable bytes, not just
            junk :)

            The above code will not convert an array of unicode-char to a
            byte-encoding of those same chars in any kind of encoding, unless the
            input-character-set is roughly ASCII.

            You can select an encoding from System.Text.Enc oding and invoke
            GetBytes() on that. UTF8 would be a prime candidate.

            --
            Helge Jensen
            mailto:helge.je nsen@slog.dk
            sip:helge.jense n@slog.dk
            -=> Sebastian cover-music: http://ungdomshus.nu <=-

            Comment

            • Lau Lei Cheong

              #7
              Re: copye char[] to byte[]

              Yes. Especially considering what people refered to as Unicode means "the
              real Unicode", "UTF-7"(this is less likely), "UTF-8" and "UCS-2", with
              different size among them, I think it's better to let the Framework routines
              handle them for you.

              "Helge Jensen" <helge.jensen@s log.dk> ¼¶¼g©ó¶l¥ó·s»D: OZQX8dIVGHA.227 6@tk2msftngp13. phx.gbl...[color=blue]
              > The above code will not convert an array of unicode-char to a
              > byte-encoding of those same chars in any kind of encoding, unless the
              > input-character-set is roughly ASCII.
              >
              > You can select an encoding from System.Text.Enc oding and invoke
              > GetBytes() on that. UTF8 would be a prime candidate.[/color]


              Comment

              Working...