[C++] How to convert an int to a char?
Go to solution
Solved by wolfsinner,
Ok, I didn't even look at your conversion code, my bad. Your conversion method is wrong. You're not dividing a by 2 effectively, your cycle will only run once.
Something like this should be correct (untested):
int bits = 0;for(int tmp = a; tmp > 0 ;tmp>>=1, bits++);while (bits){ x[--bits] = (char)(a % 2 + 48); a /=2;}
This is valid for positive integer values.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now