I would like to convert int to char
-
Hi, I would like to convert
int i = 7
tochar
, how can I do this? Regards, - When in doubt, push a pawn! - -
Hi, I would like to convert
int i = 7
tochar
, how can I do this? Regards, - When in doubt, push a pawn! -Hi, can use _itoa(..) function. Have a look @ msdn. it does have a sample too. Sujan
-
You can use on of : sprintf, itoa ... of this pseudo code
int = nex_digit while(int) char = 0x30+int
I would like to run this function in my C++ code;
void convertToBinary( int numberToConvert ) { if(!(( N / 2 ) < 1 )) { binaryRepresentation[counter] = ( N % 2 ); counter++; convertToBinary( N /= 2 ); } }
butbinaryRepresentation[counter] = ( N % 2 );
line of code does not work friend? - When in doubt, push a pawn! - -
I would like to run this function in my C++ code;
void convertToBinary( int numberToConvert ) { if(!(( N / 2 ) < 1 )) { binaryRepresentation[counter] = ( N % 2 ); counter++; convertToBinary( N /= 2 ); } }
butbinaryRepresentation[counter] = ( N % 2 );
line of code does not work friend? - When in doubt, push a pawn! -I don't understend what meen N in this code ;) For binare representation of value i use this function:
char* BinPrintf(int value) { char tmp[256]; memset(tmp, 0, 256); int tpos = 0; while(value) { tmp[tpos++] = 0x30+value%2; value = value >> 1; } char *ret = new char[strlen(tmp)+1]; memset(ret, 0, strlen(tmp)+1); tpos = strlen(tmp)-1; value = 0; while(tpos > -1) ret[value++] = tmp[tpos--]; return ret; }
-
When I made
sscanf( static_cast( N % 2 ) , "%s" , &binaryRepresentation[counter] );
Compiler gives an error like cannot convert parameter 1 from 'char' to 'const char * I'm a newbie in C++, Regards. - When in doubt, push a pawn! - -
kromozom wrote: sscanf( static_cast( N % 2 ) , "%s" , &binaryRepresentation[counter] ); that's normal, you associate a char with %s do this :
sscanf( static_cast( N % 2 ) , "%c" , &binaryRepresentation[counter] );
TOXCCT >>> GEII power
-
When I made
sscanf( static_cast( N % 2 ) , "%s" , &binaryRepresentation[counter] );
Compiler gives an error like cannot convert parameter 1 from 'char' to 'const char * I'm a newbie in C++, Regards. - When in doubt, push a pawn! -That is because
static_cast( N % 2 )
is not a pointer to a char. I don't know what it is, because N is unknown to me... Take a look at the prototype of sscanf[^] and then start wondering how you should pass the parameters... Besides that, the solution that Mad__ gave you looks like what you want... :-D Multiply it by infinity and take it beyond eternity and you'll still have no idea about what I'm talking about. -
I would like to run this function in my C++ code;
void convertToBinary( int numberToConvert ) { if(!(( N / 2 ) < 1 )) { binaryRepresentation[counter] = ( N % 2 ); counter++; convertToBinary( N /= 2 ); } }
butbinaryRepresentation[counter] = ( N % 2 );
line of code does not work friend? - When in doubt, push a pawn! -I assume
binaryRepresentation
is some sort ofchar
array, and thatN
is actuallynumberToConvert
. That being the case, what you have is working, but you are confusing 0 (ASCII 0) and '0' (ASCII 48). You simply need to change the statement to:binaryRepresentation[counter] = (char) ((N % 2 ) + 48);
Now
binaryRepresentation[counter]
will contain'0'
ifN
is an even number and'1'
ifN
is an odd number. Of course, if you wanted to use a "built-in" solution, you could simply useitoa(numberToConvert, binaryRepresentation, 2)
.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
-
I don't understend what meen N in this code ;) For binare representation of value i use this function:
char* BinPrintf(int value) { char tmp[256]; memset(tmp, 0, 256); int tpos = 0; while(value) { tmp[tpos++] = 0x30+value%2; value = value >> 1; } char *ret = new char[strlen(tmp)+1]; memset(ret, 0, strlen(tmp)+1); tpos = strlen(tmp)-1; value = 0; while(tpos > -1) ret[value++] = tmp[tpos--]; return ret; }
Mad__ wrote: char *ret = new char[strlen(tmp)+1]; memset(ret, 0, strlen(tmp)+1); tpos = strlen(tmp)-1; value = 0; while(tpos > -1) ret[value++] = tmp[tpos--]; This code could simply be replaced with a call to
strrev(tmp)
.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
-
I assume
binaryRepresentation
is some sort ofchar
array, and thatN
is actuallynumberToConvert
. That being the case, what you have is working, but you are confusing 0 (ASCII 0) and '0' (ASCII 48). You simply need to change the statement to:binaryRepresentation[counter] = (char) ((N % 2 ) + 48);
Now
binaryRepresentation[counter]
will contain'0'
ifN
is an even number and'1'
ifN
is an odd number. Of course, if you wanted to use a "built-in" solution, you could simply useitoa(numberToConvert, binaryRepresentation, 2)
.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow