Convert string to to HEX

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Convert string to to HEX

 
0
  #1
Jul 5th, 2006
Hello

how would i go about converting a string value to HEX

ie

char name[10]="STEPHEN";

Convert name to HEX
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Convert string to to HEX

 
0
  #2
Jul 5th, 2006
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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Re: Convert string to to HEX

 
0
  #3
Jul 5th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Convert string to to HEX

 
0
  #4
Jul 5th, 2006
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. }
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Re: Convert string to to HEX

 
0
  #5
Jul 5th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Convert string to to HEX

 
0
  #6
Jul 5th, 2006
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?
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Re: Convert string to to HEX

 
0
  #7
Jul 5th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Convert string to to HEX

 
0
  #8
Jul 5th, 2006
Originally Posted by sgriffiths
I cant see no given web page,
I mean this web page.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Convert string to to HEX

 
0
  #9
Jul 6th, 2006
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Convert string to to HEX

 
0
  #10
Jul 6th, 2006
What are you guys taking about? The original request is:
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 36612 | Replies: 22
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC