Char to Binary

Started by
5 comments, last by Emmanuel Deloget 17 years, 7 months ago
Hey everyone, im trying to write this c++ program that converts a char to it's binary equivalent. It works for some letters, but for letters like 'z', it gives me some crazy output: 01111012046820353 I suspect it has to do with the last few checks it does, but any help would be greatly appreciated! Thanks

#include<iostream>
using namespace std;
int main()
{
	char letter = 0;
	int bits[7] = {0};
	int number = 0;

	cout<<"Input letter to be converted to binary:  ";
	cin>> letter;
	cout<<"\n";

	number = (int)letter;
	cout<<"Your number in decimal is:  "<<number;

	while(number > 0)
	{
		if(number >= 128)
		{
			number = number - 128;
			bits[0] = 1;
		}
		else if(number < 128 && number >= 64)
		{
			number = number - 64;
			bits[1] = 1;
		}
		else if(number < 64 && number >= 32)
		{
			number = number - 32;
			bits[2] = 1;
		}
		else if(number < 32 && number >= 16)
		{
			number = number - 16;
			bits[3] = 1;
		}
		else if(number < 16 && number >= 8)
		{
			number = number - 8;
			bits[4] = 1;
		}
		else if(number < 8 && number >= 4)
		{
			number = number - 4;
			bits[5] = 1;
		}
		else if(number < 4 && number >= 2)
		{
			number = number - 2;
			bits[6] = 1;
		}
		else if(number < 2 && number > 0)
		{
			number = number - 1;
			bits[7] = 1;
		}
		else
		{
			break;
		}
	}

	cout<<"\nYour number in binary is:  "<<bits[0]<<bits[1]<<bits[2]<<bits[3]<<bits[4]<<bits[5]<<bits[6]<<bits[7];

	return 0;
}

Advertisement
Change your array initialiser from

int bits[7] = {0}; to
int bits[7] = {0, 0, 0, 0, 0, 0, 0};

Edit: Oops, better change it to
int bits[8] = {0, 0, 0, 0, 0, 0, 0, 0};

bits[n] only access from 0 to n-1

Reason being that you only allocated enough for 7 elements and bits[7] was trying to access the 8th element of the bits array.

Wow, thanks for the rapid response guys! Works perfectly! Thanks again...
I would say the only necessary edit was changing the 7 to an 8, as using {0} initializes the entire array to zero.

Although, using {0} in place of a list of zeros is just a habit I picked up from others. I've never heard any concrete info about this "trick". Does anyone know of side-effects to initializing arrays or structures using {0}?

D
Well, global variables of integral or pointer types, including arrays, are implicitly initialised to zero anyway if no other initialiser is provided.

Technically, int a[10]={ 0 }; is equivalent to int a[10]; if global.

For example, the following:

#include <iostream>int a[10]={ 11 };int main(){    for(int i=0;i<10;++i) std::cout << a << std::endl;}


just produced the following output with the compiler I have to hand:

11
0
0
0
0
0
0
0
0
0

There may be compilers that would initialise that whole array to 11 but I don't think that is part of the standard. Be interested to know if it is since it would mean the Digital Mars compiler is broken.

Paul
I somewhat remember reading that any unspecified values in the array would be set with the default initializer, so int bits[8] = {} is all you need to fill it with 0s.

Edit: After testing it, it seems you need at least one value specified, which explains why everyone used int bits[8] = {0} :)
Quote:
Well, global variables of integral or pointer types, including arrays, are
implicitly initialised to zero anyway if no other initialiser is provided.

To enforce this point, here are the quotes from the C++ Standard:
Quote:3.6.2 Initialization of non-local objects
The storage for objects with static storage duration shall be zero-initialized before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization. Objects of POD types with static storage duration initialized with constant expressions shall be initialized before any dynamic initialization takes place. Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit.
.
There's a few other quirks about the initialization order of namespace scope variables. Note that the standard used the term "zero-initialize", which can be quite different from "default-initialize".

Quote:Although, using {0} in place of a list of zeros is just a habit I picked up from others. I've never heard any concrete info about this "trick". Does anyone know of side-effects to initializing arrays or structures using {0}?

No. Here is the corresponding quote from the standard:
Quote:8.5.1 §7
If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitely initialized shall be default-initialized.

We have to resort to the definition of what a default-initialization is:
Quote:8.5 §5
To default-initialize an object of type T means:
- if T is a non-POD class type, the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor)
- if T is an array type, each element is default-initialized
- otherwise, the storage for the object is zero-initialized
.
Since the subsequent members of your array are neither non-POD objects nor arrays, they will be zero-initialized.

Hope it is clearer now :)

This topic is closed to new replies.

Advertisement