将 VARIANT 转换为无符号字符数组
我使用 Comm32(一个 ActiveX 控件)通过 GetInput() 方法获取通过串行端口发送的数据。 此方法返回一个 _variant_t,因此我需要将其转换为无符号字符数组。
当串行端口配置为 TextMode(这是数据输入被解释为字符串)时,将 Variant 转换为 BSTR,然后转换为 CString 按预期工作(代码不是我写的):
_variant_t x = m_comm32.GetInput();
CString cs = x.bstrVal;
但是,由于通过串行端口发送的数据实际上是二进制数据,当配置 comm32 将数据解释为二进制时,上面的示例代码没有给我有效的数据。 所以我想我需要将其转换为另一种格式(也许是无符号字符数组?这是我想测试的东西)。
我的问题是我快疯了,因为我找不到任何有关如何从 VARIANT 结构转换数据的详细信息。如何将变体转换为无符号字符数组?
I'm using Comm32 (an activeX control) to get data coming through serial port using the method GetInput().
This method returns a _variant_t, so I need to convert it to an array of unsigned char's.
When serial port is configured to TextMode (this is data input is interpreted as String), converting the Variant to a BSTR and then to a CString works as expected (code not writed by me):
_variant_t x = m_comm32.GetInput();
CString cs = x.bstrVal;
However, as data sent through serial port is actually binary data, when configuring comm32 to interpret data as binary the sample code above doesn't give me the valid data.
So I think I need to convert it to another format (unsigned char array maybe? that's somehting I want to test).
My problem is I'm getting crazy as I cannot find any detailed info about how to convert data from a VARIANT structure. How can I convert the variant to an unsigned char array??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
InputMode 属性确定使用 Input 属性检索的数据类型。如果InputMode 设置为comInputModeText,则Input 属性返回Variant 形式的文本数据。如果InputMode 为comInputModeBinary,则Input 属性返回Variant 字节数组中的二进制数据。 (来自 msdn)
了解如何从 safearray 获取数据请参见示例 sample1 或 示例2
The InputMode property determines the type of data that is retrieved with the Input property. If InputMode is set to comInputModeText then the Input property returns text data in a Variant. If InputMode is comInputModeBinary then the Input property returns binary data in an array of bytes in a Variant. (from msdn)
To understand how to get data from the safearray see examples sample1 or sample2