将[字节数组]分配给没有Unicode转换的Variant [英] Assign [array of byte] to a Variant with no Unicode conversion
问题描述
考虑以下代码段(在Delphi XE2中):
Consider the following code snippet (in Delphi XE2):
function PrepData(StrVal: string; Base64Val: AnsiString): OleVariant;
begin
Result := VarArrayCreate([0, 1], varVariant);
Result[0] := StrVal;
Result[1] := Base64Val;
end;
Base64Val
是一个二进制值,编码为Base64(因此没有null
字节). (OleVariant
)结果将自动编组并在客户端应用程序和DataSnap服务器之间发送.
Base64Val
is a binary value encoded as Base64 (so no null
bytes). The (OleVariant
) Result is automatically marshalled and sent between a client app and a DataSnap server.
当我使用Wireshark捕获流量时,我看到StrVal
和Base64Val
均作为Unicode字符串传输.如果可以,我想避免Base64Val
的Unicode转换.我已经看过所有Variant
类型,除了varString
之外没有其他可以传输字符数组的东西.
When I capture the traffic with Wireshark, I see that both StrVal
and Base64Val
are transferred as Unicode strings. If I can, I would like to avoid the Unicode conversion for Base64Val
. I've looked at all the Variant
types and don't see anything other than varString
that can transfer an array of characters.
我发现了这个问题,该问题显示了如何创建字节的变体数组.我在想可以使用此技术代替使用AnsiString
.不过,我很好奇,是否有另一种方法可以将非Unicode字符数据数组分配给Variant,而无需转换为Unicode字符串?
I found this question that shows how to create a variant array of bytes. I'm thinking that I could use this technique instead of using an AnsiString
. I'm curious though, is there another way to assign an array of non-Unicode character data to a Variant without a conversion to a Unicode string?
推荐答案
Delphi的实现支持使用自定义变量类型代码将AnsiString和UnicodeString存储在Variant中.这些代码是varString和varUString.
Delphi's implementation supports storing AnsiString and UnicodeString in a Variant, using custom variant type codes. These codes are varString and varUString.
但是互操作通常会使用标准OLE变体,并且OLE字符串varOleStr是16位编码的.这似乎就是您进行观察的原因.
But interop will typically use standard OLE variants and the OLE string, varOleStr, is 16 bit encoded. That would seem to be the reason for your observation.
如果您希望避免转换为16位文本,则需要将数据作为字节数组放置.这样做会使base64编码变得毫无意义.停止对有效负载进行base64编码,然后将二进制文件发送到字节数组中.
You'll need to put the data in as an array of bytes if you do wish to avoid a conversion to 16 bit text. Doing so renders base64 encoding pointless. Stop base64 encoding the payload and send the binary in a byte array.
这篇关于将[字节数组]分配给没有Unicode转换的Variant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!