help on a function of INT > CHAR convert!

This is a discussion on help on a function of INT > CHAR convert! within the C++ Programming forums, part of the General Programming Boards category; Hello, Could some one please help me make a INT > Char converter function. string INTtoCHAR(int IntBuffer) { sprintf(StringBuffer,"%d",IntBuffer); CharBuffer ...

  1. #1
    SublicK
    Guest

    help on a function of INT > CHAR convert!

    Hello,

    Could some one please help me make a INT > Char converter function.

    string INTtoCHAR(int IntBuffer)
    {
    sprintf(StringBuffer,"%d",IntBuffer);
    CharBuffer = StringBuffer;

    return(CharBuffer);
    }

    Hmmm..... I dont understand var converting yet


    thx

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    You said you wanted a conversion from int to char but your funtion returned a string. I'm working under the assumption you want a char. This is the long answer to the short quesion:

    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    char * INTtoCHAR(int IntBuffer);
    void IntToCharNoItoa(int IntBuffer, char cBuff[] ) ;
    
    int main() 
    { 
    
    	char cInt[20];
    	strcpy( cInt, INTtoCHAR( 100 ) ); //no better than itoa below
    
    	itoa( 101, cInt, 10 );//you're just using the c runtime library
    
    	IntToCharNoItoa( 461, cInt );// this loads the char array with the integer
    	
    	return 0;
    } // end
    
    char * INTtoCHAR(int IntBuffer) 
    { 
    	char szBigNum[ 12 ] = { 0 };
    
    	return _itoa( IntBuffer, szBigNum, 10 );
    } 
    
    void IntToCharNoItoa(int IntBuffer, char cBuff[] ) 
    {
    
    	for( int i = 0; IntBuffer % 10; i++ ) //until IntBuffer modulas is 0
    	{
    		cBuff[ i ] = '0' + IntBuffer % 10;// insert one decimal digit from IntBuffer
                                                    //plus the ascii '0' to get the correct ascii number value
    		IntBuffer /= 10;//reduce IntBuffer by one decimal digit
    	}
    	cBuff[ i ] = '\0';// terminate the string
    	strrev( cBuff );//its in reverse order so reverse it
                           //english text is left to right numbers right to left
    
    }

  3. #3
    Unregistered
    Guest
    This is an integer:
    1

    This is a char:
    '1'

    This is a string:
    "1"

    This is an integer; it can never be a char; it can be a string
    12345

    This is a string:
    "12345"

    This is an integer; it can be a char if you convert it using ASCII or some other character set; it can be a string
    11

    This is a string:
    "11"

    I don't know what char the integer 11 would be if I used the ASCII conversion set but I could have the compiler show me or by looking it up in a table. All integers between 0 and 128 (or 256 in most compilers) can be used to represent a char if desired. Integers above 128 (256) do not have char equivalents in ASCII but (probably) do in Unicode. If this is the conversion between int and char you are trying to do let us know. Dang already went over several ways to convert the digits in an int into chars stored in a string, if that's what you wanted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

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