In class we have been learning to convert decimal to Binary, octal, and now Hexadecimal. I've been working on a program that does all the converting for you, but so far only have decimal->binary, and decimal->octal. I have no idea how to take up decimal->hexadecimal because of the 1-9 and a-f. Take a look at my format an what not so far;


Code:

:Lbl 1
:4->X
:17->Y
:ClrHome
:Input "DECIMAL: ",D
:D->C
:Output(3,1,"Binary
:While D>0
:Y-1->Y
:fPart(D/2)->F
:iPart(D/2)->D
:If F=.5
:Then
:Output(X,Y,"1
:Else
:Output(X,Y,"0
:End:End
:Output(5,1,"OCTAL
:X+2->X
:17->Y
:While C>0
:Y-1->Y
:fPart(C/8)->F
:iPart(C/8)->C
:F(8)->F
:Output(X,Y,F
:End
:Pause
:Goto1


That's what I have so far. I'm still a noob programmer, so I don't think everything is "optimized". Any ideas on how to add hexadecimal? Sad
joshie75 wrote:
In class we have been learning to convert decimal to Binary, octal, and now Hexadecimal. I've been working on a program that does all the converting for you, but so far only have decimal->binary, and decimal->octal. I have no idea how to take up decimal->hexadecimal because of the 1-9 and a-f. Take a look at my format an what not so far;


Code:

:Lbl 1
:4->X
:17->Y
:ClrHome
:Input "DECIMAL: ",D
:D->C
:Output(3,1,"Binary
:While D>0
:Y-1->Y
:fPart(D/2)->F
:iPart(D/2)->D
:If F=.5
:Then
:Output(X,Y,"1
:Else
:Output(X,Y,"0
:End:End
:Output(5,1,"OCTAL
:X+2->X
:17->Y
:While C>0
:Y-1->Y
:fPart(C/8)->F
:iPart(C/8)->C
:F(8)->F
:Output(X,Y,F
:End
:Pause
:Goto1


That's what I have so far. I'm still a noob programmer, so I don't think everything is "optimized". Any ideas on how to add hexadecimal? Sad


For hex, you need to use a string, since in decimal, A-F would be 10-15 iirc, so just use inString() with the string it is searching through as "0123456789ABCDEF" and it will give the decimal value of hex, thought that is if I did remember hex to decimal correct.
See I'm no expert at Hexadecimal, so I asked my teacher;
how would you convert a number such as 47 decimal, into Hex. So we started working it out and it went something like this...

16/47 . . . . Goes in twice, remainder of 15, 15 converts to F. F=LSB
where do you go from there?
Convert base 10 into an n-base like this. I will use 150 as a base 10 number and I will convert it into hexadecimal.

Divide the base 10 number by the highest power of n that is less than the base 10 number. (150/16^1)= 9 R 6
Take the remainder and do the same thing until the remainder becomes 0. (6/16^0) = 6 R 0
Then take the quotients of all the previous division problems and store it into a list. {9,6}
If a number in the list is above 10, convert it into its corresponding letter.

150d=0x96
ooo I like >Smile
Thanks Smile
In other words, to convert from base 10 to base N, repeatedly divide by N and keep the remainder, until you can't anymore. Smile
Thanks Smile
joshie75 wrote:
Thanks Smile
Sure! Also, here's a selection of routines that do change-of-base; just start where I've linked and work your way down the thread:

http://www.cemetech.net/forum/viewtopic.php?p=74906#74906
So I went to store A-F as a List..

Code:

:{A,B,C,D,E,F}->LHEXAD

Am I not allowed to store letters (not variables) to a list? I tried recalling a Letter, but it shot out what that variable equals.
It doesn't understand what letters mean. If you're trying to store hexadecimal digits, store them as 10, 11, 12, 13, 14, and 15, respectively.
With the way I coed it, I had it So It did all the dividing and stored the iparts / fparts and then it would times the fpart by 16, and display that number. If that number was bigger than 9, then i told it to look to the list of letters to display the corresponding letter. here's my code:


Code:

:Lbl 1
:4->X
:17->Y
:ClrHome
:Input "DECIMAL: ",D
:D->C
:Output(3,1,"Binary
:While D>0
:Y-1->Y
:fPart(D/2)->F
:iPart(D/2)->D
:If F=.5
:Then
:Output(X,Y,"1
:Else
:Output(X,Y,"0
:End:End
:Output(5,1,"OCTAL
:X+2->X
:17->Y
:While C>0
:Y-1->Y
:fPart(C/8)->F
:iPart(C/8)->C
:F(8)->F
:Output(X,Y,F
:End
:{A,B,C,D,E,F}->LHEXAD
:Output(7,1,"HEXADECIMAL
:17->Y
:While R>0
:Y-1->Y
:fPart(R/16)->F
:iPart(R/16)->R
:F(16)->F
:If F>9
:Then
:F-9->F
:Output(8,Y,LHEXAD(F
:Else
:Output(8,Y,F
:End:End
:Pause
:Goto1
Change {A,B,C,D,E,F}->LHEXAD to {10,11,12,13,14,15}->LHEXAD
Then how will I get it to display the letter corresponding to the number? ie:10=A, 11=B etc.
Sub("0123456789ABCDEF",Ans+1,1)

That will return a string with the correct hexadecimal digit when the input (Ans) is between 0 and 15.
what are the Ans+1, and the 1 parts for?
Sub(String,Start,Length) - Ans+1 is because indexes start at 1 and not 0. So, Sub("abcdefg",4,1) will return "d". Length is the length of the string to take, so for example: Sub("ABCDEF",4,3) will return "DEF". Make sense? If you want more to read about it, check here.
Thanks Souvik! Joshie, Souvik said exactly what I would have said, so I'd recommend using that. It's probably your easiest solution for storing the hexadecimal digits in a list. And __player's solution is the correct way to turn the hex digits into a string.
Hmm. To use Sub(str,start,length) wouldn't I have to create the string first?
joshie75 wrote:
Hmm. To use Sub(str,start,length) wouldn't I have to create the string first?
As Souvik showed you, you can just use an immediate (ie, "CONTENTS") string rather than a StrN variable. For example, the following loop would turn a list LHEX into a HEX number in a string:


Code:
".
For(X,1,dim(LHEX
Ans+sub("0123456789ABCDEF",LHEX(1)+1,1
End
sub(Ans,2,dim(LHEX
Alright, I'll play around with it in class on tuesday; Thanks for all the help guys Smile
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement