About Monkey 2 › Forums › Monkey 2 Programming Help › Unexpected Output Decimal to Binary/Hex
This topic contains 8 replies, has 3 voices, and was last updated by
scurty 2 years, 6 months ago.
-
AuthorPosts
-
September 30, 2016 at 3:25 pm #4173
So I’m trying to convert a Decimal value to Binary and Hex and I guess I don’t have the correct algorithm setup. Or maybe something else is up. This code translated into GML(Game Maker Language) seems to work as expected.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657Namespace ApplicationImport "<std>"Using std..Function DecimalToHex:String(integer:Int)Local dic:String = "0123456789ABCDEF"Local hex:String = ""Local dec:Int = integerIf(Not dec) Then hex = "00"While(dec)Local bits := dec & $ffLocal hi := dic[bits / 16 + 1]Local lo := dic[bits Mod 16 + 1]hex = hi + lo + hexdec = dec Shr 8WendReturn hexEndFunction DecimalToBin:String(integer:Int)Local dec := integerLocal dic:String = "01"Local bin:String = ""If(Not dec) Then bin = "0"While(dec)bin = dic[(dec & 1) + 1] + bindec = dec Shr 1WendReturn binEndFunction Main:Int()Print(DecimalToHex(1))Print(DecimalToBin(1))Return 0EndIt outputs 99 for the Hex Function and 45 for the Bin Function. Any ideas?
September 30, 2016 at 5:02 pm #4176[/crayon]Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243[crayon-5cba8b0f36c99589456733 inline="true" ]port "<std>"Using std..Function DecimalToHex:String(dec:Int)If(Not dec) Then Return "00"Local dic:String = "0123456789ABCDEF"Local hex:String = ""While(dec)Local bits := dec & $fLocal hi := String.FromChar(dic[bits])hex = hi + hexdec = dec Shr 4WendReturn hexEndFunction DecimalToBin:String(dec:Int)If(Not dec) Then Return "0"Local bin:String = ""While(dec)bin = String(dec & 1) + bindec = dec Shr 1WendReturn binEndFunction Main()Print(DecimalToHex(26))Print(DecimalToBin(5))EndSeptember 30, 2016 at 5:46 pm #4177Oops. 4 not 8. Noob mistake. xD Gotta convert those strings. Haha.
September 30, 2016 at 6:54 pm #4181here a more complete set:
[/crayon]Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394[crayon-5cba8b0f3c52f429005164 inline="true" ]#import "<std>"Using mojo..Using std..Function DecimalToHex:String(dec:Int)If(Not dec) Then Return "00"Local dic:String = "0123456789ABCDEF"Local hex:String = ""While(dec)Local bits := dec & $fLocal hi := String.FromChar(dic[bits])hex = hi + hexdec = dec Shr 4WendReturn hexEndFunction DecimalToBin:String(dec:Int)If(Not dec) Then Return "0"Local bin:String = ""While(dec)bin = String(dec & 1) + bindec = dec Shr 1WendReturn binEndFunction HexToDecimal:Int(str:String)If str = "" Return 0Local dic := "0123456789ABCDEF"Local dec :Int = 0While str.Length > 0Local char := str.Left(1).ToUpper()If dic.Contains(char)dec = (dec Shl 4) | dic.Find(char)ElseReturn 0Endifstr = str.Mid(1,str.Length-1)WendReturn decEnd FunctionFunction BinToDec:Int(str:String)If str = "" Return 0Local dec :Int = 0While str.Length > 0Local char := str.Left(1)If char = "0" Or char = "1"dec = (dec Shl 1) | Int(char)ElseReturn 0Endifstr = str.Mid(1,str.Length-1)WendReturn decEnd FunctionFunction Main()Print(DecimalToHex(26))Print(DecimalToBin(5))Print(HexToDecimal("1aA"))Print(BinToDec("10001"))EndSeptember 30, 2016 at 7:17 pm #4182Monkey12345678910111213#import "<std>"Using std.stringioFunction Main()Print ULongToString( 26,16 )Print ULongToString( 5,2 )Print StringToULong( "1aA",16 )Print StringToULong( "10001",2)EndSeptember 30, 2016 at 8:20 pm #4183Thats funny, I have been through the string documentation several times but for some reason I never actually payed attention to those functions.
September 30, 2016 at 8:22 pm #4184Wow. *super facepalm*
Well I guess I know more about hex and binary now. xD Thank you Mark and everyone else. xD
September 30, 2016 at 8:28 pm #4185It’s good practice to write these yourself at least once in your career I think!
September 30, 2016 at 8:52 pm #4187You’re absolutely right! Knowledge is power these days. xD
-
AuthorPosts
You must be logged in to reply to this topic.