# Thread: int to char: I need some help

1. ## int to char: I need some help

Hi! I need some help to complete the following program. The following program works fine.
But I want to change it, so that the length of the character returned would be equal to the size of the variable type.
for example:
unsigned long val; the function will return 0000001a if val=0x1a,
and 0000741a if val=0x741a
unsigned short val=0x1a the function will return 001a
unsigned char val = 0x1a the function will return 1a
Code:
```char *intToChar( int val )
{
char str[8];
char *x;

x = (char*)malloc(strlen(str) + 1);
if(x == NULL) { exit(1); }
itoa(val,str,16);
strncpy(x, str, strlen(str)+1);
return x;
}```
I thought about doing it like this:
Code:
```char *intToChar(int val,int chLgth)//chLgth being the size of the variable type: e.g. for short, chLgth=2
{
char str[8];
char *x;

x = (char*)malloc(strlen(str) + 1);
if(x == NULL) { exit(1); }
itoa(val,str,16);
if( chLgth == 4 ){
}
else if( chLgth == 2 ){
}
strncpy(x, str, strlen(str)+1);
return x;
}```
Thanks for helping.
Afrinux

2. > The following program works fine.
Yeah, but it's far from bug free.

> x = (char*)malloc(strlen(str) + 1);
1. you cast the return result of malloc, see the FAQ for why not.
2. str is uninitialised at this point, so strlen returns what?

Code:
```char *intToChar(int val,int chLgth)
{
str = malloc ( 10 );  /* enough for most things */
sprintf( str, "%0*x", chLgth, val );
return str;```

3. Also itoa() is not a standard C function, so some of us can't help you with it.

The ordinary way to convert numbers with padding is to use sprintf() ... maybe you can get what you want with that ...

4. Code:
```char *intToChar(int val,int chLgth)
{
str = malloc ( 10 );  /* enough for most things */
sprintf( str, "%0*x", chLgth, val );
return str;```
Wow!
Worked like a charm. Thank you Salem.
I have wasted lots of time, trying strncpy() and strncat() with lots of lines of code.
eerok , yes you are right. I just found out that sprintf() is pretty handy.
Now I have to find a good tutorial about sprintf(). Altho I get what I wanted, I still dont undertand the "%0*x" part.
> x = (char*)malloc(strlen(str) + 1);
1. you cast the return result of malloc, see the FAQ for why not.
2. str is uninitialised at this point, so strlen returns what?
Good point! I have also to find out why my code was working.

5. Originally Posted by Afrinux
Altho I get what I wanted, I still dont undertand the "%0*x" part.
The "0" specifies padding with zeros (you could also use, ie, blanks), the "*" specifies that the amount of padding will be supplied by a variable (in this case "chLgth"), and the "x" specifies unsigned (lowercase) hex.

6. Originally Posted by eerok
The "0" specifies padding with zeros (you could also use, ie, blanks), the "*" specifies that the amount of padding will be supplied by a variable (in this case "chLgth"), and the "x" specifies unsigned (lowercase) hex.
I am comfortable with the "%d", "%x", ...
In this case "%0*x", we have the '0*' inserted.
If I have understood your explanation, in case of "%F*x", I will get a 'FFFF' padding. Is it right?