Page 1 of 1

Hex to Decimal Converter

Posted: 24 May 2014, 09:40
by Menixator

Code: Select all

/*
hexToDecimal(str)
Converts hexadecimal strings to decimal numbers.
Parameters:
    - str: The hex value to convert to decimal. Optionally can have the 0x prefix. Any characters that do not fit the range 0-9, a-z are removed beforehand.

Examples:
    MsgBox % hexToDecimal("0xfffff") ; 1048575
    ; Notice that the case doesn't matter.
    MsgBox % hexToDecimal("DEADBEEF") ; 3735928559
*/  

hexToDecimal(str){
    local newStr := ""
    static comp := {0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, "a":10, "b":11, "c":12, "d":13, "e":14, "f":15}
    StringLower, str, str
    str := RegExReplace(str, "^0x|[^a-f0-9]+", "")
    Loop, % StrLen(str)
        newStr .= SubStr(str, (StrLen(str)-A_Index)+1, 1)
    newStr := StrSplit(newStr, "")
    local ret := 0
    for i,char in newStr
        ret += comp[char]*(16**(i-1))
    return ret
}

Re: Hex to Decimal Converter

Posted: 24 May 2014, 12:42
by HotKeyIt
I don't think we need to use RegEx here, ltrim should be enough.
Here is one that runs twice as fast ;)

Code: Select all

hexToDecimal(str){
    static _0:=0,_1:=1,_2:=2,_3:=3,_4:=4,_5:=5,_6:=6,_7:=7,_8:=8,_9:=9,_a:=10,_b:=11,_c:=12,_d:=13,_e:=14,_f:=15
    str:=ltrim(str,"0x `t`n`r"),   len := StrLen(str),  ret:=0
    Loop,Parse,str
      ret += _%A_LoopField%*(16**(len-A_Index))
    return ret
}

Re: Hex to Decimal Converter

Posted: 24 May 2014, 16:30
by kon
HotKeyIt wrote:Here is one that runs twice as fast ;)
Not written in pure AHK, but the fastest conversion I know of is this DllCall:

Code: Select all

;http://www.autohotkey.com/board/topic/18670-converting-numbers-to-any-base-radix-using-msvcrtdll/
BaseToDec(n, Base) {
	static U := A_IsUnicode ? "wcstoui64_l" : "strtoui64"
	return, DllCall("msvcrt\_" U, "Str",n, "Uint",0, "Int",Base, "CDECL Int64")
}

DecToBase(n, Base) {
	static U := A_IsUnicode ? "w" : "a"
	VarSetCapacity(S,65,0)
	DllCall("msvcrt\_i64to" U, "Int64",n, "Str",S, "Int",Base)
	return, S
}
It is actually pretty interesting to do a search for this kind of script, there are quite a few of them out there.

Re: Hex to Decimal Converter

Posted: 26 May 2014, 21:42
by joedf
@Kon good point.
But, anyways... Pure math solution, no string functions or dllcall (but DLL is faster :P ) anyways, have you all forgotten about laszlo's ToBase() function ?
http://www.autohotkey.com/board/topic/1 ... ntry103624

Code: Select all

	;from Laszlo : http://www.autohotkey.com/board/topic/15951-base-10-to-base-36-conversion/#entry103624
	ToBase(n,b) { ; n >= 0, 1 < b <= 36
		Loop {
			d := mod(n,b), n //= b
			m := (d < 10 ? d : Chr(d+55)) . m
			IfLess n,1, Break
		}
		Return m
	}
Cheers ;)

Re: Hex to Decimal Converter

Posted: 27 May 2014, 02:00
by guest3456
^might as well write that in MCode :P

Re: Hex to Decimal Converter

Posted: 27 May 2014, 08:38
by joedf
Haha true! ;)

Re: Hex to Decimal Converter

Posted: 30 May 2014, 08:25
by jNizM
@kon
or in 1 function to convert base from 2 to 36

Code: Select all

MsgBox, % "Decimal:`t`t42`n"
        . "to Binary:`t`t"      ConvertBase(10, 2, 42)       "`n"
        . "to Octal:`t`t"       ConvertBase(10, 8, 42)       "`n"
        . "to Hexadecimal:`t"   ConvertBase(10, 16, 42)      "`n`n"
        . "Hexadecimal:`t2A`n"
        . "to Decimal:`t"       ConvertBase(16, 10, "2A")    "`n"
        . "to Octal:`t`t"       ConvertBase(16, 8, "2A")     "`n"
        . "to Binary:`t`t"      ConvertBase(16, 2, "2A")     "`n`n"
ExitApp


ConvertBase(InputBase, OutputBase, number)
{
    static u := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
    static v := A_IsUnicode ? "_i64tow"    : "_i64toa"
    VarSetCapacity(s, 65, 0)
    value := DllCall("msvcrt.dll\" u, "Str", number, "UInt", 0, "UInt", InputBase, "CDECL Int64")
    DllCall("msvcrt.dll\" v, "Int64", value, "Str", s, "UInt", OutputBase, "CDECL")
    return s
}

Re: Hex to Decimal Converter

Posted: 01 Mar 2016, 11:07
by kidingwithlaura
I am not a programmer but I landed here while looking for number converter tools and I have got one resource as well, I want to thank all the hard working programmers for making such useful tools.