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
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 ...
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
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 }
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.