
Decimals To Binary
I am having quite a bit of truble finding a tutorial on the net about how to convert a ASCII deciaml into binary format for encryption purposes. I have found plenty about converting it to the actual decimals but i want to take those deciamls and turn them into binary. Can anyone out there help me or point me into the direction to some help. Thanx
Kas2002

The easiest way to do it is to break it down into steps. Start by converting the decimal value to its hexadecimal equivalent, then convert the hex value to binary. The coding for these individual steps is not too challenging, just think about it.

Taken from www.cpphome.com
Code:
#include <iostream.h>
int a,b;
int n=0;
int arr[100];
void main()
{
cout <<"Decimal : ";
cin >>a;
while (a>0)
{
n++;
b=a/2;
if (a%2==0)
{
arr[n]=0;
}
else
{
arr[n]=a(2*b);
}
cout <<"b("<<n<<") = "<< b<<" ";
a=b;
cout <<"arr["<<n<<"] = "<< arr[n]<<endl;
}
cout<<"Binary : ";
for (n=n;n>0;n)
{
cout <<arr[n];
}
}

if you are looking to simply output them, then give this a go:
Code:
#include <iostream>
using namespace std;
void OutputInBinary(int item)
{
for(int i = 0; i < sizeof(int) * 8; i++)
{
cout << (((item << i) & (0x1 << (sizeof(int) * 8  1))) ? 1 : 0);
}
}
int main(void)
{
OutputInBinary(1023);
cout << endl;
OutputInBinary(12345);
cout << endl;
OutputInBinary(4832);
cout << endl;
return(1);
}
if you're just going to be using it for encryption purposes then you dont need to 'convert' to binary, coz it's already stored in binary form.
if the above sample doesn't solve your problem, can you give more information?
cheers
U.

Most binary logic is done in unsigned chars. And it can almost always be safely assumed that an unsigned char is 8 bits.
This often makes it eaisiar to work with ASCII characters in binary. But remember this, if you are doing this for encryption purposes, then if you perform bitwise manipulations on a character such than every character corresponds to one and only one other possible value of an unsigned char, then all you are doing is making a cryptogram.

This Is What I want to
I found a 64 bit encryption tutorial at cpphome.com and in the tutorial it calls for you to change the txt into decimals and then cahnge the decimals into binary so u can do the next step. They didn't say how to do this because they said there is a tutorial on cpphome.com that says how to do this and I have been looking and never found one. I haven't been able to contiue witht he tutorial because I have no idea how to change the decimal to binary and store it into an array for modification? If someone could just tell me the formula or process u got to go through in English Icould prolly code it my self it would be much better that way then I would understand my algorithm.

my stuff
when i'm dealing with ciphering, i use some functions i made.
/*
theINT is the integer of a number system that you are converting from.
theBASE is the base integer of the number system that you are converting to.
MaxExp defines the explicit maximum value of a possible divisor.
Example function calling:
Convert 2277 to decimal using the functions in the hmain.h header
file.
int RetVal = getRange(2227, 2, 32);
int *bMEM;
bMEM = new int[RetVal];
bMEM = n2bin(2227, RetVal, pow(2, RetVal));
Use a loop to display the array bMEM.
*/
int getRange(int theINT, int theBASE, int MaxExp)
{
for(int r = 0; r <= MaxExp; r++)
{
if(pow(theBASE, r) > theINT)
{
return r;
}
}
return 1;
}
int *n2bin(int theINT, int range)
{
int y = 0, bdex = 0, *bbank;
bbank = new int[range];
long d = (long)pow(2, range);
y = theINT;
while(d > 1)
{
d /= 2;
if((y / d) == 1)
{
y %= d;
bbank[bdex++] = 1;
} else {
bbank[bdex++] = 0;
}
}
return bbank;
}
int bin2n(int bin[])
{
int d, z = 0, x;
for(int n = 0; n <= 32; n++)
{
if((bin[n] > 1  bin[n] < 0))
{
x = (n  1);
d = (int)pow(2, n);
break;
}
}
for(int r = 0; r <= x; r++)
{
d /= 2;
z += (bin[r] * d);
}
return z;
}

umm...kas2002, please look at my post above. ;)

Yes but
I to be able to store the binary for later manipulation instead of just couting it one at a time. Plus it would be nice to know the process I would have to go through instead of someone just giving it to me????