Base 10 to base 255

Started by
5 comments, last by alex_myrpg 17 years, 12 months ago
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.
Advertisement
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 +
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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.
Methinks base 256 would be much easier [grin]
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.


Thanks joanusdmentia for the explaination. Thanks Antheus... I think I completely forgot about the zero, 256 it is =P

alex_myrpg: I was just wondering about any integer over 255.
Ok, well glad it's answered anyway.

This topic is closed to new replies.

Advertisement