Jump to content

Translating a GUID to string


Go to solution Solved by UEZ,

Recommended Posts

Hi all !

I came across this : '?do=embed' frameborder='0' data-embedContent>>

And I was wondering about reverting the created GUID to the original string. Is that possible ?

I think yes, because it only uses a StringRegExpReplace, but I'm not sure how to do it  :idiot:

Thx in advance !

PS : This may help

Never forget to mark a question as resolved, this button has been purposely created :-P 

Link to comment
Share on other sites

$g = CreateGUIDFromString('A GUID string.')
MsgBox(0, '', $g)

$s = BinaryToString("0x" & StringRegExpReplace($g, "[{}-]", ""))
MsgBox(0, '', $s)

Func CreateGUIDFromString($sString)
    Return StringRegExpReplace(StringToBinary($sString) & _ 
    "0000000000000000000000000000000000", "..(.{8})(.{4})(.{4})(.{4})(.{12}).*", "\{$1-$2-$3-$4-$5\}")
EndFunc

?

Link to comment
Share on other sites

  • Solution

Try this:

Func CreateStringFromGUID($sGUID)
    Local $sHex = StringRegExpReplace($sGUID, "[{|\-|}]", "")
    If @error Then Return SetError(1, 0, 0)
    If Mod(StringLen($sHex), 2) Then Return SetError(2, 0, 0)
    Local $aChars = StringRegExp($sHex, ".{2}", 3)
    If @error Then Return SetError(2, 0, 0)
    Local $i, $sReturn, $sChar
    For $i = 0 To UBound($aChars) - 1
        If Not Int($aChars[$i]) Then ExitLoop
        $sReturn &= Chr(Dec($aChars[$i]))
    Next
    Return $sReturn
EndFunc

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

It works, but it is heavily truncated : 

MsgBox(32, "", CreateStringFromGUID(CreateGUIDFromString('Some very simple string.')))
Func CreateGUIDFromString($sString)
    Return StringRegExpReplace(StringToBinary($sString) & "0000000000000000000000000000000000", "..(.{8})(.{4})(.{4})(.{4})(.{12}).*", "\{$1-$2-$3-$4-$5\}")
EndFunc ;==>CreateGUIDFromString

Func CreateStringFromGUID($sGUID)
    Local $sHex = StringRegExpReplace($sGUID, "[{|\-|}]", "")
    If @error Then Return SetError(1, 0, 0)
    If Mod(StringLen($sHex), 2) Then Return SetError(2, 0, 0)
    Local $aChars = StringRegExp($sHex, ".{2}", 3)
    If @error Then Return SetError(2, 0, 0)
    Local $i, $sReturn, $sChar
    For $i = 0 To UBound($aChars) - 1
        If Not Int($aChars[$i]) Then ExitLoop
        $sReturn &= Chr(Dec($aChars[$i]))
    Next
    Return $sReturn
EndFunc

Will return 'Some very simple' instead of 'Some very simple string.'. Dunno if it's normal, though  :ermm:

Never forget to mark a question as resolved, this button has been purposely created :-P 

Link to comment
Share on other sites

The function is limited to 16 chars. That means the encoded GUID is truncated.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

UEZ,

$g = CreateGUIDFromString('A GUID string.')
;MsgBox(0, '', $g)

$hTimer = TimerInit()
For $i = 1 to 100
  $s = CreateStringFromGUID1($g) 
Next
$fDiff1 = TimerDiff($hTimer)
;MsgBox(0, '', $s)

$hTimer = TimerInit()
For $i = 1 to 100
  $s = CreateStringFromGUID2($g)
Next
$fDiff2 = TimerDiff($hTimer)
;MsgBox(0, '', $s)

MsgBox(0, '', $fDiff1 &@crlf& $fDiff2)

;=============================================
Func CreateStringFromGUID1($sGUID)
    Local $sHex = StringRegExpReplace($sGUID, "[{}-]", "")
    If @error Then Return SetError(1, 0, 0)
    If Mod(StringLen($sHex), 2) Then Return SetError(2, 0, 0)
    $sReturn = BinaryToString("0x" & $sHex)
    Return $sReturn
EndFunc   

Func CreateStringFromGUID2($sGUID)
    Local $sHex = StringRegExpReplace($sGUID, "[{|\-|}]", "")
    If @error Then Return SetError(1, 0, 0)
    If Mod(StringLen($sHex), 2) Then Return SetError(2, 0, 0)
    Local $aChars = StringRegExp($sHex, ".{2}", 3)
    If @error Then Return SetError(2, 0, 0)
    Local $i, $sReturn, $sChar
    For $i = 0 To UBound($aChars) - 1
        If Not Int($aChars[$i]) Then ExitLoop
        $sReturn &= Chr(Dec($aChars[$i]))
    Next
    Return $sReturn
EndFunc

Func CreateGUIDFromString($sString)
    Return StringRegExpReplace(StringToBinary($sString) & _ 
    "0000000000000000000000000000000000", "..(.{8})(.{4})(.{4})(.{4})(.{12}).*", "\{$1-$2-$3-$4-$5\}")
EndFunc
Link to comment
Share on other sites

Nice mikell  :thumbsup:

I hacked my version in 5 minutes together and have to leave my pc afterwards. So I had no time for optimizations. ;)

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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...