Thread: Conversion

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    Conversion

    Hello, is there a way in C to convert an int to a char* (a string). I have done the following:

    sprintf(charptr,"%d",intvalue);

    But, when I run the program, an "H" appends to numbers that are below 3 digits. Is there a clean way to convert an int to a char*? thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, try using the function correctly. I don't see your sample code of how it "doesn't work". Post a full compilable sample code that "doesn't work" for us.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    sorry about that. out is the file descriptor and arr is the array ptr being passed in.

    Code:
    void theoutput(int* arr,int out)
    {
    	int x;
    	char convert[50]; //buffer to hold int
    	for(x = 0; x < ARRAY_SIZE; x++)
    	{
                    sprintf (convert, "%d", arr[x]);
    	 	write(out, &convert, sizeof(&convert));
    		write(out, " : ", 3);
    	}
    	write(out,"\n",1);
    }
    the only other changes i made to this code are in the write function:

    write(out, &convert, sizeof(&convert));
    changed to
    write(out, convert, sizeof(&convert));


    the program compiles correctly with both versions, but still with the "H" being appended to numbers that are below 3 digits.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    write(out, &convert, sizeof(&convert));
    Why are you using the size of the address of the array as an argument? If you're trying to get the size of the string, use strlen. Also, an easy way to see if what you've got in the buffer is correct, just printf it real quick. But anyway, it's your incorrect usage of the third parameter of write that's your problem.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    also you don't want to pass the address over your char array to write() either, just pass the variable, it's already a pointer.

    write(out, convert, sizeof(convert)); //or strlen(convert) instead of size if you don't want the null char and garbage after it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM