Sign in to follow this  
Followers 0
perfaram

Translating a GUID to string

8 posts in this topic

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 

Share this post


Link to post
Share on other sites



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!
¯\_(ツ)_/¯

Share this post


Link to post
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 

Share this post


Link to post
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!
¯\_(ツ)_/¯

Share this post


Link to post
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

Share this post


Link to post
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!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

Thanks UEZ for your quick answer, and thanks mikell for the optimization  :thumbsup:


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

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