在vba中交换参数的字节顺序
我正在创建一个 Excel 工作表,它需要能够接收十进制格式的参数列表(不同大小、有符号和无符号),并从中生成十六进制字符串。然而问题是字符串中的参数需要是小端字节序,而字符串本身是大端字节序。我正在使用 VBA 来执行此操作,但我对此很陌生并且遇到了困难。我认为在将每个参数添加到字符串之前,我需要交换每个参数的字节顺序。我查看了这个网站和网络,但没有找到任何简单的解决方案来解决我猜应该是一个简单的问题。如果有人有任何建议,我们将不胜感激。
到目前为止,我用于生成字符串的代码如下所示:
SubGenerate_String()
Dim funcset As String
Dim y As String
x = 0
z = 0
funcset = ""
'while variant column is not empty
Do While ((Trim(Cells(2, 4 + z).Value) <> "") And (Trim(Cells(2, 4 + z).Value) <> "0"))
Do
'check if last cell in the column
If Trim(Cells(4 + x, 1).Value) = "VALUE" Then
'convert cell contents to hex
y = Hex(Cells(3 + x, 4 + z).Value)
'add the converted value to the string
funcset = funcset + "0x" + y
x = x + 1
Else
'convert cell contents to hex
y = Hex(Cells(3 + x, 4 + z).Value)
'add the converted value to the string
funcset = funcset + "0x" + y + " "
x = x + 1
End If
' continue until end of column is reached
Loop While Trim(Cells(3 + x, 1).Value) <> "VALUE"
'write the string to corresponding cell
Cells(3 + x, 4 + z).Value = funcset
funcset = ""
x = 0
z = z + 1
Loop
EndSub
这是一些示例数据: 数据已生成字符串,但字节序交换尚未完成
I am creating an excel sheet which needs to be capable of taking in a list of parameters (of varying sizes, signed and unsigned) in decimal format, and generating a hex string from this. The problem however is that the parameters within the string need to be little endian, while the string itself will be big endian. I am using VBA to do this, but I am quite new to it and am running in to difficulty. I think I need to swap the endianess of each parameter before I add it to the string. I have looked about this site and the web and have not found any simple solutions to what im guessing should be a simple problem. If anyone has any suggestions it would be much appreciated.
My code so far for generating the string is shown below:
Sub Generate_String()
Dim funcset As String
Dim y As String
x = 0
z = 0
funcset = ""
'while variant column is not empty
Do While ((Trim(Cells(2, 4 + z).Value) <> "") And (Trim(Cells(2, 4 + z).Value) <> "0"))
Do
'check if last cell in the column
If Trim(Cells(4 + x, 1).Value) = "VALUE" Then
'convert cell contents to hex
y = Hex(Cells(3 + x, 4 + z).Value)
'add the converted value to the string
funcset = funcset + "0x" + y
x = x + 1
Else
'convert cell contents to hex
y = Hex(Cells(3 + x, 4 + z).Value)
'add the converted value to the string
funcset = funcset + "0x" + y + " "
x = x + 1
End If
' continue until end of column is reached
Loop While Trim(Cells(3 + x, 1).Value) <> "VALUE"
'write the string to corresponding cell
Cells(3 + x, 4 + z).Value = funcset
funcset = ""
x = 0
z = z + 1
Loop
End Sub
Here is some sample data:
The data has a string generated, but endianess swap has not been done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不懂 VBA,但这是我遵循的算法。
如果 VBA 语法与 Excel 函数类似,我认为您可以在
HEX()
函数调用中添加第二个参数,2
来强制它使用 2 个字符。I don't know VBA, but here's the algorithm I'd follow.
If VBA syntax is similar to Excel function, I think you can add a second parameter,
,2
to yourHEX()
function call to force it to use 2 characters.