C++ - Decimal to Binary Converting

Convert integer to binary in C#

Your example has an integer expressed as a string. Let's say your integer was actually an integer, and you want to take the integer and convert it to a binary string.

int value = 8;
string binary = Convert.ToString(value, 2);

Which returns 1000.

Decimal to Binary Converting Years in C++

With int i;, i *= 10 quickly reaches the max limit for 32-bit integer 0x7fff'ffff. So i will also need to be 64-bit, it can be unsigned so the ceiling is a bit higher at 0xffff'ffff'ffff'ffff. Example

unsigned long long i = 1;
unsigned long long bin = 0;
int year = 2001;
while (year > 0)
{
int temp = year % 2;
year /= 2;
bin += temp * i;
i *= 10;
printf("check i: %llu\n", i);
}
printf("%016llu\n\n", bin);

To print larger numbers use a character buffer to save temp in each iteration.

program to convert decimal to binary is not working for large outputs

You're storing a decimal number which is the binary representation of n reinterpreted as decimal.

If n>2047, ans will overflow a std::int32_t; if n>524287, ans will overflow a std::int64_t (the biggest signed 64-bit number is 9223372036854775807; unsigned would allow one more bit in ans).

The proper thing to return is a string. Try this:

std::string decimal_to_binary(int n)
{
int x=1;
std::string ans;
while (x<=n){
x*=2;
}
x/=2;
while(x>0)
{
int lastdigit=n/x;
n-=lastdigit*x;
x/=2;
ans=ans+(char)('0'+lastdigit);
}
return ans;
}

Converting Decimal to Binary with long integer values C

The fundamental problem is that you're trying to use an int to store your binary representation. An int is just that, an integer. In memory it's represented in binary, but the C syntax allows you to use base 10 and base 16 literals when assigning or doing other operations on ints. This might make it seem like it's not being treated as a binary number, but it really is. In your program you are confusing the base 2 representation and the base 10 representation. 891717742 (base 10) converted to binary is 110101001001101000100001101110. What your algorithm is essentially trying to do is store the base 10 number 110101001001101000100001101110 in an int. That base 10 number is larger than even a 64 bit number, it would actually would take 97 bits to store.

Check it this answer to see how you can print out the binary representation of an int in C: Is there a printf converter to print in binary format?

How can I convert a decimal number into binary, saving binary digits in array?

I can't test it right now, but this could help:

// Here there should be the necessary include to enable printing

unsigned int size = 10; // The size of the array that should store the bin output
int a = 3; // The int to be converted to bin

// A function that returns a pointer to a byte, that will be interpreted as an array
unsigned short int
*IntToBin (int input)
{
static unsigned short int output[size]; // Declaring the array to be returned by
// the function, with keyword 'static' to
// allow it to be referenced from outside
for (int i = 0; i < size; i++) {
output[i] = (input >> (size - 1 - i)) & 0x1; // Populating output array by
// bit-shifting the input int
// and doing a bitwise 'and' to
// keep only least significant
// digit
}
return output; // Returning the output array (an array is essentially a pointer,
// so this matches the function declaration)
}

// The main function, that will take care of calling the conversion function and
// printing its output
void
main ()
{
unsigned short int result[] = IntToBin (a); // Assigns the output of the conversion
// function to an array
// Prints the input and output of the conversion function
print("a = ");
print(a);
print("\na in bin = ");
for (int i = 0; i < size; i++) {
print (result[i]);
}
print("\n");
}

But, as mentioned in the comments, the behaviour of a signed int is different than that of an unsigned int. And a length of 10 to your output array is a bit strange.

https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm

Decimal to Binary on C library

here:

void dec2bin(int c)
{
int i = 0;
for(i = 31; i >= 0; i--){
if((c & (1 << i)) != 0){
printf("1");
}else{
printf("0");
}
}
}

But this only prints the value of an integer in binary format. All data is represented in binary format internally anyway.



Related Topics



Leave a reply



Submit