int packed as hex with memcpy

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

    int packed as hex with memcpy

    I would have a number packed with its hex representation of the integer
    below is some sample code of what is being done.

    int value = 20; //in hex it is 0x14

    AddData (value);
    ..
    ..
    ..

    AddData( USHORT myVal.....)
    {
    UCHAR tmp2[2];
    tmp2[1] = myVal & 0x00FF;
    tmp2[0] = (myVal & 0xFF00) >> 8 ;

    memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*
    }

    when I check the hex dump I see that 0x0020 was packed instead of the 0x0014
    that I want. How can I set the proper flags(?) "if" that is the solution to
    pack the hex representation of the integer?


    Thanks, your help is greatly appreciated!


    Delali


  • Ron Natalie

    #2
    Re: int packed as hex with memcpy


    "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message news:bm1hbf$oqo $1@houston.jhua pl.edu...[color=blue]
    > I would have a number packed with its hex representation of the integer
    > below is some sample code of what is being done.
    >
    > int value = 20; //in hex it is 0x14[/color]
    [color=blue]
    > AddData( USHORT myVal.....)
    > {
    > UCHAR tmp2[2];[/color]
    presumably UCHAR is unsigned char?[color=blue]
    > tmp2[1] = myVal & 0x00FF;
    > tmp2[0] = (myVal & 0xFF00) >> 8 ;
    >
    > memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*[/color]
    memcpy(myPkt, tmp2, sizeof tmp2);
    [color=blue]
    > when I check the hex dump I see that 0x0020 was packed instead of the 0x0014
    > that I want. How can I set the proper flags(?) "if" that is the solution to
    > pack the hex representation of the integer?[/color]

    There are no flags and there's no such thing as a hex representation in the code.
    The above have stored the value which is both 20 decimal and 14 hex and 24 octal
    etc...tmp2[1]. What makes you think otherwise? Are you sure your dumper is really
    showing you the bytes in hex?


    Comment

    • Peter van Merkerk

      #3
      Re: int packed as hex with memcpy

      "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message
      news:bm1hbf$oqo $1@houston.jhua pl.edu...[color=blue]
      > I would have a number packed with its hex representation of the integer
      > below is some sample code of what is being done.
      >
      > int value = 20; file://in hex it is 0x14
      >
      > AddData (value);
      > .
      > .
      > .
      >
      > AddData( USHORT myVal.....)
      > {
      > UCHAR tmp2[2];
      > tmp2[1] = myVal & 0x00FF;
      > tmp2[0] = (myVal & 0xFF00) >> 8 ;
      >
      > memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*
      > }
      >
      > when I check the hex dump I see that 0x0020 was packed instead of the[/color]
      0x0014[color=blue]
      > that I want. How can I set the proper flags(?) "if" that is the solution[/color]
      to[color=blue]
      > pack the hex representation of the integer?[/color]

      The code you posted is unfortunately not compilable since AddData() does not
      have a return type, and UCHAR and USHORT are not defined in the C++
      standard. That being said, I see nothing in your code that would explain the
      values in your hex dump. Are you sure that the tool you are using is
      displaying the data in hexadecimal format and not decimal format?

      --
      Peter van Merkerk
      peter.van.merke rk(at)dse.nl


      Comment

      • Andrey Tarasevich

        #4
        Re: int packed as hex with memcpy

        Delali Dzirasa wrote:[color=blue]
        > I would have a number packed with its hex representation of the integer
        > below is some sample code of what is being done.[/color]

        You should probably start by explaining what you understand under "hex
        representation of the integer" and under "packing a number with its hex
        representation" in this particular case. "Hex representation" is in
        essence a sequence of characters from '0'..'9', 'A'..'F' set or
        something like this. I don't see anything in your code that has anything
        to do with obtaining a hex representation of any number, let alone the
        "packing".
        [color=blue]
        > int value = 20; //in hex it is 0x14
        >
        > AddData (value);
        > .
        > .
        > .
        >
        > AddData( USHORT myVal.....)
        > {
        > UCHAR tmp2[2];
        > tmp2[1] = myVal & 0x00FF;
        > tmp2[0] = (myVal & 0xFF00) >> 8 ;
        >
        > memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*
        > }
        >
        > when I check the hex dump I see that 0x0020 was packed instead of the 0x0014
        > that I want. How can I set the proper flags(?) "if" that is the solution to
        > pack the hex representation of the integer?[/color]

        --
        Best regards,
        Andrey Tarasevich
        Brainbench C and C++ Programming MVP

        Comment

        • Delali Dzirasa

          #5
          Re: int packed as hex with memcpy

          Yes they are sent to a file via another program that I am testing, this
          program is acting like a simulator and the other application sits and waits
          for data then displays them in log files as to what was sent, so I am not
          entirely sure how they are bring printed. I read the log files by opening
          then as a binary file and viewing the content. here is a bit of
          clarification as to what is happening.

          AddData( USHORT myVal.....)
          {
          //in the first case myVal is 20

          UCHAR tmp2[2]; //yes unsigned char
          tmp2[1] = myVal & 0x00FF;
          tmp2[0] = (myVal & 0xFF00) >> 8 ;
          memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*

          myVal = 0x1500;
          tmp2[1] = myVal & 0x00FF;
          tmp2[0] = (myVal & 0xFF00) >> 8 ;
          memcpy(&myPkt[2], &tmp2, 2); // (where myPkt is a UCHAR*

          myVal = 0x0430;
          tmp2[1] = myVal & 0x00FF;
          tmp2[0] = (myVal & 0xFF00) >> 8 ;
          memcpy(&myPkt[4], &tmp2, 2); // (where myPkt is a UCHAR*
          }


          in the binary file I see : 00 20 15 00 04 30

          when I need to be seeing: 00 14 15 00 04 30
          When I explicitly assign myVal a hex value (0x.......) it works fine.....but
          when it is represented in decimal it pack the that decimal as if it were the
          original hex value ( ie 0020, and not 0014);




          I hope this is a little more clear

          Delali

          also when I try to change the code: memcpy(&myPkt[2], &tmp2, 2);
          as suggested to: memcpy(myPkt[2], tmp2, 2);" I get the following error

          "error C2664: 'memcpy' : cannot convert parameter 1 from 'unsigned char' to
          'void *'"



          "Peter van Merkerk" <merkerk@deadsp am.com> wrote in message
          news:bm1l6t$hc4 hv$1@ID-133164.news.uni-berlin.de...[color=blue]
          > "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message
          > news:bm1hbf$oqo $1@houston.jhua pl.edu...[color=green]
          > > I would have a number packed with its hex representation of the integer
          > > below is some sample code of what is being done.
          > >
          > > int value = 20; file://in hex it is 0x14
          > >
          > > AddData (value);
          > > .
          > > .
          > > .
          > >
          > > AddData( USHORT myVal.....)
          > > {
          > > UCHAR tmp2[2];
          > > tmp2[1] = myVal & 0x00FF;
          > > tmp2[0] = (myVal & 0xFF00) >> 8 ;
          > >
          > > memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*
          > > }
          > >
          > > when I check the hex dump I see that 0x0020 was packed instead of the[/color]
          > 0x0014[color=green]
          > > that I want. How can I set the proper flags(?) "if" that is the[/color][/color]
          solution[color=blue]
          > to[color=green]
          > > pack the hex representation of the integer?[/color]
          >
          > The code you posted is unfortunately not compilable since AddData() does[/color]
          not[color=blue]
          > have a return type, and UCHAR and USHORT are not defined in the C++
          > standard. That being said, I see nothing in your code that would explain[/color]
          the[color=blue]
          > values in your hex dump. Are you sure that the tool you are using is
          > displaying the data in hexadecimal format and not decimal format?
          >
          > --
          > Peter van Merkerk
          > peter.van.merke rk(at)dse.nl
          >
          >[/color]


          Comment

          • Howard

            #6
            Re: int packed as hex with memcpy


            "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message
            news:bm1hbf$oqo $1@houston.jhua pl.edu...[color=blue]
            > I would have a number packed with its hex representation of the integer
            > below is some sample code of what is being done.
            >
            > int value = 20; //in hex it is 0x14
            >
            > AddData (value);
            > .
            > .
            > .
            >
            > AddData( USHORT myVal.....)
            > {
            > UCHAR tmp2[2];
            > tmp2[1] = myVal & 0x00FF;[/color]

            Suppose you pass in the decimal value 20. That's 0x14 in hex. The above
            line masks off the high word (of a character, which is smaller!), so that
            you are doing 0x0014 & 0x00ff, which results in 0x0014. Stored in an
            unsigned char, that is 0x14, which is just what you started with: decimal
            20!
            [color=blue]
            > tmp2[0] = (myVal & 0xFF00) >> 8 ;[/color]

            Here, you have (0x0014 & 0xff00)>>8, which results in (0x0000) >> 8, which
            is 0x0000, or simply decimal 0 (zero). So, your two unsigned chars stored
            in tmp2 are 0 and 20.
            [color=blue]
            >
            > memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*
            > }
            >
            > when I check the hex dump I see that 0x0020 was packed instead of the[/color]
            0x0014[color=blue]
            > that I want. How can I set the proper flags(?) "if" that is the solution[/color]
            to[color=blue]
            > pack the hex representation of the integer?
            >[/color]

            I think your "hex dump" is not hex at all, but decimal, showing the first
            byte as zero, and the second as 20, just like your code told it to do.

            I'm not sure why you want to take an unsigned short and store it in two
            unsigned characters, but that's hardly "packing", which implies reducing the
            space required. What do you need in the output? Characters representing
            the hex digits such as ['0','0','1','4']? A pair of unsigned char values,
            one for each hex digit, such as [0,0,1,4]? Or do you really need to do this
            "packng" at all? I mean, a hex dump of the original decinal value 20 will
            show you 0x0014 just like you've been trying to get in the first place.

            If you're trying to get the hex digits as separate values, such as
            [0,0,1,4], remember that you've got 4 bytes in a short, not 2, and will need
            an array of 4 unsigned char's to handle all possible unsigned short values.
            And, your masks should mask off one byte at a time, not two like 0x00ff and
            0xff00 do.

            -Howard





            Comment

            • Jakob Bieling

              #7
              Re: int packed as hex with memcpy

              "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message
              news:bm1hbf$oqo $1@houston.jhua pl.edu...[color=blue]
              > I would have a number packed with its hex representation of the integer
              > below is some sample code of what is being done.[/color]


              I do not really understand what you are trying to do. But I think that
              you think that an integer can hold a number in decimal representation and
              *in addition* in a, as you say, packed hexadecimal representation. That is
              not the case. Assign 20 to an int and you will get the *exact* same bit
              pattern when assigning 0x14 to it (which is 00010100 in both cases).

              In another post you said that if you use the hexadecimal representation,
              it works fine. I do not want to call you a liar, but whether you pass '20'
              or '0x14' is completely irrelevant and will produce the *exact* same code.
              It might be a good idea to post a new *minimal*, compilable code sample,
              which people just have to paste into an new cpp file and compile.

              hth
              --
              jb

              (replace y with x if you want to reply by e-mail)


              Comment

              • Ron Natalie

                #8
                Re: int packed as hex with memcpy


                "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message news:bm1mhs$lic $1@houston.jhua pl.edu...
                [color=blue]
                >
                > also when I try to change the code: memcpy(&myPkt[2], &tmp2, 2);
                > as suggested to: memcpy(myPkt[2], tmp2, 2);" I get the following error[/color]

                I didn't suggest that.



                Comment

                • Ron Natalie

                  #9
                  Re: int packed as hex with memcpy


                  "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message news:bm1mhs$lic $1@houston.jhua pl.edu...[color=blue]
                  > Yes they are sent to a file via another program that I am testing,[/color]

                  Make a complete version of a program that demonstrates the problem AND compiles.
                  You keep giving us fragments, which look like they ought to be fine. We're not
                  clairvoyant. The fault is almost certianly in the creation of the value you pass
                  to AddData.


                  Comment

                  • Howard

                    #10
                    Re: int packed as hex with memcpy


                    "Ron Natalie" <ron@sensor.com > wrote in message
                    news:3f846fd8$0 $36937$9a6e19ea @news.newshosti ng.com...[color=blue]
                    >
                    > "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message[/color]
                    news:bm1mhs$lic $1@houston.jhua pl.edu...[color=blue][color=green]
                    > > Yes they are sent to a file via another program that I am testing,[/color]
                    >
                    > Make a complete version of a program that demonstrates the problem AND[/color]
                    compiles.[color=blue]
                    > You keep giving us fragments, which look like they ought to be fine.[/color]
                    We're not[color=blue]
                    > clairvoyant. The fault is almost certianly in the creation of the value[/color]
                    you pass[color=blue]
                    > to AddData.
                    >
                    >[/color]

                    I think the fault is actually with the fact the OP is masking off the
                    high/low words but trying to get bytes:

                    tmp2[1] = myVal & 0x00FF;
                    tmp2[0] = (myVal & 0xFF00) >> 8 ;


                    -Howard


                    Comment

                    • Howard

                      #11
                      Re: int packed as hex with memcpy


                      "Howard" <alicebt@hotmai l.com> wrote in message
                      news:bm1mtc$nej @dispatch.conce ntric.net...

                      A correction is in order: I got so confused by the original post, I got my
                      own bytes and words mixed up. The mask is wrong, as I stated, but whereever
                      I said "byte" I should have said "nibble", and where I said "word" I should
                      have said "byte". The problem still is in the mask, in that to get the
                      first (low-order) hex digit (nibble), you need:

                      y = x & 0x000f;

                      not

                      y = x & 0x00ff;

                      -Howard


                      Comment

                      • Howard

                        #12
                        Re: int packed as hex with memcpy


                        "Ron Natalie" <ron@sensor.com > wrote in message
                        news:3f846fd8$0 $36937$9a6e19ea @news.newshosti ng.com...[color=blue]
                        >
                        > "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message[/color]
                        news:bm1mhs$lic $1@houston.jhua pl.edu...[color=blue][color=green]
                        > > Yes they are sent to a file via another program that I am testing,[/color]
                        >
                        > Make a complete version of a program that demonstrates the problem AND[/color]
                        compiles.[color=blue]
                        > You keep giving us fragments, which look like they ought to be fine.[/color]
                        We're not[color=blue]
                        > clairvoyant. The fault is almost certianly in the creation of the value[/color]
                        you pass[color=blue]
                        > to AddData.
                        >
                        >[/color]
                        I think the fault is actually with the fact the OP is masking off the
                        high/low bytes but trying to get the hex digits, which would be "nibbles":

                        This:
                        tmp2[1] = myVal & 0x00FF;

                        should be:
                        tmp2[3] = myVal & 0x000f;

                        (and there should be four unsigned char's to handle the four hex digits
                        properly, not two.)

                        -Howard


                        Comment

                        • Samuel Barber

                          #13
                          Re: int packed as hex with memcpy

                          "Delali Dzirasa" <Delali.Dzirasa @jhuapl.edu> wrote in message news:<bm1mhs$li c$1@houston.jhu apl.edu>...[color=blue]
                          > AddData( USHORT myVal.....)
                          > {
                          > //in the first case myVal is 20[/color]

                          //no it isn't

                          Ever heard of a debugger?

                          Sam

                          Comment

                          • Thomas Matthews

                            #14
                            Re: int packed as hex with memcpy

                            Delali Dzirasa wrote:
                            [color=blue]
                            > I would have a number packed with its hex representation of the integer
                            > below is some sample code of what is being done.
                            >
                            > int value = 20; //in hex it is 0x14
                            >
                            > AddData (value);
                            > .
                            > .
                            > .
                            >
                            > AddData( USHORT myVal.....)
                            > {
                            > UCHAR tmp2[2];
                            > tmp2[1] = myVal & 0x00FF;
                            > tmp2[0] = (myVal & 0xFF00) >> 8 ;
                            >
                            > memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*
                            > }
                            >
                            > when I check the hex dump I see that 0x0020 was packed instead of the 0x0014
                            > that I want. How can I set the proper flags(?) "if" that is the solution to
                            > pack the hex representation of the integer?
                            >
                            >
                            > Thanks, your help is greatly appreciated!
                            >
                            >
                            > Delali
                            >
                            >[/color]

                            Try this sequence:
                            char temp;
                            std::list<char> ascii_num;
                            unsigned int value = 0x14; // 20 in decimal.
                            temp = static_cast<cha r>(value & 0x0F); // keep Least Significant
                            // Hex digit. [1]
                            if (temp < 10)
                            temp += '0'; // Assumes '0'..'9' are contiguous.
                            else
                            temp += 'A'; // Assumes 'A'..'F' are contiguous.
                            ascii_num.push_ front(temp);
                            value = value >> 4; // 4 bits per hex digit.

                            Notes:
                            [1] a 'char' type may be unsigned or signed. However, it is
                            guaranteed to be at least 8 bits in size. Only 4 bits are needed
                            from the original value, which should fit in either a signed
                            or unsigned char.

                            --
                            Thomas Matthews

                            C++ newsgroup welcome message:

                            C++ Faq: http://www.parashift.com/c++-faq-lite
                            C Faq: http://www.eskimo.com/~scs/c-faq/top.html
                            alt.comp.lang.l earn.c-c++ faq:

                            Other sites:
                            http://www.josuttis.com -- C++ STL Library book

                            Comment

                            • Howard

                              #15
                              Re: int packed as hex with memcpy


                              "Thomas Matthews" <Thomas_Matthew sHatesSpam@sbcg lobal.net> wrote in message
                              news:ffghb.1184 3> if (temp < 10)
                              [color=blue]
                              > temp += 'A'; // Assumes 'A'..'F' are contiguous.[/color]

                              You need to subtract 10 from that!

                              -Howard


                              Comment

                              Working...