Hello

how would i go about converting a string value to HEX

ie

char name[10]="STEPHEN";

Convert name 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.

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

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;
}

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

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?

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

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/techtalkforums/showthread.php?t=11049&highlight=itoa
or use this code

#include <stdio.h>
#include <string.h>
int main()
{
	char name[10]="STEPHEN";
	char buffer[21]="";
	char *pbuffer = buffer;
	int len = strlen( name );
	int i;
	for(i = 0; i < len ;i++ )
	{
		sprintf(pbuffer, "%x", name[i]);
		pbuffer +=2;
	}
	printf( "%s\n", buffer );
    return 0;
}

Compiled with gcc

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?

How would i be able to let the user enter HEX and convert it into string?

ie

User Enters 4c3258
and displays L2X

How would i be able to let the user enter HEX and convert it into string?

ie

User Enters 4c3258
and displays L2X

#include <stdio.h>

int main()
{
   int hex;
   
   printf("Enter hex: ");
   
   scanf("%x", &hex);
   
   printf("%c", (char) ((hex & 0xFF000000) >> 24));
   printf("%c", (char) ((hex & 0xFF0000) >> 16));
   printf("%c", (char) ((hex & 0xFF00) >> 8));
   printf("%c", (char) (hex & 0xFF));
   return 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

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
*/

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.

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

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);

Okay, i have now NULL termainted my converted_string, but this still does not work, any help?

[LEFT]int Convert_Update_From_Hex(char *update_record, char *field_name )
{
    int i, k=0;
    char *pconvert=update_record;
    char converted_string[20];[/LEFT]
 
[LEFT]    while(sscanf(pconvert, "%2x", &i ) == 1 )
    { 
        converted_string[k]=i;
        k++;
        pconvert +=2;
    }
    converted_string[k]=NULL;[/LEFT]
 
[LEFT]    strcpy(update_record,converted_string);[/LEFT]

An additional question

If i want to convert hex 4c4c

Do i pass the complete string of 4c4c or do i pass 2 characters at a time, ie 4c, let the conversion take place to L, then pass the next 4c and let the conversion take place?

Okay, i have now NULL termainted my converted_string, but this still does not work, any help?

Well, it does work, even though you did this

converted_string[k]=NULL;

instead of this

converted_string[k]='\0'; /* null terminate */

So I guess you'd have to provide more context.

Do i pass the complete string of 4c4c or do i pass 2 characters at a time, ie 4c, let the conversion take place to L, then pass the next 4c and let the conversion take place?

The function will handle a larger string, but if you want to make more work for yourself you could pass only 2 characters at a time.

This article has been dead for over six months. Start a new discussion instead.