Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. convert UCHAR array to UINT array

convert UCHAR array to UINT array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi! I need to convert UCHAR array to UINT array. Example:

        0      8      16     24     32                 0            32
        +------+------+------+------+                  +------------+
    

    INDATA | 0x11 | 0x22 | 0x33 | 0x44 | == CONVERT ==> | 0x11223344 | OUTDATA
    +------+------+------+------+ +------------+

    I have this two functions.

    /* put byte to dword */
    UINT uchar2uint(UCHAR * in)
    {
    return ((*in << 24) | (*(in + 1) << 16) | (*(in + 2) << 8) | *(in + 3));
    }

    /* put byte array to dword array */
    void uchar2uinta(UCHAR * in, UINT * out, size_t outlen)
    {
    while(outlen--)
    {
    *out++ = uchar2uint(in);
    in += 4;
    }
    }

    int main(void)
    {
    int i;

    UCHAR indata\[8\] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 };
    UINT outdata\[2\];
    
    uchar2uinta(indata, outdata, 2);
    
    for(i = 0; i < 8; ++i)
        printf("%#x ", indata\[i\]);
    printf("\\n");
    
    for(i = 0; i < 2; ++i)
        printf("%#x ", outdata\[i\]);
    printf("\\n");
    
    return 0;
    

    }

    at the console displays the following text:

    0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
    0x11223344 0x55667788

    It's OK! But, i can have a problems, when working in other system with different endianness (big-endian or little-endian)? May be existed more correct way to convert? Sorry for my poor English :) Best regards, Alexander S.

    P H 2 Replies Last reply
    0
    • L Lost User

      Hi! I need to convert UCHAR array to UINT array. Example:

          0      8      16     24     32                 0            32
          +------+------+------+------+                  +------------+
      

      INDATA | 0x11 | 0x22 | 0x33 | 0x44 | == CONVERT ==> | 0x11223344 | OUTDATA
      +------+------+------+------+ +------------+

      I have this two functions.

      /* put byte to dword */
      UINT uchar2uint(UCHAR * in)
      {
      return ((*in << 24) | (*(in + 1) << 16) | (*(in + 2) << 8) | *(in + 3));
      }

      /* put byte array to dword array */
      void uchar2uinta(UCHAR * in, UINT * out, size_t outlen)
      {
      while(outlen--)
      {
      *out++ = uchar2uint(in);
      in += 4;
      }
      }

      int main(void)
      {
      int i;

      UCHAR indata\[8\] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 };
      UINT outdata\[2\];
      
      uchar2uinta(indata, outdata, 2);
      
      for(i = 0; i < 8; ++i)
          printf("%#x ", indata\[i\]);
      printf("\\n");
      
      for(i = 0; i < 2; ++i)
          printf("%#x ", outdata\[i\]);
      printf("\\n");
      
      return 0;
      

      }

      at the console displays the following text:

      0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
      0x11223344 0x55667788

      It's OK! But, i can have a problems, when working in other system with different endianness (big-endian or little-endian)? May be existed more correct way to convert? Sorry for my poor English :) Best regards, Alexander S.

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      What you have done will work in big- or little-endian systems. It is correct. There may be more *efficient* ways to do the conversion, depending on quirks of the hardware, compilers, etc. Regards, Peter

      Software rusts. Simon Stephenson, ca 1994.

      L 1 Reply Last reply
      0
      • P Peter_in_2780

        What you have done will work in big- or little-endian systems. It is correct. There may be more *efficient* ways to do the conversion, depending on quirks of the hardware, compilers, etc. Regards, Peter

        Software rusts. Simon Stephenson, ca 1994.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Many Thanks. I was primarily interested in portability of the code. Regards, Alexander S.

        1 Reply Last reply
        0
        • L Lost User

          Hi! I need to convert UCHAR array to UINT array. Example:

              0      8      16     24     32                 0            32
              +------+------+------+------+                  +------------+
          

          INDATA | 0x11 | 0x22 | 0x33 | 0x44 | == CONVERT ==> | 0x11223344 | OUTDATA
          +------+------+------+------+ +------------+

          I have this two functions.

          /* put byte to dword */
          UINT uchar2uint(UCHAR * in)
          {
          return ((*in << 24) | (*(in + 1) << 16) | (*(in + 2) << 8) | *(in + 3));
          }

          /* put byte array to dword array */
          void uchar2uinta(UCHAR * in, UINT * out, size_t outlen)
          {
          while(outlen--)
          {
          *out++ = uchar2uint(in);
          in += 4;
          }
          }

          int main(void)
          {
          int i;

          UCHAR indata\[8\] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 };
          UINT outdata\[2\];
          
          uchar2uinta(indata, outdata, 2);
          
          for(i = 0; i < 8; ++i)
              printf("%#x ", indata\[i\]);
          printf("\\n");
          
          for(i = 0; i < 2; ++i)
              printf("%#x ", outdata\[i\]);
          printf("\\n");
          
          return 0;
          

          }

          at the console displays the following text:

          0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
          0x11223344 0x55667788

          It's OK! But, i can have a problems, when working in other system with different endianness (big-endian or little-endian)? May be existed more correct way to convert? Sorry for my poor English :) Best regards, Alexander S.

          H Offline
          H Offline
          Harsh Shankar
          wrote on last edited by
          #4

          fasked wrote:

          void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = uchar2uint(in); in += 4; } }

          Try this instead, its simpler and will do the same you are looking for void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = *(UINT*)in; in += 4; } }

          HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.

          L 2 Replies Last reply
          0
          • H Harsh Shankar

            fasked wrote:

            void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = uchar2uint(in); in += 4; } }

            Try this instead, its simpler and will do the same you are looking for void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = *(UINT*)in; in += 4; } }

            HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            thanks, but thus result will be different in systems with big-endian and little-endian. Regars, Alexander S.

            1 Reply Last reply
            0
            • H Harsh Shankar

              fasked wrote:

              void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = uchar2uint(in); in += 4; } }

              Try this instead, its simpler and will do the same you are looking for void uchar2uinta(UCHAR * in, UINT * out, size_t outlen) { while(outlen--) { *out++ = *(UINT*)in; in += 4; } }

              HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              This code will not achieve the results required; take a look at your indexing.

              It's time for a new signature.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups