# decimal to binary and back

## Recommended Posts

Hi I have three numbers eg. 2, 253 and 0 (one byte each) I want to convert these to binary numbers and put them together (24 bytes) and convert back. eks. 2 = 10, 253 = 11111101 and 0 = 0 together = 101111110100000000 and back = 195840 Anyone who knows a good way? The result must be an int.

##### Share on other sites
using C

// to binarychar a=2, b=253, c=0;int bin = a;bin = (bin << 8) | b;bin = (bin << 8) | c; // backchar a2 = (bin & 0x00FF0000) >> 16;char b2 = (bin & 0x0000FF00) >>  8;char c2 = (bin & 0x000000FF)

##### Share on other sites
I'm not sure I undestand, maybe I didn't explained it well.

got three ints (but none of them get larger than one byte), (2, 253 and 0)
converting them to binary numbers, (10, 1111101 and 0)
concatenate them, (10+1111101+00000000 = 101111110100000000)
convert back to decimal int, (1011111010000000 = 195840)

How do I convert from (decimal) int to binary, concatenate them and convert back to (decimal)int?

##### Share on other sites
Hi Majak,

try this:

int a = 2;
int b = 253;
int c = 0;

int sum = (a << 16) + (b << 8) + c;

so you get int of the concatenated binaries

cheers,
tgar

##### Share on other sites
What Thaligar said, but (if I undestrand correctly) instead of shifting 16 and 8 bits, you'll have to determine how many bits are needed (e.g. 2 = 10 so only 2 bit shift) and shift by that.

##### Share on other sites
Quote:
 Original post by majak10+1111101+00000000

Why 00000000 instead of 0 ?

##### Share on other sites
So if it's really important to check for the number of bits you probably have to write something like

inline int HighestBit( int nData )
{
// find here the highest set bit
// if none is set return 7
};

then you can write,

int a = 2;
int b = 253;
int c = 0;

int sum = (a << ( HighestBit(b) + 1 )) + (b << ( HighestBit(c) + 1)) + c;

greets
tgar

EDIT: HighestBit should return a number between 0 and 7

##### Share on other sites
Quote:
 Original post by majakI'm not sure I undestand, maybe I didn't explained it well.got three ints (but none of them get larger than one byte), (2, 253 and 0)converting them to binary numbers, (10, 1111101 and 0)concatenate them, (10+1111101+00000000 = 101111110100000000)convert back to decimal int, (1011111010000000 = 195840)How do I convert from (decimal) int to binary, concatenate them and convert back to (decimal)int?

Assuming that you don't know what the original numbers were when you're converting back, you can't. Reason being that you have no way of knowing how many bits make up each number. The simplest solution to this is to fix the numbers to a known length, which in your case would be 8 bits. So in otherwords, the first post sounds like exactly what you want.

Oh wait...just went back and reread, you want the numbers output as a *string* of the resulting binary number...

In that case...
std::string convertToBinaryString(unsigned char a, unsigned char b, unsigned char c){    std::string str(24,'0');    unsigned int val = (a<<16) + (b<<8) + c;    for(int i=0 ; i<24 ; ++i, val>>=1) str[23-i] += (val&1);    return str;}bool convertToNumbers(const std::string& str, unsigned char* a, unsigned char* b, unsigned char* c){    if(str.size()!=24) return false;    int value = 0;    for(int i=0 ; i<24 ; ++i)    {        unsigned char digit = str[i]-'0';        if(digit&0xFE) return false;        value = (value<<1) + digit;    }    *a = (value&0x00FF0000) >> 16;    *b = (value&0x0000FF00) >> 8;    *c = (value&0x000000FF);    return true;}void test(){    unsigned char a=2;    unsigned char b=253;    unsigend char c=0;    std::string str = convertToBinaryString(a, b, c);    std::cout << "Binary string = " << str << std::endl;    unsigned char d,e,f;    bool ok = convertToNumbers(str, &d, &e, &f);    if(!ok) std::cout << "Conversion back to numbers failed!" << std::endl;    else std::cout << "Numbers = " << d << "," << e << "," << f << std::endl;}

##### Share on other sites
I think by convert back to decimal he means converting the new bit string into an int. Clarification?

##### Share on other sites
Quote:
 Original post by majakI'm not sure I undestand, maybe I didn't explained it well.got three ints (but none of them get larger than one byte), (2, 253 and 0)converting them to binary numbers, (10, 1111101 and 0)concatenate them, (10+1111101+00000000 = 101111110100000000)convert back to decimal int, (1011111010000000 = 195840)How do I convert from (decimal) int to binary, concatenate them and convert back to (decimal)int?

I assumed that you had three byte-sized integers that you wanted to pack into a 24-bit integer. Maybe I misunderstood you?

Are the numbers in the form of int variables or are they strings representing numbers? Do you want the resulting concatenated binary number as a string or as an int?

##### Share on other sites
and what's also interresting,

can the leading zeros be neglected

and should 0 really be 00000000 ??

cheers,
tgar

##### Share on other sites
Quote:
 Original post by crudbreederI assumed that you had three byte-sized integers that you wanted to pack into a 24-bit integer. Maybe I misunderstood you?

That's what I though too, until I saw this in the OP:
Quote:
 ...together (24 bytes) and...

Quote:
 Original post by crudbreederAre the numbers in the form of int variables or are they strings representing numbers? Do you want the resulting concatenated binary number as a string or as an int?

Well, he also says the original numbers are 1 byte each, so I've just assumed they're unsigned chars.

##### Share on other sites
Quote:
 Original post by ThaligarHi Majak,try this:int a = 2;int b = 253;int c = 0;int sum = (a << 16) + (b << 8) + c;so you get int of the concatenated binariescheers,tgar

I'm sorry I haven't been able to get to the computer sooner (live in another timezone..) But this is exactly what I was looking for. So simple.. sorry for confusing you all.

Quote:
 Why 00000000 instead of 0 ?

leading zeroes doesn't matter in binary, but I need all eight bits in the other end otherwise I will get a wrong number. 101111110100000000 = 195840, but if you remove the last zeroes it will be 765.

Quote:
 That's what I though too, until I saw this in the OP:Quote:...together (24 bytes) and...

My mistake, ofcause 24 bit..

##### Share on other sites
Quote:
 Original post by majakI'm sorry I haven't been able to get to the computer sooner (live in another timezone..) But this is exactly what I was looking for. So simple.. sorry for confusing you all.

##### Share on other sites
Quote:
Original post by majak
Quote:
 That's what I though too, until I saw this in the OP:Quote:...together (24 bytes) and...

My mistake, ofcause 24 bit..

And I went to all that trouble too!! [evil]

... j/k [grin]

##### Share on other sites
Thaligar probably had it right the first time. Probably all that is wanted is simply:
int concatBytes(unsigned char a, unsigned char b, unsigned char c){    return (((int(a) << 8) + int(b)) << 8) + int(c);}int x = concatBytes(2, 253, 0); //x equals 195840

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627741
• Total Posts
2978887

• 10
• 10
• 21
• 14
• 14