How to convert a Byte array to a char array?

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

    How to convert a Byte array to a char array?

    How do I convert a Byte array (unsigned char managed) to a
    char array(unmanaged ) with wide character taken into
    account?
  • Nishant S

    #2
    Re: How to convert a Byte array to a char array?

    Byte barray[] = new Byte[512];
    __wchar_t* arr = new __wchar_t[barray->Length];
    for(int i=0; i < barray->Length; i++)
    arr[i] = barray[i];
    delete arr;

    --
    Regards,
    Nish [VC++ MVP]



    "Kueishiong Tu" <kst001@seed.ne t.tw> wrote in message
    news:2da101c374 a4$b85591a0$a10 1280a@phx.gbl.. .[color=blue]
    > How do I convert a Byte array (unsigned char managed) to a
    > char array(unmanaged ) with wide character taken into
    > account?[/color]


    Comment

    • Kueishiong Tu

      #3
      Re: How to convert a Byte array to a char array?

      [color=blue]
      >-----Original Message-----
      >Byte barray[] = new Byte[512];
      >__wchar_t* arr = new __wchar_t[barray->Length];
      >for(int i=0; i < barray->Length; i++)
      >arr[i] = barray[i];
      >delete arr;[/color]

      How do I further convert to a array declared as char
      carray[]? The access of a char array through its pointer
      has taken the wide character into account. i.e., if

      char *cpp;
      int n;
      cpp = carray;

      (cpp+n) will always point to a valid wide charater (n any
      int).

      Comment

      • Nishant S

        #4
        Re: How to convert a Byte array to a char array?

        Byte barray[] = new Byte[512];

        //char here is 16 bits
        char smallarray[512];
        for(int i=0; i<barray->Length; i++)
        smallarray[i] = barray[i];

        //Char here is 32 bits
        Char carray[] = Encoding::ASCII->GetChars(barra y);


        --
        Regards,
        Nish [VC++ MVP]



        "Kueishiong Tu" <kst001@seed.ne t.tw> wrote in message
        news:2f9801c374 e6$35387b60$a10 1280a@phx.gbl.. .[color=blue]
        >[color=green]
        > >-----Original Message-----
        > >Byte barray[] = new Byte[512];
        > >__wchar_t* arr = new __wchar_t[barray->Length];
        > >for(int i=0; i < barray->Length; i++)
        > >arr[i] = barray[i];
        > >delete arr;[/color]
        >
        > How do I further convert to a array declared as char
        > carray[]? The access of a char array through its pointer
        > has taken the wide character into account. i.e., if
        >
        > char *cpp;
        > int n;
        > cpp = carray;
        >
        > (cpp+n) will always point to a valid wide charater (n any
        > int).
        >[/color]



        Comment

        • Kueishiong Tu

          #5
          Re: How to convert a Byte array to a char array?

          [color=blue]
          >-----Original Message-----
          >Byte barray[] = new Byte[512];
          >
          >//char here is 16 bits
          >char smallarray[512];
          >for(int i=0; i<barray->Length; i++)
          > smallarray[i] = barray[i];
          >
          >//Char here is 32 bits
          >Char carray[] = Encoding::ASCII->GetChars(barra y);
          >[/color]

          The byte array contains a mixture of
          raw data of Ascii (8-bit) and Chinese characters (16-bit).
          What I need is to convert the byte array into a char array
          so that if I assigned a char pointer to the beginning of
          the char array and I increment the char pointer, it will
          point to a valid wide character which may be 8-bit or 16-
          bit.

          Byte barray[512];
          char carray[512];
          char *cpp;
          //convert barray to caaray
          cpp = carray;
          printf("%c\n", *cpp); // valid 8-bit or 16-bit character
          *(cpp++);
          printf("%c\n", *cpp); // valid 8-bit or 16-bit character
          *(cpp++);
          printf("%c\n", *cpp); // valid 8-bit or 16-bit character
          *(cpp++);
          printf("%c\n", *cpp); // valid 8-bit or 16-bit character
          *(cpp++);
          printf("%c\n", *cpp); // valid 8-bit or 16-bit character
          *(cpp++);
          printf("%c\n", *cpp); // valid 8-bit or 16-bit character
          *(cpp++);

          and so on ....




          Comment

          • Jochen Kalmbach

            #6
            Re: How to convert a Byte array to a char array?

            Kueishiong Tu wrote:
            [color=blue]
            > How do I convert a Byte array (unsigned char managed) to a
            > char array(unmanaged ) with wide character taken into
            > account?[/color]

            See: StringToHGlobal Ansi (System::Runtim e::InteropServi ces::Marshal)
            Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

            us/cpref/html/frlrfsystemrunt imeinteropservi cesmarshalclass stringtohglobal a
            nsitopic.asp

            But conversion to ANSI is bad... because String is UNICODE... so if it
            contains some unicode characters this might be lost.

            Better convert to unicde: StringToHGlobal Uni
            or use PtrToStringChar s (which is faster; if you do not need to modify the
            string)

            See also:
            HOW TO: Convert from System::String* to Char* in Visual C++ .NET
            Microsoft Support is here to help you with Microsoft products. Find how-to articles, videos, and training for Microsoft Copilot, Microsoft 365, Windows 11, Surface, and more.


            --
            Greetings
            Jochen

            Do you need a memory-leak finder ?

            Comment

            • Nishant S

              #7
              Re: How to convert a Byte array to a char array?

              There was en error in my prev post.

              char is 8 bits (a byte) and Char is 16 bits (2 bytes)

              --
              Regards,
              Nish [VC++ MVP]



              "Nishant S" <nish@nospam.as ianetindia.com> wrote in message
              news:upOT4hOdDH A.3248@tk2msftn gp13.phx.gbl...[color=blue]
              > Byte barray[] = new Byte[512];
              >
              > //char here is 16 bits
              > char smallarray[512];
              > for(int i=0; i<barray->Length; i++)
              > smallarray[i] = barray[i];
              >
              > //Char here is 32 bits
              > Char carray[] = Encoding::ASCII->GetChars(barra y);
              >
              >
              > --
              > Regards,
              > Nish [VC++ MVP]
              >
              >
              >
              > "Kueishiong Tu" <kst001@seed.ne t.tw> wrote in message
              > news:2f9801c374 e6$35387b60$a10 1280a@phx.gbl.. .[color=green]
              > >[color=darkred]
              > > >-----Original Message-----
              > > >Byte barray[] = new Byte[512];
              > > >__wchar_t* arr = new __wchar_t[barray->Length];
              > > >for(int i=0; i < barray->Length; i++)
              > > >arr[i] = barray[i];
              > > >delete arr;[/color]
              > >
              > > How do I further convert to a array declared as char
              > > carray[]? The access of a char array through its pointer
              > > has taken the wide character into account. i.e., if
              > >
              > > char *cpp;
              > > int n;
              > > cpp = carray;
              > >
              > > (cpp+n) will always point to a valid wide charater (n any
              > > int).
              > >[/color]
              >
              >
              >[/color]


              Comment

              • Nishant S

                #8
                Re: How to convert a Byte array to a char array?

                If you have Chinese characters then you cannot use a char array because a
                char is only 8 bits. You need to use a Char (System::Char) or a __whcar_t
                then.

                BTW considering that a Byte is 8 bits I wonder how you can store chinese
                characters in a Byte array unless you have a random kinda layout where
                depending on whether the character you want to store is unicode or not, you
                allot either one byte or two bytes for storing a character. Not a very
                organized approach in my opinion.

                --
                Regards,
                Nish [VC++ MVP]



                "Kueishiong Tu" <kst001@seed.ne t.tw> wrote in message
                news:3ba501c374 ff$eb879680$a60 1280a@phx.gbl.. .[color=blue]
                >[color=green]
                > >-----Original Message-----
                > >Byte barray[] = new Byte[512];
                > >
                > >//char here is 16 bits
                > >char smallarray[512];
                > >for(int i=0; i<barray->Length; i++)
                > > smallarray[i] = barray[i];
                > >
                > >//Char here is 32 bits
                > >Char carray[] = Encoding::ASCII->GetChars(barra y);
                > >[/color]
                >
                > The byte array contains a mixture of
                > raw data of Ascii (8-bit) and Chinese characters (16-bit).
                > What I need is to convert the byte array into a char array
                > so that if I assigned a char pointer to the beginning of
                > the char array and I increment the char pointer, it will
                > point to a valid wide character which may be 8-bit or 16-
                > bit.
                >
                > Byte barray[512];
                > char carray[512];
                > char *cpp;
                > //convert barray to caaray
                > cpp = carray;
                > printf("%c\n", *cpp); // valid 8-bit or 16-bit character
                > *(cpp++);
                > printf("%c\n", *cpp); // valid 8-bit or 16-bit character
                > *(cpp++);
                > printf("%c\n", *cpp); // valid 8-bit or 16-bit character
                > *(cpp++);
                > printf("%c\n", *cpp); // valid 8-bit or 16-bit character
                > *(cpp++);
                > printf("%c\n", *cpp); // valid 8-bit or 16-bit character
                > *(cpp++);
                > printf("%c\n", *cpp); // valid 8-bit or 16-bit character
                > *(cpp++);
                >
                > and so on ....
                >
                >
                >
                >[/color]


                Comment

                • Kueishiong Tu

                  #9
                  Re: How to convert a Byte array to a char array?

                  [color=blue]
                  >-----Original Message-----
                  >If you have Chinese characters then you cannot use a[/color]
                  char array because a[color=blue]
                  >char is only 8 bits. You need to use a Char[/color]
                  (System::Char) or a __whcar_t[color=blue]
                  >then.
                  >
                  >BTW considering that a Byte is 8 bits I wonder how you[/color]
                  can store chinese[color=blue]
                  >characters in a Byte array unless you have a random[/color]
                  kinda layout where[color=blue]
                  >depending on whether the character you want to store is[/color]
                  unicode or not, you[color=blue]
                  >allot either one byte or two bytes for storing a[/color]
                  character. Not a very[color=blue]
                  >organized approach in my opinion.
                  >
                  >--
                  >Regards,
                  >Nish [VC++ MVP]
                  >[/color]

                  The Byte array I got is from a Webclient::Uplo adData call
                  which put the data returned in a Byte array (which may
                  contain both one byte Ascii and two-byte Chinese Big-5
                  code). VC++ can handle char array which may store both
                  one-byte ascii and two=byte Big-5 characters. I have
                  several functions which work fine with this situation.
                  The problem is how to convert the Byte array to a char
                  array so I have use a char porinter to decode the message
                  one character at a time.

                  Comment

                  • Nishant S

                    #10
                    Re: How to convert a Byte array to a char array?

                    Hmmm

                    Okay I think one of the following is what you want :-

                    //this is to obtain a single byte char array
                    //might lose chinese characters

                    char smallarray[512];
                    for(int i=0; i<barray->Length; i++)
                    smallarray[i] = barray[i];

                    //this gets a wide char array
                    String* tmp = new String(Encoding ::ASCII->GetChars(barra y));
                    __wchar_t* array = (__wchar_t*)Mar shal::StringToH GlobalUni(
                    tmp).ToPointer( );
                    //...
                    // You can now use array (wide char array)
                    //...
                    Marshal::FreeCo TaskMem(array);

                    I think the second one is what you want. Please try it out and see if it
                    works for you.

                    --
                    Regards,
                    Nish [VC++ MVP]



                    "Kueishiong Tu" <kst001@seed.ne t.tw> wrote in message
                    news:30ce01c375 10$c0da14b0$a10 1280a@phx.gbl.. .[color=blue]
                    >[color=green]
                    > >-----Original Message-----
                    > >If you have Chinese characters then you cannot use a[/color]
                    > char array because a[color=green]
                    > >char is only 8 bits. You need to use a Char[/color]
                    > (System::Char) or a __whcar_t[color=green]
                    > >then.
                    > >
                    > >BTW considering that a Byte is 8 bits I wonder how you[/color]
                    > can store chinese[color=green]
                    > >characters in a Byte array unless you have a random[/color]
                    > kinda layout where[color=green]
                    > >depending on whether the character you want to store is[/color]
                    > unicode or not, you[color=green]
                    > >allot either one byte or two bytes for storing a[/color]
                    > character. Not a very[color=green]
                    > >organized approach in my opinion.
                    > >
                    > >--
                    > >Regards,
                    > >Nish [VC++ MVP]
                    > >[/color]
                    >
                    > The Byte array I got is from a Webclient::Uplo adData call
                    > which put the data returned in a Byte array (which may
                    > contain both one byte Ascii and two-byte Chinese Big-5
                    > code). VC++ can handle char array which may store both
                    > one-byte ascii and two=byte Big-5 characters. I have
                    > several functions which work fine with this situation.
                    > The problem is how to convert the Byte array to a char
                    > array so I have use a char porinter to decode the message
                    > one character at a time.[/color]



                    Comment

                    • Nishant S

                      #11
                      Re: How to convert a Byte array to a char array?

                      System::Char can store both unicode as well as single byte characters.

                      --
                      Regards,
                      Nish [VC++ MVP]



                      "Kueishiong Tu" <kst001@seed.ne t.tw> wrote in message
                      news:3c5001c375 18$abc316a0$a60 1280a@phx.gbl.. .[color=blue]
                      >[color=green]
                      > >-----Original Message-----
                      > >There was en error in my prev post.
                      > >
                      > >char is 8 bits (a byte) and Char is 16 bits (2 bytes)
                      > >[/color]
                      >
                      > Does the 2-byte Char have a fixed length or it can adjust
                      > to the data depending on what are stored (may be one-byte
                      > or two-byte)?
                      > By the way, do you know a convert routine that can
                      > convert a Byte array to a Big-5 char array (a mixture of
                      > one-byte and two-byte characers)?
                      >
                      > regards,
                      > Kueishiong Tu[/color]


                      Comment

                      • Kueishiong Tu

                        #12
                        Re: How to convert a Byte array to a char array?

                        [color=blue]
                        >-----Original Message-----
                        >Hmmm
                        >
                        >Okay I think one of the following is what you want :-
                        >
                        >//this is to obtain a single byte char array
                        >//might lose chinese characters
                        >
                        >char smallarray[512];
                        >for(int i=0; i<barray->Length; i++)
                        > smallarray[i] = barray[i];
                        >
                        >//this gets a wide char array
                        >String* tmp = new String(Encoding ::ASCII->GetChars[/color]
                        (barray));[color=blue]
                        >__wchar_t* array = (__wchar_t*)[/color]
                        Marshal::String ToHGlobalUni([color=blue]
                        > tmp).ToPointer( );
                        >//...
                        >// You can now use array (wide char array)
                        >//...
                        >Marshal::FreeC oTaskMem(array) ;
                        >
                        >I think the second one is what you want. Please try it[/color]
                        out and see if it[color=blue]
                        >works for you.
                        >
                        >--
                        >Regards,
                        >Nish [VC++ MVP]
                        >[/color]

                        The problem I have is that the decoding routine (already
                        available) expects the input to be a char array instead
                        of a wide char array. How do I get around this?
                        Does VC++ .net has the concept of locality so that you
                        have to set up the locality properly before it will
                        recognize the local language (something like sun solaris)?
                        I think this is done implicitly in Microsoft products.

                        Kueishiong Tu

                        Comment

                        • Kueishiong Tu

                          #13
                          Re: How to convert a Byte array to a char array?

                          I have found the solution to my problem. And the solution
                          is surprising easy. It is

                          Byte a[100];
                          char b[100];

                          for(int i=0; i<a->Length; i++)
                          {
                          b[i] = (char) a[i];
                          }

                          The resulting char array then works fine for both 1-byte
                          character and 2-byte character.

                          Comment

                          • Nishant S

                            #14
                            Re: How to convert a Byte array to a char array?

                            Considering that a Byte array *cannot* contain 2-byte characters, the last
                            sentence about how it's fine for both 1-byte and 2-byte characters is a moot
                            point

                            --
                            Regards,
                            Nish [VC++ MVP]



                            "Kueishiong Tu" <kst001@seed.ne t.tw> wrote in message
                            news:18a401c382 86$134603d0$a30 1280a@phx.gbl.. .[color=blue]
                            > I have found the solution to my problem. And the solution
                            > is surprising easy. It is
                            >
                            > Byte a[100];
                            > char b[100];
                            >
                            > for(int i=0; i<a->Length; i++)
                            > {
                            > b[i] = (char) a[i];
                            > }
                            >
                            > The resulting char array then works fine for both 1-byte
                            > character and 2-byte character.[/color]


                            Comment

                            • Nishant S

                              #15
                              Re: How to convert a Byte array to a char array?

                              Considering that a Byte array *cannot* contain 2-byte characters, the last
                              sentence about how it's fine for both 1-byte and 2-byte characters is a moot
                              point

                              --
                              Regards,
                              Nish [VC++ MVP]



                              "Kueishiong Tu" <kst001@seed.ne t.tw> wrote in message
                              news:18a401c382 86$134603d0$a30 1280a@phx.gbl.. .[color=blue]
                              > I have found the solution to my problem. And the solution
                              > is surprising easy. It is
                              >
                              > Byte a[100];
                              > char b[100];
                              >
                              > for(int i=0; i<a->Length; i++)
                              > {
                              > b[i] = (char) a[i];
                              > }
                              >
                              > The resulting char array then works fine for both 1-byte
                              > character and 2-byte character.[/color]


                              Comment

                              Working...