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 unsigned 16 bit Word

Convert unsigned 16 bit Word

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
5 Posts 5 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.
  • S Offline
    S Offline
    simonpearson
    wrote on last edited by
    #1

    Hello all, This is my first post to this forum, so apologies in advance for the lack of clarity. Im trying to assign an unsigned 16 bit word to a char array i.e. two 8 bit single bytes. my value (u16 sum) contains the word value 24fa which I need to insert into the middle of a char array buff[100]. Obviously, I am concious of the fact that the array is of type char (8 bit), thus any allocation will shorten the original value e.g. buff[0] = sum //buff[0] only contains 0x24 Is there any way to change the word value to two 8 bit singular bytes for storage? Or is there another way via strcat/memcpy etc..etc Many thanks in advance. Stu

    F C R 3 Replies Last reply
    0
    • S simonpearson

      Hello all, This is my first post to this forum, so apologies in advance for the lack of clarity. Im trying to assign an unsigned 16 bit word to a char array i.e. two 8 bit single bytes. my value (u16 sum) contains the word value 24fa which I need to insert into the middle of a char array buff[100]. Obviously, I am concious of the fact that the array is of type char (8 bit), thus any allocation will shorten the original value e.g. buff[0] = sum //buff[0] only contains 0x24 Is there any way to change the word value to two 8 bit singular bytes for storage? Or is there another way via strcat/memcpy etc..etc Many thanks in advance. Stu

      F Offline
      F Offline
      Fired Fish
      wrote on last edited by
      #2

      oh me too .I can not know use this forum my english is poor haha I

      P 1 Reply Last reply
      0
      • S simonpearson

        Hello all, This is my first post to this forum, so apologies in advance for the lack of clarity. Im trying to assign an unsigned 16 bit word to a char array i.e. two 8 bit single bytes. my value (u16 sum) contains the word value 24fa which I need to insert into the middle of a char array buff[100]. Obviously, I am concious of the fact that the array is of type char (8 bit), thus any allocation will shorten the original value e.g. buff[0] = sum //buff[0] only contains 0x24 Is there any way to change the word value to two 8 bit singular bytes for storage? Or is there another way via strcat/memcpy etc..etc Many thanks in advance. Stu

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        simonpearson wrote: Is there any way to change the word value to two 8 bit singular bytes for storage BYTE b1 = value & 0xff; BYTE b2 = (value >> 8); or WORD *pw = &value; BYTE *pb = (BYTE *)pw; // now, pb[0] is the first byte of your WORD and pb[1] is the second etc.. Cleek | Image Toolkits | Thumbnail maker

        1 Reply Last reply
        0
        • S simonpearson

          Hello all, This is my first post to this forum, so apologies in advance for the lack of clarity. Im trying to assign an unsigned 16 bit word to a char array i.e. two 8 bit single bytes. my value (u16 sum) contains the word value 24fa which I need to insert into the middle of a char array buff[100]. Obviously, I am concious of the fact that the array is of type char (8 bit), thus any allocation will shorten the original value e.g. buff[0] = sum //buff[0] only contains 0x24 Is there any way to change the word value to two 8 bit singular bytes for storage? Or is there another way via strcat/memcpy etc..etc Many thanks in advance. Stu

          R Offline
          R Offline
          Rick York
          wrote on last edited by
          #4

          You can use a union. This union can be used to extract the individual bytes of most of the primitive data types. You can add more types as needed.

          typedef union
          {
          double Double;
          float Float;
          UINT Int;
          USHORT Short;
          UCHAR Bytes[sizeof(double)];
          } UnionBytes;

          UnionBytes ub;
          ub.Short = your_short;

          = wb.Bytes[0]; // first byte

          your_string[x+1] = wb.Bytes[1]; // second byte

          I know you didn't ask this but you can do something similar to access the bits of a byte :

          typedef union
          {
          UCHAR Byte;
          struct
          {
          unsigned Bit0 : 1;
          unsigned Bit1 : 1;
          unsigned Bit2 : 1;
          unsigned Bit3 : 1;
          unsigned Bit4 : 1;
          unsigned Bit5 : 1;
          unsigned Bit6 : 1;
          unsigned Bit7 : 1;
          } Bits;
          } ByteBits

          1 Reply Last reply
          0
          • F Fired Fish

            oh me too .I can not know use this forum my english is poor haha I

            P Offline
            P Offline
            Prakash Nadar
            wrote on last edited by
            #5

            fisheryj wrote: haha So you find it funny if your english is poor?


            -prakash

            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