Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: psygate on September 09, 2007

Title: Changing Number System (Well... Hex to binary and so an)
Post by: psygate on September 09, 2007
I have a really stupid question. I'd like to convert numbers from the decimal system to others (like hexadecimal, binary usw.) but I encountert a problem and I couldn't fix it.

If I take a whole number the case is clear to me, I can put every whole number like 1,2,3,4.... into another system BUT, and that's what bothers me, I can't "translate" numbers which are not whole, f.e.: 3,1415 or 0,12723. Who can I translate these positions after the decimal point?
Title: Re: Changing Number System (Well... Hex to binary and so an)
Post by: Jim on September 09, 2007
The fractions are just like they are in Hundreds, Tens and Units (base 10).  The first column after the decimal point is 1/10, the next 1/100, the next 1/1000.  In Hex, they're 1/16, 1/256, 1/4096, in binary they're 1/2, 1/4, 1/8.
The easiest way to work out the digits for a few more decimal places is to multiply the fraction by a power of the base.  So in base 10, multiply by 10 to get one extra digit, 100 for two, 1000 for three.  In hex, multiply by 16, 256, 4096, in binary, multiply by 2,4,8.

Jim
Title: Re: Changing Number System (Well... Hex to binary and so an)
Post by: Shockwave on September 09, 2007
And to represent a floating point number in binary you'd have to "fix" the decimal point into position.

For example;
Code: [Select]
+8421 1248-
+0010.1100-

Would represent the number 2.3

I am sure that you could obtain the conversion very cheaply using binary shifts. Of course you sacrifice the size of number you can store according to the amount of decimal places you want.
Title: Re: Changing Number System (Well... Hex to binary and so an)
Post by: psygate on September 09, 2007
Well, the Memory shouldn#t be the problem  :D I could store it as a string, or as a long integer base and a integer exponent, so, if I got that right, the simple formula is:

Code: [Select]

a=number to convert
b=basesystem
c=0 'exponent
output=returnstring
while a<>0
 digit=a*base^c mod basesystem
 c+=1
 output=output+string(digit)
wend
Title: Re: Changing Number System (Well... Hex to binary and so an)
Post by: psygate on September 09, 2007
Now, I found a way, I think it's pretty simple, but I'd like to share my incredibly deep insights into maths with you.

Excuse the shitty code, but it's late.

(in FreeBasic written)

Code: [Select]
cconv(q as double,s as uinteger) 'q = Number you'd like to convert
           's = System to which you'd like to convert
           
dim as integer d,c
dim as double q
dim as string o,u,r(36)
declare function modulo(byval x as double,byval y as double) as double

for c=0 to 9
    r(c)=str(c)
next
for c=asc("a") to asc("z")
    r(c-asc("a")+10)=chr(c)
next

d=int(q)
q-=d

while modulo(q,1)<>0
    q*=s
wend

while q<>0 or d<>0
    if q<>0 then
        p=modulo(q,s)
        o=r(p)+o
        q=int(q/s)
    elseif d<>0 then
        q=modulo(d,s)
        u=r(p)+u
        d=int(d/s)
    end if
wend

print u+"."+o

function modulo(byval x as double,byval y as double) as double
 return x-y*int(x/y)   
end function

sleep

the code needs improvement, so if you find time, please have a look at it and improve it, I don't know how, but it could be that it makes mistakes at some points, I'll have a look at it as soon as I find time too do so.
Title: Re: Changing Number System (Well... Hex to binary and so an)
Post by: Jim on September 09, 2007
Does it work?  I don't think your pseudo-code (2 posts back) will work.  What you need to do is pick how many fractional places you would like in your base, multiply by base^num_places and just convert that as if it were a whole number.
eg.
12.12345
in hex would be
hex$(12)+"."+padzero$(hex$(Int(0.12345*16^2)),2) to two places
hex$(12)+"."+padzero$(hex$(Int(0.12345*16^3)),3) to three places

Where padzero$(s$,places) is a function that pads s$ to the left with '0's so it is 'places' long.

Jim
Title: Re: Changing Number System (Well... Hex to binary and so an)
Post by: psygate on September 10, 2007
According to my program pi in hexadecimal is:
3.243f6a8885a6

according to a homepage about bourwine pouffle algorithm:

hexadezimal (16er-System):
pi = 3,243F6A8885A308D313198A2E03707344A4093822299F31D00...

must be working  :D ;D
Title: Re: Changing Number System (Well... Hex to binary and so an)
Post by: Jim on September 10, 2007
Cool :D
Title: Re: Changing Number System (Well... Hex to binary and so an)
Post by: psygate on September 11, 2007
Ok, I found a little but grave glitch in the system. If the decimals after the point are with leading zero(s), it won't output them. I'll repost the code now with that bug fixed, anyone interested in the mathematical theory behind it?  :D

Code: [Select]
cconv(q as double,s as uinteger) 'q = Number you'd like to convert
           's = System to which you'd like to convert
           
dim as integer d,c
dim as double q
dim as string o,u,r(36),lz
declare function modulo(byval x as double,byval y as double) as double

for c=0 to 9
    r(c)=str(c)
next
for c=asc("a") to asc("z")
    r(c-asc("a")+10)=chr(c)
next

d=int(q)
q-=d

while modulo(q,1)<>0
    q*=s
    lz+="0"
wend

while q<>0 or d<>0
    if q<>0 then
        p=modulo(q,s)
        o=r(p)+o
        q=int(q/s)
    elseif d<>0 then
        q=modulo(d,s)
        u=r(p)+u
        d=int(d/s)
    end if
wend

print u+"."+lz+o

function modulo(byval x as double,byval y as double) as double
 return x-y*int(x/y)   
end function

sleep