#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    0
    Rep Power
    0

    Make 2 Bytes Char into 1Byte Char(Hex )


    Hello,

    I currently have a character array of 16 chars. These 16 chars are actually HEX pairs meaning hexstring[0] = 3 hexstring[1] = f, hexstring[2]=3 hexstring[3]=d, ectc. What I need to do is join these HEX pairs togather and create an array of 8 chars.

    I need to be able to take hexstring[0] and hexstring[1] and turn them into the HEX(3f from above) and make 3f = to newString[0].

    Thanks for your help,

    techyjt
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    try this:

    char *hexString='2f1e';

    struct {
    char c1, c2;
    } s;

    ......

    s=hexString;
    (s[0] should be "2f" and s[1] should be "1e" - untested, if it does not work, weŽll find another approach :) )
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    I think you're looking for something like this:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    int hextoint(char);
    
    int main() {
      char *inputbuf = "48454c4C4F";
      char outputbuf[20];
      char *ptr, *ptr2;
      int temp;
    
      for (ptr = inputbuf, ptr2 = outputbuf; *ptr; ptr+=2, ptr2++) {
        temp = (hextoint(*ptr) << 4) + hextoint(*(ptr+1));
        *ptr2 = (char)temp;
      }
      *ptr2 = '\0';
      
      printf("The value is %s\n", outputbuf);
      return 0;
    }
    
    int hextoint(char c) {
      c = toupper(c);
      return (c > '9' ? c - 'A' + 10 : c - '0');
    }

IMN logo majestic logo threadwatch logo seochat tools logo