Jump to content

[C++] How to convert an int to a char?

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

So I have made a program that takes a number in decimal (integers only for now) and converts it to binary. So the thing is that I have tried using char x [somenumber] = (char) b, but the output is something weird like xm؀ . Any suggestions? Btw, here is the code:

#include <iostream>#include <fstream>#include <cmath>#include <cstring>char* reverse(char v[1024]){  int y = strlen(v);  for (int n = 0; n < y/2; n++)    {      v[n] = v[y-n];    }  return v;}int main(){  int a,b = 0,d,counter = 1;  std::cout << "Please input the number that you want converted to binary(in decimal ofc)" << std::endl;  std::cin >> a;char x[1024];  while (b > 2)    {      b = a % 2;      a = b;      x [counter] = (char) b;      counter = counter++;    }std::string binarynumber(x);  std::cout << "The decimal you entered is equal to: " << binarynumber << " in binary :)" << std::endl;  return 0;}

I am not using reverse() yet, so I can see where the problem is.

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Every variable in C is a number. Characters hold the ASCII code of a character. 0 and 1 are not the ASCII code for the characters "0" and "1".

 

If you add 48 to b, you should obtain what you want.

Want to solve problems? Check this out.

Link to comment
Share on other sites

Link to post
Share on other sites

Every variable in C is a number. Characters hold the ASCII code of a character. 0 and 1 are not the ASCII code for the characters "0" and "1".

 

If you add 48 to b, you should obtain what you want.

Even after that, the output is x]Q  when the input is 436347...

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

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.

Want to solve problems? Check this out.

Link to comment
Share on other sites

Link to post
Share on other sites

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.

Thanks that worked :)

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×