在Excel VBA中将字符串转换为十六进制 [英] Converting String to Hex in Excel VBA
问题描述
我尝试过:
Dim val As String
val =10
Dim hexVal As Integer
hexVal = Convert.ToInt32(val,16)
但 Convert.ToInt32(val,16)
不工作,猜测是因为它不是VB.NET?
无论如何,我可以使用 CInt(val)
转换字符串到一个整数,但如果我有十六进制值的字符串表示,例如3366CC,如何将其转换为十六进制,以便我可以执行十六进制计算?
在VBA中,您需要用$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ (& H&10)'// 16
value = val(& H3366CC)'// 3368652
编辑
函数FromHex(hexString As String)As Long
FromHex = Val(& H& hexString)
结束函数
然后
resultString = hex $(FromHex(3366CC)/ FromHex(A))
或者对于常量显而易见;
resultString = hex $(FromHex(3366CC)/& HA)
I need to convert strings to hexadecimals in my Excel VBA macro.
I tried:
Dim val As String
val = "10"
Dim hexVal As Integer
hexVal = Convert.ToInt32(val, 16)
but Convert.ToInt32(val, 16)
does not work, I'm guessing because it's not VB.NET?
Anyway, I can use CInt(val)
to convert a string to an integer but if I have the string representation of a hex value, e.g. "3366CC", how do I convert it to hex so that I can perform hex calculations on it?
In VBA you need to mess about with the &H
literal:
value = val("&H" & "10") '// 16
value = val("&H3366CC") '// 3368652
Edit
Function FromHex(hexString As String) As Long
FromHex = Val("&H" & hexString)
End Function
Then
resultString = hex$(FromHex("3366CC") / FromHex("A"))
Or for constants obviously;
resultString = hex$(FromHex("3366CC") / &HA)
这篇关于在Excel VBA中将字符串转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!