Sign in to follow this  
Followers 0
SkellySoul

String Return

8 posts in this topic

Hello :(

I got no idea how to even go about this but what I would like is for away to take a string and make it return a unique 8 digit number for that string...

So for Example

Red = 01286410

Blue = 12345678

Green = 80012864

and these strings would always return the same results and no other strings can return this same result...

question is how would you do that :graduated:

Share this post


Link to post
Share on other sites



Does it have to be an 8 digit string?

If you made a CRC hash of the string you would get a unique 8 character Hexadecimal number that would always be the same for that particular string.

Red = 6F192CC2

Blue = 25C15B11

Green = 8A65043E

Thanks, yes it has to be a 8 digit return and it must only be digits which makes it kind of hard :graduated:

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

:(

Func _Hash($sText)
    Switch $sText
        Case "Red"
            Return 01286410
        Case "Blue"
            Return 12345678
        Case "Green"
            Return 80012864
        Case Else
            Return "Unsupported something something"
    EndSwitch
EndFunc

Edit: wrong numbers! :graduated:

Edited by AdmiralAlkex

Share this post


Link to post
Share on other sites

:D

Func _Hash($sText)
    Switch $sText
        Case "Red"
            Return 01286410
        Case "Blue"
            Return 12345678
        Case "Green"
            Return 80012864
        Case Else
            Return "Unsupported something something"
    EndSwitch
EndFunc

Edit: wrong numbers! :graduated:

Thanks, but that would take forever and get to bulky...this is for a server basically...you see when people register they are given a unique 8 digit id...so it has to be done sort of like a encryption... :(

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

If you're looking for a conversion of the string, AngelofDeath's solution is probably pretty close to what you can do, but there is a minute chance of getting identical numerical strings for different alphabetic strings for the simple reason that there are more permutations for a 6 character alphabetic combination than there are for an 8 digit numerical combination.

Wouldn't it be easier to generate a database that stores the used character strings, and assigns an arbitrary unused numerical string when a new character string is added?

Edited by Tvern

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

How unique does it have to be?

You can safe the character with AscW() but then you are limited to four Characters max.

Small uncomplete example:

#include <Array.au3>    ; For _ArrayDisplay()
Local $a = StringToASCIIArray("Red");eg Red
;_ArrayDisplay($a)
if UBound($a) < 4 Then
    _ArrayAdd($a, "66");for less than 4 Characters Need to also cover shorter Words.
                        ;Need to also remember to catch the 40 as a nothing
endif
Local $saveas
For $i = 0 To 3
    $saveas &= $a[$i] - 26;Keep ASCII under 100 also add some more secure less obvious return
Next
MsgBox(0, '', $saveas)

Added example.

Edited by JoHanatCent

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Well ok, basically I want to come with a way to assign a unique 8 digit number to each user and I figured a encryption like method would be best since it would be sure of not repeating the return twice...however I am willing to drop the encryption unique number generator but the thing is how do I assign a unique number each time...I don't want to keep doing a "Until Loop" until I get a unique number.

I can make it check the database for the key to see if it's already in use but I am afraid of it taking to long to search through all of the numbers.

Ive noticed that some games on Ipods do this and give your account a unique friend code and they must do it some way that is fast.

Edited by SkellySoul

Share this post


Link to post
Share on other sites

This example generates a unique number for a specified string.

Here are some results from using the add string and read/write CSV functions:-

Using a 2000x2 array, the following approximate speeds were obtained.

Add string time = 100 milliseconds

Save array time = 220 milliseconds

Load array time = 130 milliseconds

Using a 20000x2 array, the following approximate speeds were obtained.

Add string time = 620 milliseconds

Save array time = 1800 milliseconds

Load array time = 1200 milliseconds

#include <Array.au3>

Local $sDisplay
Global $aStringToUniqueNumber[3][2] = [["Red", "01286410"],["Blue", "12345678"],["Green", "80012864"]]
;Global $aStringToUniqueNumber = _CSVReadTo2DArray("test2000x1.csv")

; ============= Add new string to storage array. ======
;Local $begin = TimerInit()
_AddStr_Num("A new unique string added. ", $aStringToUniqueNumber)
;ConsoleWrite("Add Time = " & TimerDiff($begin) & @CRLF)

; ============= Display array =========================
Local $sDisplay
For $i = 0 To UBound($aStringToUniqueNumber) - 1
    ;$sDisplay &= $aStringToUniqueNumber[$i][0] & " = " & StringRight("0000000" & $aStringToUniqueNumber[$i][1], 8) & @CRLF
    ; or
    $sDisplay &= StringFormat("%s = %08u\n", $aStringToUniqueNumber[$i][0], $aStringToUniqueNumber[$i][1])
Next
MsgBox(0, "Stored in Array", $sDisplay)

; ============= Save Array ===========================
;Local $begin = TimerInit()
_CSVWriteFrom2DArray(@ScriptDir & "\file.txt", $aStringToUniqueNumber)
;ConsoleWrite("Save Time = " & TimerDiff($begin) & @CRLF)
ShellExecute(@ScriptDir & "\file.txt")
Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
WinWait("file.txt")
MsgBox(0, "Stored Array in File", "Contents of file.")

; ============= Load array ===========================
;Local $begin = TimerInit()
$aNewArray = _CSVReadTo2DArray(@ScriptDir & "\file.txt")
;ConsoleWrite("Load Time = " & TimerDiff($begin) & @CRLF)
_ArrayDisplay($aNewArray, "Loaded Array from File")

FileDelete(@ScriptDir & "\file.txt")


Func _AddStr_Num($sStr, ByRef $aStrUniqueNum)
    Local $iUniqueNum, $iDim
    If _ArraySearch($aStrUniqueNum, $sStr) = -1 Then
        Do ; ------ Generate unique number ----------
            $iUniqueNum = Random(0, 99999999, 1)
            If _ArraySearch($aStrUniqueNum, $iUniqueNum) = -1 Then
                $iDim = UBound($aStrUniqueNum)
                ReDim $aStrUniqueNum[$iDim + 1][2]
                $aStrUniqueNum[$iDim][0] = $sStr
                $aStrUniqueNum[$iDim][1] = $iUniqueNum
                ExitLoop
            EndIf
        Until 0
    Else
        MsgBox(16, "Error", '"' & $sStr & '" already exists. Can not add string.')
    EndIf
    Return
EndFunc   ;==>_AddStr_Num

Func _CSVReadTo2DArray($path)
    Local $hFile = FileOpen($path, 0)
    If $hFile = -1 Then Return SetError(1, 0, 0)
    Local $sFileContent = StringStripWS(FileRead($hFile), 2)
    FileClose($hFile)
    $aTmp = StringSplit($sFileContent, @LF)
    Local $aLines = StringSplit(StringStripCR($sFileContent), "," & @LF)
    Local $iCol = $aLines[0] / $aTmp[0]
    Local $arr[$aTmp[0]][$iCol]
    For $i = 0 To UBound($aLines) - $iCol - 1 Step $iCol
        For $x = 0 To UBound($arr, 2) - 1
            $arr[($i / $iCol)][$x] = $aLines[$i + $x + 1]
        Next
    Next
    Return $arr
EndFunc   ;==>_CSVReadTo2DArray

Func _CSVWriteFrom2DArray($path, ByRef $arr)
    Local $hFile = FileOpen($path, 10) ; 10 = 2(Erase previous contents) + 8(Create if not exist)
    Local $sStr
    For $i = 0 To UBound($arr) - 1
        For $x = 0 To UBound($arr, 2) - 1
            $sStr &= $arr[$i][$x] & ","
        Next
        If $i <> UBound($arr) - 1 Then
            FileWrite($hFile, StringTrimRight($sStr, 1) & @CRLF)
        Else
            FileWrite($hFile, StringTrimRight($sStr, 1))
        EndIf
        $sStr = ""
    Next
    FileClose($hFile)
    Return
EndFunc   ;==>_CSVWriteFrom2DArray

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  
Followers 0