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
I thought about doing it like this: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; }
Thanks for helping.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 ){ // what to add here? } else if( chLgth == 2 ){ // what to add here? } strncpy(x, str, strlen(str)+1); return x; }
Afrinux



LinkBack URL
About LinkBacks


