yuy2002
asked on
how to convert an 128 bit binary number to decimal number
What I want, how to convert below 128 bit number to deciaml number
"0xFF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF" to "255^16+255^15+....+255^2+ 255".
I plan to write a new class to save this so large number, like this
class NUM128
{
public: ...
private:
unsigned char numbinary[16];
unsigned char numdecimal[6];
}
I use numbinary to save the binary number and numdecimal save the decimal number.
for(int i=0;i<16;i++)
numbinary[i]= 0xFF;
How could I get the numdecimal according to the numbinary?
Anyone has any idea will be welcome and appreciated.
"0xFF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF" to "255^16+255^15+....+255^2+
I plan to write a new class to save this so large number, like this
class NUM128
{
public: ...
private:
unsigned char numbinary[16];
unsigned char numdecimal[6];
}
I use numbinary to save the binary number and numdecimal save the decimal number.
for(int i=0;i<16;i++)
numbinary[i]= 0xFF;
How could I get the numdecimal according to the numbinary?
Anyone has any idea will be welcome and appreciated.
ASKER
I want to get the decimal result of 0xFF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF.
ASKER
Anyone could compute the "0xFF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"'s decimal result and save in a char array?
ASKER
0xFF ---->> 255
8bits char[3]
0xFFFF ---->>65535
16bits char[5]
0xFFFFFFFF---->>4294967295
32bits char[10]
0xFFFFFFFFFFFFFFFF---->>18 4467440737 09551615
64bits char[20]
.......................... .......... .......... .......... .......... .......... >>above could be computed by compiler , 32bits we could use unsigned int, 64bits we could use unsigned long
0xFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFF-- -->>184467 4407370955 1616^2-1
128bits maybe char[40]
In this case, how should I do?
8bits char[3]
0xFFFF ---->>65535
16bits char[5]
0xFFFFFFFF---->>4294967295
32bits char[10]
0xFFFFFFFFFFFFFFFF---->>18
64bits char[20]
..........................
0xFFFFFFFFFFFFFFFFFFFFFFFF
128bits maybe char[40]
In this case, how should I do?
That clarification helps. A few more questions:
Do you want this answer just for the number 0xFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFF you have specified, or do you want a more general solution?
If you just want the answer for this number, is it important to have a program that calculates it, or do you just want to know what the number is?
If you want a program, either for just this number or in general, does the program have to be limited to the basic facilities of C++, or can you use an arbitrary precision integer arithmetic library that somebody else has already created?
Do you want this answer just for the number 0xFFFFFFFFFFFFFFFFFFFFFFFF
If you just want the answer for this number, is it important to have a program that calculates it, or do you just want to know what the number is?
If you want a program, either for just this number or in general, does the program have to be limited to the basic facilities of C++, or can you use an arbitrary precision integer arithmetic library that somebody else has already created?
ASKER
>>If you want a program
yes, I want a programme that implement the 128-bit number operations.
I could implement the operations between two 128-bit numbers, but I couldn't get the decimal output according to the 128-bit numbers.
What I want is to convert the 128-bit binary number to a decimal number.
yes, I want a programme that implement the 128-bit number operations.
I could implement the operations between two 128-bit numbers, but I couldn't get the decimal output according to the 128-bit numbers.
What I want is to convert the 128-bit binary number to a decimal number.
To convert binary to decimal you need at the very least a routine to subtract 128-bit numbers.
Then apply this algorithm:
( build a table of powers of ten, 128-bits wide, up to whatever power of ten is just below 2^128 )
subtract the biggest number of the table from X.
repeat that until the number goes negative.
The number of times you can do that is your first digit.
move to the next smallest power of ten
repeat all that until you're down to the 10^0 entry.
example for 16 bits:
table: 10000
1000
100
10
1
X = 12345
subtract 10000 from X until it goes negative.
times you can do this: 1
X is now 2345
subtract 1000 from X until it goes negative
times you can do this: 2
... and so on.
Then apply this algorithm:
( build a table of powers of ten, 128-bits wide, up to whatever power of ten is just below 2^128 )
subtract the biggest number of the table from X.
repeat that until the number goes negative.
The number of times you can do that is your first digit.
move to the next smallest power of ten
repeat all that until you're down to the 10^0 entry.
example for 16 bits:
table: 10000
1000
100
10
1
X = 12345
subtract 10000 from X until it goes negative.
times you can do this: 1
X is now 2345
subtract 1000 from X until it goes negative
times you can do this: 2
... and so on.
ASKER
1. how do u know the biggest number's power of the table through programming?
2. how do u implement the subtraction, use binary number to implement, then you face how to convert the ten power to binary number?
2. how do u implement the subtraction, use binary number to implement, then you face how to convert the ten power to binary number?
I'd suggest you look at one or more of the libraries listed on this page:
http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic
http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>Just keep a table of powers of ten
I also should have a table of power of 2 , and implement their conversions.
I've got the idea, thank you.
I also should have a table of power of 2 , and implement their conversions.
I've got the idea, thank you.
ASKER
Thanks to all.
"255^16+255^15+....+255^2+
These two character strings do not represent the same number. An expression equivalent to the first expression would be something like "(255*(256^15))+(255*(256^
The only variable parts of this expression are the "255"s. You would need to plug in the character representation of each byte of the numbinary string.
The line
numbinary[i]= 0xFF;
indicates that you intend to store data in numbinary in binary form. To learn how to convert a binary number to character representation, search this site. This question must have been answered many times already.