[source]binary/decimal converter
Author |
Message |
iker
|
Posted: Tue Feb 07, 2006 7:04 pm Post subject: [source]binary/decimal converter |
|
|
Here's a little program I made for my computer engineering class, in about 15 minutes. Anyways, hope it helps anyone if they need it
code: |
var firststring, reverse, laststring, choice : string := ' '
var firstint, lastint, count, l : int := 0
var endit : boolean := false
loop
locate (1, 1)
put "[1]Decimal>Binary [2]Binary>Decimal"
get choice
put "Number"
get firststring
firstint := strint (firststring)
loop
if choice = '1' then
laststring := intstr (firstint mod 2) + laststring
firstint := firstint div 2
if endit = true then
exit
end if
if firstint div 2 = 0 then
endit := true
end if
elsif choice = '2' then
l := length (firststring)
for decreasing i : l .. 1
if i < l then
reverse := reverse + firststring (i)
else
reverse := firststring (i)
end if
end for
for decreasing i : l .. 1
if reverse (i) = '1' then
lastint += 2 ** (i - 1)
end if
end for
exit
else
exit
end if
end loop
cls
locate (5, 1)
if choice = '1' then
put "[", choice, "]", firststring, ">", laststring
else
put "[", choice, "]", firststring, ">", lastint
end if
lastint := 0
firstint := 0
laststring := ' '
endit := false
count := 0
end loop
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Andy
|
Posted: Tue Feb 07, 2006 10:30 pm Post subject: (No subject) |
|
|
you're going to hate me for this...
code: |
put strint("10101010",2)
put intstr(170,0,2)
|
|
|
|
|
|
|
Delos
|
Posted: Tue Feb 07, 2006 10:41 pm Post subject: (No subject) |
|
|
Ah, yes. Well, that's not so much the point of this code anyway - more of the challenge of being able to convert decimal to binary!
Ok, well I'm biased since I did a similar thing and had someone point out the base conversion thing to me too . Ah, good times.
Either way, good stuff iker on converting decimal to binary. |
|
|
|
|
|
Andy
|
Posted: Tue Feb 07, 2006 10:50 pm Post subject: (No subject) |
|
|
haha i noe, you shouldnt use a function without knowing how to write it yourself... i worte the same program for comp eng class in high school.. except i was told that i could've used strint and intstr without losing anymarks by the teacher after i finished my program.. |
|
|
|
|
|
iker
|
Posted: Tue Feb 07, 2006 10:58 pm Post subject: (No subject) |
|
|
Andy wrote: you're going to hate me for this...
code: |
put strint("10101010",2)
put intstr(170,0,2)
|
not much of a "hate", more of a ? I always have known about there being a base parameter for the strint and intstr and what not, but never understood it. I guess you can learn something new everyday in turing. |
|
|
|
|
|
Delos
|
Posted: Tue Feb 07, 2006 11:44 pm Post subject: (No subject) |
|
|
iker wrote: not much of a "hate", more of a ? I always have known about there being a base parameter for the strint and intstr and what not, but never understood it. I guess you can learn something new everyday in turing.
I guess you'll be busy for 558 days then ! |
|
|
|
|
|
|
|