|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 )
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#3
|
||||
|
||||
|
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');
}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Make 2 Bytes Char into 1Byte Char(Hex ) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|