如何将字节数组转换为Variant [英] How to convert Array of bytes to Variant

查看:224
本文介绍了如何将字节数组转换为Variant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字节数组转换为Variant?
我有一个WebService应该接收一个字节数组,但它只接受类型为VARIANT的变量,我不知道如何转换为了将其作为Web服务的参数传递。



谢谢

解决方案

根据评论踪迹,您需要创建一个 SAFEARRAY 的字节。这是在Delphi中完成的:

  V:= VarArrayCreate([0,N-1],varByte); 

或者,如果 SAFEARRAY 需要1-基于索引:

  V:= VarArrayCreate([1,N],varByte); 

然后可以使用 V [i]在循环中填充数组: = ...



如果您有一个Delphi动态数组的字节并且预期的 SAFEARRAY 使用基于0的索引,那么可以简单地写:

  V:= a; 

如果你有很多数据要传输,那么元素的元素戳数据就是RTL报价几乎没有希望。即使简单的 v = = a 方法也会导致元素复制,这对大量数据来说将是非常缓慢的。



在你的位置,我会一'd而就的。像这样:

  var 
i:整数;
a:字节数组;
V:变体;
SafeArray:PVarArray;
....
//填充
V:= VarArrayCreate([0,high(a)],varByte);
SafeArray:= VarArrayAsPSafeArray(V);
Move(指针(a)^,SafeArray.Data ^,Length(a)* SizeOf(a [0]));

或者,如果您需要使用基于1的索引:

  V:= VarArrayCreate([1,Length(a)],varByte); 
SafeArray:= VarArrayAsPSafeArray(V);
Move(指针(a)^,SafeArray.Data ^,Length(a)* SizeOf(a [0]));


How to convert a byte array to Variant? I have a WebService that should receive an array of byte, but it only accepts variable of type VARIANT, I wonder how to convert in order to pass it as parameter for Web Services.

thank you

解决方案

According to the comment trail, you need to create a SAFEARRAY of bytes. Which is done like this in Delphi:

V := VarArrayCreate([0, N-1], varByte);

Or, if the SAFEARRAY needs 1-based indexing:

V := VarArrayCreate([1, N], varByte);

You can then populate the array in a loop using V[i] := ....

If you have a Delphi dynamic array of Byte, and the expected SAFEARRAY uses 0-based indexing, then you can simply write:

V := a;

If you have a lot of data to transfer then the element by element poking of the data that the RTL offers is pretty much hopeless. Even the simple v := a approach results in element by element copying which will be horribly slow for large amounts of data.

In your position, I'd blit the array in one go. Like this:

var
  i: Integer;
  a: array of Byte;
  V: Variant;
  SafeArray: PVarArray;
....
// populate a
V := VarArrayCreate([0,high(a)], varByte);
SafeArray := VarArrayAsPSafeArray(V);
Move(Pointer(a)^, SafeArray.Data^, Length(a)*SizeOf(a[0]));

Or, if you need to use 1-based indexing:

V := VarArrayCreate([1,Length(a)], varByte);
SafeArray := VarArrayAsPSafeArray(V);
Move(Pointer(a)^, SafeArray.Data^, Length(a)*SizeOf(a[0]));

这篇关于如何将字节数组转换为Variant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