Author |
Message |
braeden
|
Posted: Fri Nov 14, 2008 1:01 pm Post subject: Binary to Hexadecimal |
|
|
Here is a code i have to convert a binary number to hexadecimal. The code will not output anything if the there is more than 4 digits. It is part of a larger propgram that is due Monday. I have been working on this for 2 weeks. Help in getting it to work the correct way would be excellent.
Turing: |
var total : string
%Will change binary to hexadecimal
fcn BH (b : int) : string %B is the binary number coming from the program. The function will output a string.
var binarynum : int := b %Binarynum is equal.
var hexa : string := "" %Hexa is the hexadecimal number.
var base16 : string %base16 is outputed.
%Runs through the program until the end of binarynum is reached.
%All of the different 4 digit combinations are stated and what they represent.
loop
if binarynum = 0 then
hexa := hexa + "0"
elsif binarynum = 1 then
hexa := hexa + "1"
elsif binarynum = 10 then
hexa := hexa + "2"
elsif binarynum = 011 then
hexa := hexa + "3"
elsif binarynum = 100 then
hexa := hexa + "4"
elsif binarynum = 101 then
hexa := hexa + "5"
elsif binarynum = 110 then
hexa := hexa + "6"
elsif binarynum = 111 then
hexa := hexa + "7"
elsif binarynum = 1000 then
hexa := hexa + "8"
elsif binarynum = 1001 then
hexa := hexa + "9"
elsif binarynum = 1010 then
hexa := hexa + "A"
elsif binarynum = 1011 then
hexa := hexa + "B"
elsif binarynum = 1100 then
hexa := hexa + "C"
elsif binarynum = 1101 then
hexa := hexa + "D"
elsif binarynum = 1110 then
hexa := hexa + "E"
elsif binarynum = 1111 then
hexa := hexa + "F"
end if
%Base16 replaces hexa
base16 := hexa
%Resets hexa
hexa := ""
end loop
%Outputs the base16 number
result base16
end BH
put "entre a binary number (1's and 0's only)."
get total
put BH (total)
|
Remember to use syntax tags. Thanks. code: | [syntax="Turing"]Code here[/syntax] |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
jbking
|
Posted: Fri Nov 14, 2008 1:20 pm Post subject: Re: Binary to Hexadecimal |
|
|
Are you familiar with div and mod operators? That is my suggestion to fix the problem you have. |
|
|
|
|
![](images/spacer.gif) |
pavol
|
Posted: Fri Nov 14, 2008 1:22 pm Post subject: Re: Binary to Hexadecimal |
|
|
To start, your code doesn't output anything because you have an infinite loop: your main loop does not have any exit condition and so it runs forever and you never reach the end of your function. so you need somewhere in your loop something like
exit when condition
Also, since your code only checks the 16 cases that end with each distinct hex value, you cannot really take in a number like 10010 and give a correct output. Therefore, I don't suggest you add a bunch more elsif conditions to make your program work on a larger scale, but instead try to simplify your program a lot more by coming up with a more mathematical method on paper first and then implement it in your program. |
|
|
|
|
![](images/spacer.gif) |
Chittle
|
Posted: Fri Nov 21, 2008 8:32 pm Post subject: Re: Binary to Hexadecimal |
|
|
Here this is what i came up with
Turing: |
var binary : string %It is helpful for this to be a string because then you can manipulate it easily
get binary
var b : string := ""
%This statement makes it so that the length of the binary is an even division
% of 4 since it is easier this way
if length (binary ) rem 4 not= 0 then
for i : 1 .. 4 - (length (binary ) rem 4)
b + = "0"
end for
binary := b + binary
end if
put binary
loop
%at the beginning of each loop do this check
exit when length(binary ) = 0
%Make all your checks for your binary to hex here
%Make sure you check for binary (1-4) <The first four digits of the binary number>
%Such as
if binary (1.. 4) = "0001" then
hexa + = "1"
end if
%Every time you go through the loop perform this
binary = binary (5 .. length(binary ))
%This is so you get rid of the first four digitsand dont repeat them
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
Chittle
|
Posted: Fri Nov 21, 2008 8:44 pm Post subject: Re: Binary to Hexadecimal |
|
|
Oh sorry I guess this is a little late if it was due on monday... well there it is anyway. |
|
|
|
|
![](images/spacer.gif) |
|