Base 10 to base 255
Converting from base 10 to base 255 should be really easy, all you need to do is assign a char byte a number, and that char byte will basicly be base 255, right? That would only work for numbers less than or equal to 255 though..
How would you do this if you wanted to convert numbers higher than 255 into two char bytes?
I've never done anything like this before, so what I was thinking was "6301 % 255" would give you remainder 181... so the number would be 181 181 (ASCII values)? How would you get it back to base 10 to do math on it?
I'm pretty confused.
To convert a number to base 255 your close, but not right.
digit1 = floor(N/2550) % 255
digit2 = floor(N/2551) % 255
...
digitM = floor(N/255M) % 255
So for 6301, the base 255 number would be 24 181
digit1 = floor(6301/2550) % 255 = 6301 % 255 = 181
digit2 = floor(6301/2551) % 255 = 24 % 255 = 34
As for getting from base 255 to base 10, do it the same way you take the individual digits of a base 10 number to get the complete number
For 1234 in base 10
num = 1*1000 + 2*100 + 3*10 + 4*1
or written in a way that makes it a bit more obvious what's happening
num = 1*103 + 2*102 + 3*101 + 4*100
So for the base 255 number above,
num = 24*2551 + 181*2550 = 6301
Or for base N
num = xM*NM + ... + x2*N2 + x1*N1 + x0*N0 +
digit1 = floor(N/2550) % 255
digit2 = floor(N/2551) % 255
...
digitM = floor(N/255M) % 255
So for 6301, the base 255 number would be 24 181
digit1 = floor(6301/2550) % 255 = 6301 % 255 = 181
digit2 = floor(6301/2551) % 255 = 24 % 255 = 34
As for getting from base 255 to base 10, do it the same way you take the individual digits of a base 10 number to get the complete number
For 1234 in base 10
num = 1*1000 + 2*100 + 3*10 + 4*1
or written in a way that makes it a bit more obvious what's happening
num = 1*103 + 2*102 + 3*101 + 4*100
So for the base 255 number above,
num = 24*2551 + 181*2550 = 6301
Or for base N
num = xM*NM + ... + x2*N2 + x1*N1 + x0*N0 +
Should be fairly simple with a byte array but how how big is your number (8-bit, 16-bit, 32-bit, etc.?) and is it an integer or floating point?
EDIT: just saw joanusdmentia's solution. it should work nicely but it needs a bit of an extension for floating point numbers.
EDIT: just saw joanusdmentia's solution. it should work nicely but it needs a bit of an extension for floating point numbers.
If you look at data as it is stored in memory, using 8 bits per byte, it is already in base 256 (I hope that is what you mean, values from 0 to 255. If not, disregard the following).
Base 10 number:
[0..9][0..9][0..9]...
25661 = [2][5][6][6][1]
Base 256 number:
[0..255][0..255][0..255]...
25661 = 0x643d.
In memory it is stored as [0x64][0x3d].
And that just happens to be your base 256.
You can also easily get base 2, 4, 8, 16, 32, ... just from doing a bit of bit shifting on memory blocks.
Base 10 number:
[0..9][0..9][0..9]...
25661 = [2][5][6][6][1]
Base 256 number:
[0..255][0..255][0..255]...
25661 = 0x643d.
In memory it is stored as [0x64][0x3d].
And that just happens to be your base 256.
You can also easily get base 2, 4, 8, 16, 32, ... just from doing a bit of bit shifting on memory blocks.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement