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|
|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)|
|ConvertNumbersToHex = text_result|
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.
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"
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.