我是VB6的新手,我想将参数从Double转换为Hex,然后再转换为Long for Chinese Unicode。
此处的问题是输入值为1-127,输出相同。对于超过127的值,输出为63.参数长度为4个字节。这是我到目前为止使用的2个转换函数的片段:
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 Function
HexToLong:
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
答案 0 :(得分:0)
从评论转到答案,SO的对话太多了。
尝试更改此内容:
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字符串并一次将一个字符编码成Hex,那么你将需要使用一个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字符,您需要它们,因为它们可能大于一个字节。