# Thread: No atoh() function in C ( Ascii To Hex )? - Well, Let's Create One

1. ## No atoh() function in C ( Ascii To Hex )? - Well, Let's Create One

Hi, I'm new to the board and appreciate any input anyone has reguarding this problem of which I've been wrestling of late.

My goal is to have the user enter a Hex depicted input ( ie. "ff0077554433" ) which of course gets interpreted as ascii characters, and then convert the hex depicted ascii characters to actual hex values. Therefore, upon the user entering "ff00775533", after the conversion over to hex, I'd should have a character string in my program that contains the following values:

UserInput=0xff
UserInput=0x00
UserInput=0x77
UserInput=0x55
UserInput=0x44
UserInput=0x33

One may so, "Oh, well that's easy!".

However, after reading various C references as well as searching over the Internet to find code that does this, the only code that I've found so far that works is as follows:

Code:
```unsigned long int UserInput = 0;

int main()
{

/* Get user HEX input */
Printf ( "Please enter hex value\n");
scanf("%x", &UserInput );

/* User then input's something like "ff008877" */
printf("The converted hex text decimal value is now %d",
UserInput );

return 0;

}```
Resulting output from this program:

Code:
`The converted hex text decimal value is now 4278225015` And so this works fine. The conversion is made from the ascii hex input to actual hex values as confirmed by the decimal output.

However, the problem here is that the user is limited to an input of only 4 HEX BYTES.

I REALLY WISH there were a function that took an input such as:

ff008877663344 - ( Ascii Input of course )

and returned a string with each byte containing the following values:

OutPutString = 0xff
OutPutString = 0x00
OutPutString = 0x88
OutPutString = 0x77
OutPutString = 0x66
OutPutString = 0x33
OutPutString = 0x44

Such a function would logically be called atoh ( Ascii to Hex ) and have the following signature:

Code:
` int atoh(char*, char*)`
where the first arg is the string of ascii chars depicting the hex value and the second arg being a buffer of bytes that containes the actual hex values depicted in the user input - BUT THE PROBLEM IS, AS FAR AS I'VE RESEARCHED, SUCH A FUNCTION DOESN'T EXIST in C . Therefore, in trying to create my own version of atoh( char*, char* ) I've written the following program. Please note that for now, instead of implementing this code inside of a function called int atoh(char *, char* ), for testing purposes, the code is contained in a main() program for now.

Code:
```#include <stdio.h>
#include <string.h>

union IntCharUnion
{

unsigned long int UserInputInt;
char UserInputChar;
};

int main()
{

/* Union Containing an integer and char of 4 bytes */
IntCharUnion IntCharVar;

/* Used to reference each element of user input */
int InputIndex = 0;

/* String that captures user input of up to 12 chars. Could
make it larger, but let's keep it simple for now. */
char UserInput = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };

/* The resulting string containing the Converted Ascii --->
Hex Chars. */
char OutputBuffOfHex;

/* Used to temporarily store 4 hex values that ultimately get
appended to the output string */
char PreConversionBuff;

printf ( "Please enter hex value\n");
scanf("%s", UserInput );

/* At this point, user then input's something like "ff008877554422" */

/* Set up loops that peform the conversion to actual hex */
while( UserInput[InputIndex] != '0' )
{

/* Every 4 bytes, convert the latest 4 bytes from ascii to hex via.
the %x specifier. We can only do this 4 bytes at a time using the
%x specifier. */
if( ( InputIndex != 0) && ( ( InputIndex % 4 ) == 0 ) )
{
/* Perform the conversion for this grouping of 4 bytes */
sscanf( PreConversionBuff, "%x",
&IntCharVar.UserInputInt );

/* And Now, append the new Hex values inside of the Union
to the output string */
strcat( OutputBuffOfHex, IntCharVar.UserInputChar);

InputIndex++;

}
/* Else, transfer this byte to the buffer. */
else
{
PreConversionBuff[InputIndex] = UserInput[InputIndex];
InputIndex++;
}
/* end if */

}/* end while */

printf("\nThe value of the OutputBuffOfHex is %x\n", OutputBuffOfHex );
}```

Program output:

Code:
```Please enter hex value
1122

The value of the OutputBuffOfHex is ffbef301``` After investingating this closely, the problem appears to be in using the union.
That is, after the following line of code gets executed:

