Integer to String

This is a discussion on Integer to String within the C Programming forums, part of the General Programming Boards category; Hello, I've been wrecking my brain for hours now on how to store a value stored inside an integer variable ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    10

    Integer to String

    Hello,

    I've been wrecking my brain for hours now on how to store a value stored inside an integer variable to a character string or pointer variable.

    I'm using the WriteConsoleOutputCharacter function to print out some text in colour, but I'm having trouble printing out numbers. The second arg of that function requires to be a string.

    For example, if I had an
    int num = 5;

    and I wanted
    char *buffer;
    or
    char buffer[10];
    to have the value of "5" or num, how would I go about doing that?

    Currently if I try to use my integer variable for the function, I get an error: warning: passing arg 2 of 'WriteConsoleOutputCharacter' makes pointer from integer without cast.

    Any ideas? Thanks for your help in advance..

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    _itoa() changes an int to string.
    _ltoa() for longs.

  3. #3
    Unregistered
    Guest
    Wonder if Typecasting really helps here.

    like char_var=(char)int_value;

    ??

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Your can use the sprintf function:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    	char buf[1024];
    	int i = 24;
    
    	sprintf(buf, "The number is: %d\n", i);
    	printf(buf);
    	return 1;
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    Wonder if Typecasting really helps here.
    like char_var=(char)int_value;
    ??
    In a word... no. The int value, eg 65, would be assigned directly to the char variable, and when printed using type %c would represent the character of the ASCII value (or whatever you OS uses). In this case the letter A would be printed.

    What would happen to the number if the value of the int was >255 (max unsigned char value)? It would overflow, and the value would be lost.

    Also, a char is not a string, it's only a single character.

    Code:
    #include <stdio.h>
    /* Example of incorrect conversion from int to char */
    int main(void)
    {
    	char c;
    	int i = 65;
    	
    	printf("i is %d\n", i);
    	
    	c = (char)i;
    	
    	printf("c is %c, %d\n", c, c);
    	return (0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Unregistered
    Guest
    Originally posted by jdinger
    _itoa() changes an int to string.
    _ltoa() for longs.
    Thank you very much! It seems to work..

    I've looked up the function for anyone else that's interested:

    Code:
    #include <stdlib.h> 
    
    char *itoa(int value, char *string, int radix);
    It's said that the radix (third arg) value should be 2 - 32 though I'm not sure what value would be most appropriate for which case. If anyone knows, it would be apprciated.

    Thanks again for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  2. Integer to string?
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2006, 06:13 AM
  3. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21