关于Hex函数.
用Hex函数时,比如shu(0)=&H0
Hex(shu(0))就是0
怎么样让他变成00?
使用这个方法把Byte数组转换成16进制字串符时,只要是0打头的全省略的.
0E就成E了.怎么弄啊?
Function BinArrayToHex(Bin() As Byte) As String
Dim iLoop As Integer
Dim sResult As String
sResult = " "
For iLoop = LBound(Bin) To UBound(Bin)
sResult = sResult + Right("0 " & Hex(Bin(iLoop)), 2) & " "
Next iLoop
BinArrayToHex = Trim(sResult)
End Function
[解决办法]
Dim A As Long
Dim strTemp As String
A=5
strTemp = Hex(A)
strTemp = String(2-Len(strTemp),"0") & strTemp
Msgbox strTemp
[解决办法]
VB下做你需要的这种函数要比你想象中容易的多。HEX不是那么好用的。
- VB code
'第一种用法'输出Ascii编码的HEX串(可直接存盘的,显示需要StrConv转换)Text2.Text = StrConv(BytesHEX(tBytes()), vbUnicode) '第二种用法'输出Unicode编码的HEX串(在VB下直接可显示的)'函数输出的是Byte数组,但这里却赋给了字符串。'因为:VB下的Byte数组和字符串之间是可以互相赋值的。Text3.Text = BytesHEX(tBytes(), , True) Function BytesHEX(ByRef pBytes() As Byte, Optional ByVal pLimit As Byte = &H20, Optional ByVal pUnicode As Boolean = False) As Byte() Dim tSurBytes_Index As Long Dim tDesBytes() As Byte, tDesBytes_Index As Long, tDesBytes_Length As Long tDesBytes_Length = UBound(pBytes()) * 3 * ((pUnicode And 1) + 1) + 2 + (pUnicode And 3) ReDim tDesBytes(tDesBytes_Length) For tDesBytes_Index = 0 To tDesBytes_Length Step (3 + (pUnicode And 3)) tDesBytes(tDesBytes_Index) = HexEnCode(pBytes(tSurBytes_Index) \ 16) tDesBytes(tDesBytes_Index + 1 + (pUnicode And 1)) = HexEnCode(pBytes(tSurBytes_Index) Mod 16) tDesBytes(tDesBytes_Index + 2 + (pUnicode And 2)) = pLimit tSurBytes_Index = tSurBytes_Index + 1 Next BytesHEX = tDesBytes()End FunctionFunction HexEnCode(pHEX As Byte) As Byte '根据0~15的Byte数值输出0~F的Ascii码 '这行函数的意思是:HexEnCode = 48 + pHEX;如果pHEX>9,那么HexEnCode再加7。 HexEnCode = 48 + pHEX + ((pHEX > 9) And 7)End Function
[解决办法]
right("0" & Hex(shu(0)),2)