Jump to content

Convert Alpha-Num to Decimal?


Recommended Posts

Okay what I'm actually doing is converting a dell service tag into an express service tag.

Based on what I read this seemed like something easy for Auto-IT but haven't found anything in the Math or Conversion sections of the help file.

Here is an example and description along with a link.

5RFDP01 = 125-423-316-01

http://www.creativyst.com/Doc/Articles/HT/Dell/DellNumb.htm

The dashes are the only thing that might throw you.

  • The Service Tag is just a big base-36 number (composed of digits [0-9A-Z]) so,
  • Convert it to a big decimal number (12,542,331,601), then
  • Add dashes every third digit starting from the left-most (MSD) digit
Thanks again as always,

-Kenny

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

;From base 36
consolewrite(addDash(ConvertBase("5RFDP01")) & @crLF)
consolewrite(ConvertBase("5rfdP01") & @crLF)
consolewrite(ConvertBase("0") & @crLF)
consolewrite(ConvertBase("Z") & @crLF)
consolewrite(ConvertBase("Z0") & @crLF)
consolewrite(ConvertBase("10") & @crLF)
consolewrite(ConvertBase("200")& @crLF)

;From hex (base 16)
consolewrite(ConvertBase("0",16)& @crLF)
consolewrite(ConvertBase("10",16)& @crLF)

;From binary (base 2)
consolewrite(ConvertBase("11",2)& @crLF)
consolewrite(ConvertBase("100",2)& @crLF)

;Converts from a certain base to base 10
func convertBase($s, $fromBase=36)
    $Total=0
    $BaseValue=1
    
    for $i=stringlen($s) to 1 step -1
        $DecimalValue = asc(stringmid($s,$i,1))-48
        if $DecimalValue >= 17 then $DecimalValue=$DecimalValue-7
        if $DecimalValue >= 42 then $DecimalValue=$DecimalValue-32
        
        if $i=stringlen($s) Then
            $total+=$decimalvalue
        Else
            $total += ($decimalvalue * $BaseValue)
        EndIf
        
;   consolewrite(stringmid($s,$i,1)& " " & $DecimalValue & " " & $decimalvalue * $BaseValue & @CRLF)

        $BaseValue=$BaseValue * $fromBase
    next
    
    return $total
    
EndFunc

;Adds some formatting if needed
func addDash($s)
    local $result = stringleft($s,3)
    For $i=4 To StringLen($s) Step 3
        $result &= "-" & StringMid($s, $i, 3)  
    Next
    Return $result
endFunc

edit:

* Added lowercase a-z to work

* Added dashes logic

* Added different base from logic so can handle binary (base 2), handle hex (base 16) or any other wanted base between 2 and 36

Edited by junkew
Link to comment
Share on other sites

Okay what I'm actually doing is converting a dell service tag into an express service tag.

Based on what I read this seemed like something easy for Auto-IT but haven't found anything in the Math or Conversion sections of the help file.

Here is an example and description along with a link.

5RFDP01 = 125-423-316-01

http://www.creativyst.com/Doc/Articles/HT/Dell/DellNumb.htm

The dashes are the only thing that might throw you.

  • The Service Tag is just a big base-36 number (composed of digits [0-9A-Z]) so,
  • Convert it to a big decimal number (12,542,331,601), then
  • Add dashes every third digit starting from the left-most (MSD) digit
Thanks again as always,

-Kenny

Here's another attempt

MsgBox(0, "result = ", code("5RFDP01"))

Func code($tag)
    Local $factor = 1
    Local $ans = 0
    Local $n
    For $n = StringLen($tag) To 1 Step -1
        $next = StringMid($tag, $n, 1)
        Switch Asc($next)
            Case Asc("A") To Asc("Z")
                $ans += (Asc($next) - Asc("A") + 10) * $factor
            Case Asc("0") To Asc("9")
                $ans += (Asc($next) - Asc("0")) * $factor
        EndSwitch
        $factor *= 36
    Next

    Local $result = StringMid($ans, 1, 3)
    For $n = 4 To StringLen($ans) Step 3
        $result &= "-" & StringMid($ans, $n, 3)
    Next
    Return $result

EndFunc  ;==>code
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Shames me to admit, but I never did a to-AutoIt3 conversion of Dell's own ECMAScript converter code (copied from Dell's support site years and years ago).

Very, very nice, group. Only gotcha is that you need to be sure to convert the initial string to uppercase if you're leveraging the ASCII code for the letters.

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Link to comment
Share on other sites

@junkew: :D Respect for your compact code! :D

[snipped the far better approach]

As I thought this an interesting example to explain base conversion I coded a much too many lines and illustrated approach. As I've written it, I'll post it anyways:

#include <array.au3>

dim $Base36Arr[36][2]
Dim $Base=36
dim $k = -1
#Region fill the base36 lookup table
#Region fill 0-9
for $i = 0 to 9
    $k += 1 
    $Base36Arr[$k][0]=$i
    $Base36Arr[$k][1]=$k
Next
#EndRegion fill 0-9

#Region fill A - Z, A = 65, Z = 90
for $i = 65 to 90
    $k += 1 
    $Base36Arr[$k][0]=chr($i)
    $Base36Arr[$k][1]=$k
Next
#EndRegion fill A - Z, A = 65, Z = 90
_ArrayDisplay($Base36Arr,"The lookup table for base 36 numbers:")
#EndRegion fill the base36 lookup table

#Region Fill the 36^x lookup table
Dim $Exp36Array[7] ; what's the base36 value of this digit, goes 0 to 6
for $i = 0 to 6
    $Exp36Array[$i]=Exp(Log(36)*$i) ; Exp(log(x)*y) = x^y
Next
_ArrayDisplay($Exp36Array,"That's the value of the digits in a base 36 number")
#EndRegion Fill the 36^x lookup table


$DellSrvTg="5RFDP01"
$ExpressSrvTg=0
$Bit36=-1
for $i = StringLen($DellSrvTg) to 1 Step -1
    $Bit36 +=1 
    $NextDigit=StringMid($DellSrvTg,$i,1)
    for $k = 0 to 35
        If $NextDigit = $Base36Arr[$k][0] Then
                     $ExpressSrvTg += $Base36Arr[$k][1] * $Exp36Array[$Bit36]
        EndIf
        #ce
    Next
Next
MsgBox(0,"Your Express Service Tag is:",$ExpressSrvTg)


MsgBox(0,"Final Value:",StringLeft($ExpressSrvTg,3) & "-" & StringMid($ExpressSrvTg,4,3) & "-" & StringMid($ExpressSrvTg,7,3)& "-" & StringRight($ExpressSrvTg,2))

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Great, works perfect. Thanks guys!

DaveF, perhaps could be added to the next release as "AlphaToDec"? Granted I don't know how it would be otherwise useful but we seem to have just about every other conversion in their such as dec, hex, bin etc...

Thanks again,

Kenny

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Been there, done that, wasn't accepted. I wrote a _NumBaseConvert function a long time ago.

Very nice function.

Just range checking would need to be added to recognize if the "$v_Input" isn't a valid number for the given "$i_BaseFrom":

MsgBox(0,"",_numbaseconvert("ffff",2,10) & ", Error: "&@error)

should throw an error instead of returning "225"

I think this is a :D function :D worth to "be accepted" :)

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...