Thread: int to char: I need some help

  1. #1
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46

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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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. #3
    old man
    Join Date
    Dec 2005
    Posts
    90
    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. #4
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    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. #5
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote 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. #6
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Quote 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?
    Thanks for your time.
    Mas

  7. #7
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by Afrinux
    If I have understood your explanation, in case of "%F*x", I will get a 'FFFF' padding. Is it right?
    No, I should've said "you could also use blanks" instead of "you could also use, ie, blanks" -- the "ie" implies an arbitrary example, but in fact zero or blank are your only options AFAIK. They're the only two padding values that make sense, really.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM