Jump to content
Sign in to follow this  
The_Noob

Convert from base 16 (hex) to base 24

Recommended Posts

The_Noob

So i thought i had to convert hex (base 16) to dec (base 10) but apparently for what i want to do, i need to convert a string to base 24. Any takers on how to do that manually?

may help to wiki "Radix", And i found base 24 is 0 1 2 3 4 5 6 7 8 9 A B C D E F G H J K L M N P. No I or O because they can be confused with 1 and 0.

Why would i want to do this? To be able to make a prog that recovers my Halo CD key. :/

I know the registry keys that i need to access, and i had a prog that was semi written, but once i figured this is what i had to do, i was stuck. Everything else is... well pretty much useless unless i can get this figured out.

Im not real optimistic about this. If someone could figure it out that would totally surprise me.

Share this post


Link to post
Share on other sites
Zedna

Here are some conversion functions from DES.AU3 project.

It's not mine and I couldn't find it here so I posted source snippet:

Func _DecToBin($dec, $size = 0)
    If (Not IsInt($dec)) Or ($dec < 0) Then Return -1
    $bin = ""
    If $dec = 0 Then Return "0000"
    While $dec <> 0
        $bin = BitAND($dec, 1) & $bin
        $dec = BitShift($dec, 1)
    WEnd
    $diff = $size - StringLen($bin)
    If $diff > 0 Then
        For $i = 1 To $diff
            $bin = 0 & $bin
        Next
    EndIf
    Return $bin
EndFunc   ;==>_DecToBin

Func _BinToDec($bin)
    If (Not IsString($bin)) Then Return -1
    $end = StringLen($bin)
    $dec = 0
    For $cpt = 1 To $end
        $char = StringMid($bin, $end + 1 - $cpt, 1)
        Select
            Case $char = "1"
                $dec = BitXOR($dec, BitShift(1, - ($cpt - 1)))
            Case $char = "0"
                ; nothing
            Case Else
                ;error
                Return -1
        EndSelect
    Next
    Return $dec
EndFunc   ;==>_BinToDec

Func _HexToDec($hex)
    Local $dec = 0
    $hex = StringSplit($hex, "")
    For $i = $hex[0] To 1 Step - 1
        $dec += Dec($hex[$i]) * (16^ ($i - 1))
    Next
    Return $dec
EndFunc   ;==>_HexToDec

Func _HexToBin($hex)
    Local $bin = ""
    $hex = StringSplit($hex, "")
    For $i = 1 To $hex[0]
        $bin &= _DecToBin(Dec($hex[$i]), 4)
    Next
    Return $bin
EndFunc   ;==>_HexToBin

Func _BinToHex($bin)
    Local $hex
    For $i = 1 To StringLen($bin) Step 4
        $hex &= Hex(_BinToDec(StringMid($bin, $i, 4)), 1)
    Next
    Return $hex
EndFunc   ;==>_BinToHex

Func _XOR($bits1, $bits2)
    Local $return
    $bits1 = StringSplit($bits1, "")
    $bits2 = StringSplit($bits2, "")
    
    For $i = 1 To $bits1[0]
        If $bits1[$i] <> $bits2[$i] Then
            $return &= 1
        Else
            $return &= 0
        EndIf
    Next
    
    Return $return
EndFunc   ;==>_XOR

Func _RotateLeft($bin, $pos)
    Return StringRight($bin, StringLen($bin) - $pos) & StringLeft($bin, $pos)
EndFunc   ;==>_RotateLeft

Look at these functions for ideas and make similar one for you ...

Edited by Zedna

Share this post


Link to post
Share on other sites
PsaltyDS

Couldn't help taking a shot at it. What can I say? I'm a multi-base math geek:

While 1
    $TestNum = InputBox("Base24 Convert", "Input a number: ")
    If @error Then Exit
    
    $Direction = 0
    If MsgBox(32 + 4, "Base24 Convert", "Convert Number --> Base24?" & @CRLF & _
            "Click YES to convert Number --> Base24," & @CRLF & _
            "or click NO to convert Base24 --> Number") = 7 Then $Direction = 1

    $Answer = _Base24Convert($TestNum, $Direction)
    If @error Then
        MsgBox(16, "Base24 Convert", "Error returned from _Base24Convert():" & @CRLF & _
                "Input = " & $TestNum & @CRLF & _
                "Direction = " & $Direction & @CRLF & _
                "Return = " & $Answer & @CRLF & _
                "@error = " & @error)
    Else
        MsgBox(64, "Base24 Convert", "Success:  " & $TestNum & " = " & $Answer)
    EndIf
WEnd

; ------------------------------------
; Function _Base24Convert($Input, $Dir = 0)
;   Converts between regular numbers and Base24
;   Call with:  _Base24Convert($Input, $Dir)
;       Where:  $Input = Input number in any format that Number() will accept
;                       Floats will be rounded to integers
;               $Dir =  0 Direction Number --> Base24 (Default)
;                       1 Direction Base24 --> Number
;   On success, returns the converted number.
;   On error, returns 0 and sets @error (see code)
; ------------------------------------
Func _Base24Convert($Input, $Dir)
    Local $B24[24] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", _
            "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P"]
    Local $Split, $n, $x, $Neg = False, $Digit, $Exp = 0
    
    If $Dir Then
        ; --------------------------
        ; Convert Base24 --> Number
        ; --------------------------
        $RET = 0
        ; Check for negative
        If StringLeft($Input, 1) = "-" Then
            $Neg = True
            $Input = StringTrimLeft($Input, 1)
        EndIf
        If $Input = "" Then Return SetError(1, 0, 0)

        ; Convert
        $Split = StringSplit($Input, "")
        For $n = $Split[0] To 1 Step - 1
            ; Lookup digits in Base 24 table
            $Digit = ""
            For $x = 0 To 23
                If $Split[$n] = $B24[$x] Then
                    $Digit = $x
                    ExitLoop
                EndIf
            Next
            If $Digit = "" Then Return SetError(2, 0, 0)
            
            ; Multiply it in
            $RET += $Digit * (24 ^ $Exp)
            $Exp += 1
        Next
        If $Neg Then $RET = $RET * - 1
        Return $RET
    Else
        ; --------------------------
        ; Number --> Base24 (Default)
        ; --------------------------
        $RET = ""
        If $Input = "" Then Return SetError(1, 0, 0)
        $Input = Round(Number($Input))
        
        ; Check for negative
        If $Input < 0 Then
            $Neg = True
            $Input = $Input * - 1
        EndIf
        
        ; Convert
        While 1
            $Digit = Mod($Input, 24)
            
            ; Lookup digit in Base 24 table
            $RET = $B24[$Digit] & $RET
            
            ; Test for doneness
            $Input = ($Input - $Digit) / 24
            If $Input <= 0 Then ExitLoop
        WEnd
        If $Neg Then $RET = "-" & $RET
        Return $RET
    EndIf
EndFunc   ;==>_Base24Convert

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
therks

Ahh fuggit, here's my spin on it. It's only one way though (which I'm sure is the only reason it's shorter than Psalty's).

Func _Base16to24($vInput)
    Local $iLength, $iDecimal, $sOutput, $sChar, $iCharVal
    Local $sCharList = 'ABCDEFGHJKLMNP'
    $vInput = String($vInput)
    $iLength = StringLen($vInput)
    For $iPos = 1 to $iLength
        $sChar = StringMid($vInput, $iPos, 1)
        If $sChar = '0' Then
            ContinueLoop
        ElseIf StringIsInt($sChar) Then
            $iCharVal = Int($sChar)
        Else
            $iCharVal = StringInStr($sCharList, $sChar)
            If Not $iCharVal Then
                SetError(3, $iPos)
                Return False
            Else
                $iCharVal += 9
            EndIf
        EndIf
        $iPow = $iLength - $iPos
        $iDecimal += $iCharVal * 16 ^ $iPow
    Next
    While 1
        $iCharVal = Mod($iDecimal, 24)
        $iDecimal = ($iDecimal - $iCharVal) / 24
        If $iCharVal < 10 Then
            $sChar = $iCharVal
        Else
            $sChar = StringMid($sCharList, $iCharVal - 9, 1)
        EndIf
        $sOutput = $sChar & $sOutput
    WEnd
    Return $sOutput
EndFunc

Share this post


Link to post
Share on other sites
PsaltyDS

Ahh fuggit, here's my spin on it. It's only one way though (which I'm sure is the only reason it's shorter than Psalty's).

Make yours convert BOTH WAYS and we'll talk about who's function is shorter. The one-way conversion is only 17 lines in mine.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×