943,935 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 74879
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 5th, 2006
0

Convert string to to HEX

Expand Post »
Hello

how would i go about converting a string value to HEX

ie

char name[10]="STEPHEN";

Convert name to HEX
Similar Threads
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Jul 5th, 2006
0

Re: Convert string to to HEX

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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jul 5th, 2006
0

Re: Convert string to to HEX

Quote originally posted by WolfPack ...
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
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Jul 5th, 2006
0

Re: Convert string to to HEX

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
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. int main()
  5. {
  6. char name[10]="stephen";
  7. char buffer[21]="";
  8. char *pbuffer = buffer;
  9. int len = strlen( name );
  10. for( int i = 0; i < len ;i++ )
  11. {
  12. itoa (name[i],pbuffer,16);
  13. pbuffer +=2;
  14. };
  15. printf( "%s\n", buffer );
  16. return 0;
  17. }
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jul 5th, 2006
0

Re: Convert string to to HEX

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
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Jul 5th, 2006
0

Re: Convert string to to HEX

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?
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jul 5th, 2006
0

Re: Convert string to to HEX

Quote originally posted by WolfPack ...
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
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Jul 5th, 2006
0

Re: Convert string to to HEX

Quote originally posted by sgriffiths ...
I cant see no given web page,
I mean this web page.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jul 6th, 2006
0

Re: Convert string to to HEX

Quote originally posted by sgriffiths ...
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
Yes of course itoa is nonstandard. U can implement your own itoa
http://www.daniweb.com/techtalkforum...highlight=itoa
or use this code
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5. char name[10]="STEPHEN";
  6. char buffer[21]="";
  7. char *pbuffer = buffer;
  8. int len = strlen( name );
  9. int i;
  10. for(i = 0; i < len ;i++ )
  11. {
  12. sprintf(pbuffer, "%x", name[i]);
  13. pbuffer +=2;
  14. }
  15. printf( "%s\n", buffer );
  16. return 0;
  17. }

Compiled with gcc
Last edited by andor; Jul 6th, 2006 at 6:07 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Jul 6th, 2006
0

Re: Convert string to to HEX

What are you guys taking about? The original request is:
Quote originally posted by sgriffiths ...
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?
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is online now Online
7,738 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Error
Next Thread in C Forum Timeline: Start C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC