Convert string to to HEX
Hello
how would i go about converting a string value to HEX
ie
char name[10]="STEPHEN";
Convert name to HEX
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
You can pass character by character to itoa with radix 16.
If you only want to display the hex values, you can use printf with format specifiers %x or %X.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
You can pass character by character to itoa with radix 16.
If you only want to display the hex values, you can use printf with format specifiers %x or %X.
I understand how to use the itoa function, but this radix 16?
Regards
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
If you use the radix 10, you will get a string of the an integer in base 10.
For radix 16, you will get a hexadecimal string.
You can try the example in the given web page or the one below.
e.g
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char name[10]="stephen";
char buffer[21]="";
char *pbuffer = buffer;
int len = strlen( name );
for( int i = 0; i < len ;i++ )
{
itoa (name[i],pbuffer,16);
pbuffer +=2;
};
printf( "%s\n", buffer );
return 0;
}
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
When i try to compile, i get this
> gcc string_to_hex.c
Undefined first referenced
symbol in file
itoa /tmp/cc21JKaJ.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
Looks like a linker error. Do you have this problem for my example only or for the example given in the link I posted also?
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Looks like a linker error. Do you have this problem for my example only or for the example given in the link I posted also?
I cant see no given web page, but i changed the code a little to make it as simple as possible with the same error
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
I cant see no given web page,
I mean this web page.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
What are you guys taking about? The original request is:
how would i go about converting a string value to HEX
ie
char name[10]="STEPHEN";
Convert name to HEX
What does this mean?
1) "STEPHEN" is not a number so it can't be converted to Hex
or
2) "STEPHEN" is a string of characters that each have a Hex value. Do you mean add these characters to get a value?
Maybe an actual example would be in order. After the conversion, what should the Hex value of "STEPHEN" be?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
How would i be able to let the user enter HEX and convert it into string?
ie
User Enters 4c3258
and displays L2X
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
How could i use this in a loop, i cant get it to work
The string the user enters has to start as a character string
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
How would i be able to let the user enter HEX and convert it into string?
ie
User Enters 4c3258
and displays L2X
Perhaps something like this.
#include <stdio.h>
int main(void)
{
char name[10] = "4c3258", *ptr = name;
int i;
while ( sscanf(ptr, "%2x", &i) == 1 )
{
putchar(i);
ptr += 2;
}
putchar('\n');
return 0;
}
/* my output
L2X
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Set up a loop that takes 2 characters at a time, convert those 2 characters to a hex value, and output as a character.
To convert 2 chars to hex:
subtract '0' from first giving X. It's now binary.
if X > 9, subract 7 -- it's now hex.
Do the same with 2nd character, making Y
HEX = (X << 4) | Y which shifts the second 'digit' X by 4 bits (multiply by 16), then adds in the first 'digit'
putchar(HEX) outputs the character.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Set up a loop that takes 2 characters at a time, convert those 2 characters to a hex value, and output as a character.
Didn't I do that -- and in a standard and simpler way?
:p
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
????
You have my vote for the most useless post of the year.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Im not quite there, and need a little bit more help, when i run my code, update_record="4c3333"
i get L3^C out.....anyone help ,me so i can get the correct L33.
int Convert_Update_From_Hex(char *update_record, char *field_name )
{
int i,k=0;
char *pconvert=update_record;
char converted_string[20];
while(sscanf(pconvert, "%2x", &i ) == 1 )
{
converted_string[k]=i;
k++;
pconvert +=2;
}
strcpy(update_record,converted_string);
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
Null terminate converted_string to use it correctly with strcpy .
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314