Code:
```/* Perform the conversion for this grouping of 4 bytes */
sscanf( PreConversionBuff, "%x",
&IntCharVar.UserInputInt );```
IntCharVar.UserInputInt DOES CONTAIN the proper value. But, when examining IntCharVar.UserInputChar, the value stored in there is completely different.

So, then, I wrote a smaller program to see how I could get the union to work the way I wanted.
The program is as follows:

Code:
```union IntCharUnion
{
unsigned long int UserInputInt;
char UserInputChar;
};

int main()
{

IntCharUnion IntCharVar;

Printf ( "Please enter hex value\n");
scanf("%x", &IntToStringOfHex.UserInputInt );

/* User then input's something like "ff008877" */

/* Then, just as a sanity check, print out what's in the Char
String portion of the UnionVar */
printf( "Hex values now stored in the char string portion of
the Union are as follows: %x", IntToStringOfHex.
UserInputChar );

return 0;
}```
Output of this program:

Code:
```Please enter hex value
ff008877

Hex values now stored in the char string portion of the Union are
as follows: ffbef316``` Of course the desired result is:

Code:
```Hex values now stored in the char string portion of the Union are
as follows: ff008877```
Then, I got desperate and decided to completely ditch the whole Union idea. You see, all I'm trying to do is to get the frigin' 4 bytes stored in the integer transferred over to a 4 byte character string. So I then wrote the following simple program to once again attempt to transfer what's stored in the 4 byte integer var over to a 4 byte character string:

Code:
```#include <stdio.h>
#include <string.h>

int main()
{

char UserInput;
char DestinationBuffer;
int intBuff = 0;

DestinationBuffer = 0;
DestinationBuffer = 0;
DestinationBuffer = 0;
DestinationBuffer = 0;

/* Get the entire ascii hex digit input and store into a char array */
printf("Please enter the the hex packet of data\n", UserInput );
scanf("%s", UserInput );

/* Convert the ascii input to Hex via. the "%x" specifier */
sscanf(UserInput, "%x", &intBuff );
printf ("The int value is now %d\n", intBuff);

/* And finally, move the 4 byte hex value from the integer to
the desired output string. */
memmove( DestinationBuffer, &intBuff, 4);
printf("%x\n", DestinationBuffer );

return 0;

}```

Program output:

Code:
```Please enter the the hex packet of data
1122
The int value is now 4386
ffbef316``` Again, the conversion from ascii hex depicted input to hex actual hex worked:

Code:
```/* Convert the ascii input to Hex via. the "%x" specifier */
sscanf(UserInput, "%x", &intBuff );
printf ("The int value is now %d\n", intBuff);```
But, the next line of code that attempts to move what's now inside of the 4 byte integer over to the 4 byte array of characters still fails at this line of code:

Code:
```* And finally, move the 4 byte hex value from the integer to
the desired output string. */
memmove( DestinationBuffer, &intBuff, 4);
printf("%x\n", DestinationBuffer );```
Any ideas on what I'm doing wrong here - in trying to transfer the 4 bytes in the integer over to the 4 byte character string? Or more importantly, does anyone have any better ideas on converting ascii ( hex depicted chars ) input over to the actual hex values depicted - period?

dedham_ma_man 2. All you really need is to convert each 2 characters entered into a single character, and to display each character in your new array as its decimal value. You can't treat the second array as a string, in case they enter 00.

Quzah. 3. Like this?
Code:
```#include <stdio.h>

int main ( ) {
char buffer[] = "ff008877554422\n";
int res, pos = 0;
unsigned int byte;
printf( "Scanning %s", buffer );
while ( (res=sscanf( &buffer[pos], "%2x", &byte )) == 1 ) {
printf( "res=%d, byte=%d(%02x)\n", res, byte, byte );
pos += 2;
}
return 0;
}``` 4. Sorry, long post, I only skimmed after a bit. Originally Posted by dedham_ma_man
However, the problem here is that the user is limited to an input of only 4 HEX BYTES.

I REALLY WISH there were a function that took an input such as:

ff008877663344 - ( Ascii Input of course )

and returned a string with each byte containing the following values:

