converting an int into hex and memcpy

This is a discussion on converting an int into hex and memcpy within the C Programming forums, part of the General Programming Boards category; I am trying to copy a byte in hex into a char buffer... char str[15]; memcpy (str,"\x11",1); However I need ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    7

    converting an int into hex and memcpy

    I am trying to copy a byte in hex into a char buffer...

    char str[15];
    memcpy (str,"\x11",1);

    However I need calculate the byte I am copying, dynamically, and put it in the char buffer as hex numbers...


    so far I have tried using sprintf ...
    int int_offset=17; //hex 11
    sprintf (str_offset, "%02x", int_offset); // int int_offset=17;
    This gives the correct output (hex 11)

    now the problem is copying the contents of str_offset AS HEX VALUES into str (the char buffer)
    when memcpy is used the values are copies as ASCII values, not hex !.

    I have searched the for the answer and tried many things with the debugger , yet been unsuccessful.
    Would be very grateful for any pointers ....

    Code:
     
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
      char str[15],
           str_offset[4];
      	   
      int  int_offset=17;
      
      sprintf (str_offset, "%02x", int_offset);
      
      memcpy (str ,str_offset,2);
      
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,301
    HEX and ASCII are just different means of representing the same value. Internally the values don't change; they are simply output differently. That's what the %02x format specifier for sprintf() is doing. Change that to %d, and str_offset will contain the bytes '1', '7' and a trailing zero.

    In your last code example, you would be better off memcpy'ing THREE bytes rather than two. This would mean that the trailing zero byte appended by sprintf() is also copied. Alternatively, simply use strcpy(str, str_offset).

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > char str[15];
    > memcpy (str,"\x11",1);
    Seems a long winded way of doing any of these
    Code:
    str[0] = '\x11'; // as a char constant
    str[0] = 0x11;   // an integer, represented in hex
    str[0] = 17;     // in decimal
    str[0] = 021;    // in octal
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Dear All,
    Thank you very much for all the replies....
    The issue is I need to calculate the int_offset dynamically and put in the buffer as hex.

    When using
    str[0] = 0x11;
    is it possible to dynamically calculate the 11 in 0x11 ?.

    for e.g.
    int int_offset=17; //int_offset will be calculated dynamically...

    using
    str[0] = 0x(int_offset); //trying to get the value of int_offset and change to hex

    or

    str[0] = (0x)int_offset; //casting as hex ??

    doesn't work..
    Would be grateful for any comments...

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,705
    There is nothing dynamic about a numeric constant. Your variable is dynamic.
    str[0] = 0x11;
    str[0] = 0x17;
    str[0] = 0xff;
    Wow.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > int int_offset=17; //int_offset will be calculated dynamically...
    In the same way as
    int int_offset=0x11; //int_offset will be calculated dynamically...

    str[0] = int_offset;

    Base is an illusion of how you represent the number.
    The underlying value doesn't change because you decide to represent the base in base 2, base 10 or base 16.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Thank you for the answers....
    sorry, I think I haven't explained the dynmic property clearly....

    the function I am writing is to ....
    1. capture packets off the network and
    2. calculate the offset, depending on the captured data, at run time
    3. copy this offset to a buffer, as hex values and
    4. send it back on the network...

    I tried by hardcoding the hex_offset
    memcpy(payload , "\x44",1); //u_char payload[512];
    this works fine...but I need to calculate the hex_offset at run time based on the captured data (from the network)

    I have tried ...
    1. calculating the offset
    int_offset //int int_offset, is calculated at run time
    2. convert int_offset in to hex and store in hex_offset

    3. memcpy(payload , hex_offset ,1);

    my aim is to build hex_offset to be equalent to "\x44" string and memcpy into payload....

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Well show us how you calculate this "hex offset", because I think you're making a mountain out of nothing.

    If for example you have
    offset = 0x22 - 0x11;
    then you've achieved 34 - 17

    Whether you want to say the checksum (for example) is at offset 0x11 or offset 17 makes not a blind bit of difference to the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    Thanks...
    Code:
    char     *data,
    	 *packet,
    	 *payload,
    	 offset[4];
    
    data = (packet + 14 + LIBNET_IP_H + LIBNET_UDP_H + LIBNET_DNS_H);
    
    datalen = strlen(data); 
    
    sprintf (offset, "%02x", datalen + 23);
    
    memcpy(payload , offset ,2);
    problem here is offset is NOT copied as hex !!!
    Last edited by puppy; 07-02-2006 at 11:57 AM. Reason: more details

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are not paying attention. There is no such thing "as hex". That is just for you the programmer, if you prefer hex over decimal. It's just how you display it. You either get as an integer, or as a string. Or I suppose, as a floating point number. Other than that, there is nothing else. You can take a decimal number and display it however you want. It doesn't matter, it's all the same.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Right now I'm thinking your pointers aren't even pointing at valid memory, nevermind anything else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21