Sign in to follow this  
majak

decimal to binary and back

Recommended Posts

majak    122
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 this post


Link to post
Share on other sites
crudbreeder    224
using C


// to binary
char a=2, b=253, c=0;
int bin = a;
bin = (bin << 8) | b;
bin = (bin << 8) | c;

// back
char a2 = (bin & 0x00FF0000) >> 16;
char b2 = (bin & 0x0000FF00) >> 8;
char c2 = (bin & 0x000000FF)


Share this post


Link to post
Share on other sites
majak    122
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 this post


Link to post
Share on other sites
Thaligar    142
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 this post


Link to post
Share on other sites
joanusdmentia    1060
Quote:
Original post by majak
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?


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 this post


Link to post
Share on other sites
crudbreeder    224
Quote:
Original post by majak
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?


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 this post


Link to post
Share on other sites
joanusdmentia    1060
Quote:
Original post by crudbreeder
I 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 this post


Link to post
Share on other sites
majak    122
Quote:
Original post by Thaligar
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


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 this post


Link to post
Share on other sites
Kuladus    380
Quote:
Original post by majak
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.


Just make sure you understand it before your teacher/lecturer/tutor asks you about it ...

Share this post


Link to post
Share on other sites
joanusdmentia    1060
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 this post


Link to post
Share on other sites
iMalc    2466
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

Share this post


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

Sign in to follow this