OutPutString = 0xff
OutPutString = 0x00
OutPutString = 0x88
OutPutString = 0x77
OutPutString = 0x66
OutPutString = 0x33
OutPutString = 0x44
Wouldn't simply limiting the input size for *scanf do what you want?
Code:
```#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main( void )
{
char input[] = "ff008877663344", *ptr = input;
unsigned int byte;
unsigned char array;
size_t i, j;
for ( i = 0; i < sizeof array; ++i )
{
if ( sscanf(ptr, "%2x", &byte) != 1 )
{
break;
}
array[i] = byte;
ptr += 2;
}
for ( j = i, i = 0; i < j; ++i )
{
printf("array[%lu] = %02X\n", (long unsigned)i, (unsigned)array[i]);
}
return 0;
}

/* my output
array = FF
array = 00
array = 88
array = 77
array = 66
array = 33
array = 44
*/``` 5. I can't argue that this is simpler than what's already been suggested, but it's probably faster with larger strings:

Code:
```char *
atohx(char * dst, const char * src)
{
char
* ret = dst;
for(int lsb, msb; *src; src += 2)
{
msb = tolower(*src);
lsb = tolower(*(src + 1));
msb -= isdigit(msb) ? 0x30 : 0x57;
lsb -= isdigit(lsb) ? 0x30 : 0x57;
if((msb < 0x0 || msb > 0xf) || (lsb < 0x0 || lsb > 0xf))
{
*ret = 0;
return NULL;
}
*dst++ = (char)(lsb | (msb << 4));
}
*dst = 0;
return ret;
}``` 6. Thanks all - this has helped tremendously - ie. converting from Ascii depicted Hex to char string.

As mentioned in the second part of this long post of mine, how would one go from a 4byte char string back to an int.

In other words, if I have:

CharString = 0x00
CharString = 0x88
CharString = 0x00
CharString = 0x00

and want to move those 4 bytes into an Integer, how would I do that?

Among other things, I've tried the following:

Code:
```char BitBuffer = { 0x00, 0x88, 0x00, 0x00 };
int IntBits = 0;
unsigned int byte = 0;

//printf( "The value of BitBuffer is %2x\n", BitBuffer);

sscanf(BitBuffer, "%d", &IntBits );

printf("\nThe integer value of IntBits is %d\n", IntBits );```
output of this program:

Code:
`The integer value of IntBits is 0`
Any other ideas? Thanks again.  7. Is this the output you expect?
Code:
```#include <stdio.h>

int main( void )
{
char BitBuffer = { 0x00, 0x88, 0x00, 0x00 };
int IntBits = *(int*)BitBuffer;
printf("The integer value of IntBits is %d\n", IntBits );
return 0;
}

/* my output
The integer value of IntBits is 34816
*/``` 8. Beautiful, thank you. 9. Originally Posted by Dave_Sinkula
Is this the output you expect?
Code:
```#include <stdio.h>

int main( void )
{
char BitBuffer = { 0x00, 0x88, 0x00, 0x00 };
int IntBits = *(int*)BitBuffer;
printf("The integer value of IntBits is %d\n", IntBits );
return 0;
}

/* my output
The integer value of IntBits is 34816
*/```
Ok, so when I initiate BitBuffer to { 0x99, 0x88, 0x77, 0x00 } instead of { 0x00, 0x88, 0x00, 0x00 } in the above example, my output is now -1719109888.

So, I've tried the following:

Code:
```char BitBuffer = { 0x99, 0x88, 0x77, 0x00 };
unsigned int IntBits = *(unsigned int*)BitBuffer;
printf("The integer value of IntBits is %d\n", IntBits );
return 0;```
I still get the signed output -1719109888 So, I became desperate, I've tried this:

Code:
```char BitBuffer = { 0x99, 0x88, 0x77, 0x00 };
unsigned long int IntBits = *(unsigned long int*)BitBuffer;
printf("The integer value of IntBits is %d\n", IntBits );
return 0;```
I still get the signed output -1719109888.

The output should be 10061943 Any ideas what I'm missing?
Thanks again Code:
` char BitBuffer = { 0x00, 0x99, 0x88, 0x77 };`
Also, use %lu, not %d for printing unsigned longs.

Also, casting in this way is likely to get you a bus error on some machines, if the alignment doesn't match.
memcpy( &IntBits, BitBuffer, sizeof IntBits );
is a much safer way 11. Originally Posted by Salem
Code:
` char BitBuffer = { 0x00, 0x99, 0x88, 0x77 };`
Also, use %lu, not %d for printing unsigned longs.

Also, casting in this way is likely to get you a bus error on some machines, if the alignment doesn't match.
memcpy( &IntBits, BitBuffer, sizeof IntBits );
is a much safer way
OK Thank you. 12. Another option that could do what you want:
Code:
```union
{
unsigned int ival;
unsigned char bval;
} un;``` Popular pages Recent additions 