decimal to binary and back
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.
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)
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?
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?
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
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
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.
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
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
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-'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;}
I think by convert back to decimal he means converting the new bit string into an int. Clarification?
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement