Skip to content

Instantly share code, notes, and snippets.

@isosphere
Last active February 28, 2023 13:33
Embed
What would you like to do?
Given a string with decimal numbers in it, convert all of those to their hexadecimal representation.
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
@isosphere
Copy link
Author

isosphere commented Feb 28, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment