Last active
March 28, 2018 13:16
-
-
Save JohnLBevan/54e9e40ac5b6a7dbaba986d754f896e7 to your computer and use it in GitHub Desktop.
Shorten a GUID from 36 chars to 11 UTF-8 Chars. Inspired by https://www.codeproject.com/Tips/1236704/Reducing-the-string-Length-of-a-Guid?msg=5505216#xx5505216xx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Compress-Guid { | |
| [CmdletBinding()] | |
| Param ( | |
| [Parameter(ValueFromPipeline = $true)] | |
| [Guid]$Guid = [Guid]::NewGuid() | |
| ) | |
| Process { | |
| Write-Verbose "Received Guid $Guid" | |
| [string]$GuidJustTheDigits = ($Guid.ToString() -replace '[{}-]','') #actually we don't need to replace {} since PS defaults to hyphens only formatting; but this does no harm / there's not much additional cost. | |
| Write-Verbose "Just the digits length: $($GuidJustTheDigits.Length)" #32 chars | |
| Write-Verbose "Just the digits: $GuidJustTheDigits" | |
| $CompressedGuid = ($GuidJustTheDigits -split '(.{3})' | ?{$_} | %{'0x{0}' -f $_} | %{32 + [int]$_} | %{[char]$_}) -join '' | |
| Write-Verbose "Length of Compressed Guid: $($CompressedGuid.Length )" #11 chars | |
| Write-Verbose "Compressed GUID: $CompressedGuid" | |
| $CompressedGuid | |
| } | |
| } | |
| Compress-Guid -Verbose |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment