如何将十六进制数据(存储在字符串变量中)转换为整数值

How to convert hexadecimal data (stored in a string variable) to an integer value

编辑(摘要)

我试图将 Char/String 数据解释为 Byte,一次 4 个字节。这是因为我只能得到 TComport/TDatapacket 来将流数据解释为字符串,而不是任何其他数据类型。我仍然不知道如何让 Read 方法和 OnRxBuf 事件处理程序与 TComport 一起工作。

问题总结

我正在尝试使用一些 Delphi 代码从质谱仪 (MS) 获取数据。仪器通过串口线连接,遵循RS232协议。我能够毫无问题地发送命令和处理来自 MS 的基于文本的输出,但我在解释数据缓冲区时遇到了问题。

背景

来自本仪器的用户手册:

"With the exception of the ion current values, the output of the RGA are ASCII character strings terminated by a linefeed + carriage return terminator. Ion signals are represented as integers in units of 10^-16 Amps, and transmitted directly in hex format (four byte integers, 2's complement format, Least Significant Byte first) for maximum data throughput."

我不确定 (1) 十六进制数据是否可以正确存储在字符串变量中。我也不确定如何 (2) 在 Delphi 和 (3) 中首先实现 2 的补码。

按照@David Heffernan 的建议,我去修改了我的数据类型。尝试从字符中获取二进制数据是行不通的,因为并非 0-255 之间的所有值都可以正确表示。基本上,您会在此过程中丢失数据。特别是你的数据一次表示 4 个字节。

我的解决方案是使用 Async Professional 组件而不是 Denjan 的 Comport 库。它可以更好地处理数据流,并且有一个内置日志,我可以用它来弄清楚如何解释来自仪器的流式响应。它也有更好的记录。所以,如果您是串行通信的新手(就像我一样),不妨试一试。