在vba中交换参数的字节顺序

发布于 2025-01-06 02:14:05 字数 1426 浏览 3 评论 0原文

我正在创建一个 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
The data has a string generated, but endianess swap has not been done

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨凡馨 2025-01-13 02:14:05

我不懂 VBA,但这是我遵循的算法。

for each parameter in parameter_list
    for i = 1 to (number of bytes in parameter type)
        append 2-character hex value of (parameter mod 256) to string
        parameter = parameter / 256

如果 VBA 语法与 Excel 函数类似,我认为您可以在 HEX() 函数调用中添加第二个参数 ,2 来强制它使用 2 个字符。

I don't know VBA, but here's the algorithm I'd follow.

for each parameter in parameter_list
    for i = 1 to (number of bytes in parameter type)
        append 2-character hex value of (parameter mod 256) to string
        parameter = parameter / 256

If VBA syntax is similar to Excel function, I think you can add a second parameter, ,2 to your HEX() function call to force it to use 2 characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文