Last active
February 28, 2023 13:33
-
-
Save isosphere/92c24d92ca220b90fc5bc1d7fe93b5a3 to your computer and use it in GitHub Desktop.
Given a string with decimal numbers in it, convert all of those to their hexadecimal representation.
This file contains 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 ConvertNumbersToHex(text As String) As String | |
Dim objRegex As RegExp | |
Dim results | |
Dim text_result | |
Dim hex_value | |
Set objRegex = New RegExp | |
objRegex.Global = True | |
objRegex.Pattern = "(\d+)" | |
Set results = objRegex.Execute(text) | |
text_result = text | |
For Each MatchInstance In results | |
hex_value = Hex(CInt(MatchInstance.Value)) | |
text_result = Replace(text_result, MatchInstance.Value, hex_value, 1, 1, vbTextCompare) | |
Next | |
ConvertNumbersToHex = text_result | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've used this script in Excel with success.
Example usage:
ConvertNumbersToHex("0: ADSR; 25: AHR; 50: AR; 75: AR loop; 127: Open") results in "0: ADSR; 19: AHR; 32: AR; 4B: AR loop; 7F: Open"
Warning
I think it's possible for this to misfire, since we're using
Replace
. Good results depend on the order of matching + order of replacing aligning, which I think should probably be fine but honestly I would have preferred to do more direct substring manipulation, the match object knows the index of the match and its total length so it should be easy, unfortunately VBScript has no such functionality.