Thread: hexadecimal and character string usage?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    hexadecimal and character string usage?

    hello all,

    Iam trying to use a character string as hexadecimal.

    for example i have a char array[] = "00001767fb693bc7"

    the array represents character but these are hexadecimal characters each one.

    now using memcpy i want to copy to one pointer variable like below

    unsing char *p;
    .
    .
    .
    memcpy(p,array,8);

    but its not getting me correct result.

    result should be the same like below if do .

    Code:
     unsigned char mac1 = 0x00;
      unsigned char mac2 = 0x00;
      unsigned char mac3 = 0x17;
      unsigned char mac4 = 0x67;
      unsigned char mac5 = 0xfb;
      unsigned char mac6 = 0x69;
      unsigned char mac7 = 0x3b;
      unsigned char mac8 = 0xc7;
      syslog(LOG_ERR,"Changed the mac address -3 ");
      memcpy(p, &mac1, 1);
      p++;
      memcpy(p, &mac2, 1);
      p++;
      syslog(LOG_ERR,"Changed the mac address -4 ");
      memcpy(p, &mac3, 1);
      p++;
      memcpy(p, &mac4, 1);
      p++;
      memcpy(p, &mac5, 1);
      p++;
      syslog(LOG_ERR,"Changed the mac address -5 ");
      memcpy(p, &mac6, 1);
      p++;
      memcpy(p, &mac7, 1);
      p++;
      syslog(LOG_ERR,"Changed the mac address -6 ");
      memcpy(p, &mac8, 1);
      p++;*/
    can any body help me here , you help would be highly appreciated.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What is actually happening?
    How is memory for p allocated?

    A string is rather different from the hexadecimal representation of the raw binary data.
    It sounds to me like what you actually want is:
    Code:
    char array[] = {0x00, 0x00, 0x17, 0x67, 0xfb, 0x69, 0x3b, 0xc7};
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    iMalc thanks

    what you said is correct,

    but i have
    Code:
    char array[] = "00001767fb693bc7"
    like this

    i need to make it to like below

    Code:
    char array[] = {0x00, 0x00, 0x17, 0x67, 0xfb, 0x69, 0x3b, 0xc7};
    how can i do using a c syntax

    you help would be highly appreciated.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This is what you are looking for:
    Hex String to Byte Array in C - Stack Overflow

    (I googled "hex string to byte array in C" and it was the first hit)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string::operand+ usage problem (with WIN32 data types)
    By flashbaz-pi in forum C++ Programming
    Replies: 17
    Last Post: 11-02-2008, 02:41 PM
  2. convert string to hexadecimal
    By nocturna_gr in forum C Programming
    Replies: 3
    Last Post: 12-11-2007, 04:45 AM
  3. comparing character in a string to anothr character
    By merike in forum C Programming
    Replies: 5
    Last Post: 05-11-2007, 12:16 AM
  4. file and string streams usage
    By curlious in forum C++ Programming
    Replies: 0
    Last Post: 09-01-2003, 12:35 AM
  5. Replies: 3
    Last Post: 11-03-2002, 02:14 AM