我对VB6还是个新手,我想把一个参数从双精度转换成十六进制,再转换回长整型。
这里的问题是对于1-127的输入值,输出是相同的。对于超过127的值,输出为63。该参数的长度为4个字节。下面是我到目前为止使用过的两个转换函数的一个片段:
DoubleToHex:
Public Function ConvDoubleToHexString(dVal As Double, ByVal nByteCount As Integer) As String
m = 1
sHex = Hex(dVal)
nByteCountMerk = nByteCount
If nByteCount < Len(sHex) / 2 Then nByteCount = Len(sHex) / 2
If nByteCount < 1 Then nByteCount = 1
sHex = String(nByteCount * 2 - Len(sHex), "0") & sHex
For n = 0 To nByteCount - 1
sTmp = ChrW(Val("&H" & Mid(sHex, m, 2)))
m = m + 2
sRet = sRet & sTmp
next n
If Not bLH Then
ConvDoubleToHexString = Right(sRet, nByteCountMerk)
Else
For n = Len(sRet) To 1 Step -1
sLH = sLH & Mid(sRet, n, 1)
Next n
ConvDoubleToHexString = Left(sLH, nByteCountMerk)
End If
End FunctionHexToLong:
Public Function ConvHexStringToLong(sHex As String) As Long
If sHex = "" Then
ConvHexStringToLongAbs = knolong
Exit Function
End If
For i = 1 To Len(sHex)
If l >= 8388608 Then
ConvHexStringToLongAbs = knolong
Exit Function
End If
l = l * 256 * 2
l = l + Asc(Mid(sHex, i, 1))
Next
ConvHexStringToLong = l
End Function发布于 2017-05-16 22:40:12
从评论到答案,这里有太多的对话。
尝试更改此设置:
sHex = String(nByteCount * 2 - Len(sHex), "0") & sHex
For n = 0 To nByteCount - 1
sTmp = ChrW(Val("&H" & Mid(sHex, m, 2)))
m = m + 2
sRet = sRet & sTmp
next n要这样做:
Dim charCode As Long
sHex = String(nByteCount * 2 - Len(sHex), "0") & sHex
For n = 0 To nByteCount - 1
sTmp = ChrW(Val("&H" & Mid(sHex, m, 2)))
charCode = AscW(sTmp)
m = m + 2
sRet = sRet & sTmp
next n然后,在单步执行调试器中每个循环上的代码时,请确认charCode的值是您期望的值。
编辑:
我仍然不清楚你真正想要完成的是什么(脱离上下文的功能不能说明问题)。但是,如果您尝试获取unicode字符串并将其一次一个字符编码为十六进制,那么您将需要使用4个字符的十六进制代码,因为它们每个都是2个字节。如下所示(不检查语法):
Dim unicodeInput As String
unicodeInput = "whatever"
Dim offset As Long
Dim charCode As Integer
Dim hexOutput As String
for offset = 1 to Len(unicodeInput)
charCode = AscW(Mid$(unicodeInput, offset, 1))
hexOutput = hexOutput & Right$("0000" & Hex$(charCode), 4)
next然后,您可以将该字符串从十六进制移回原始字符串,如下所示:
Dim hexInput As String
hexInput = "003900390039"
Dim offset As Long
Dim char As String
Dim output As String
for offset = 1 to Len(hexInput) Step 4
char = ChrW(CLng("&H" & Mid$(hexInput, offset, 4)))
output = output & char
next不过,我不确定这是否解决了您的问题,因为我不明白您为什么要将unicode字符序列编码和解码为十六进制缓冲区。
您可能不想在十六进制值中使用额外的零,但是为了容纳Unicode字符,您将需要它们,因为它们可能比一个字节还大。
https://stackoverflow.com/questions/43973030
复制相似问题