[英]I need the equivalent code for Delphi 5/6
下面是将字符串数组转换为字节数组的代码。 我正在尝试使用WebService获取二进制文件。 它以字符串形式返回文件的内容,我必须将其更改为字节数组。.我已经能够通过以下代码在C#中做到这一点,但在Delphi 5中却没有。
str是一些字符串。 我已经用定界符','进行了拆分。
string[] byteArrayStr = str.Split(Convert.ToChar(","));
sbyte[] sbyteArray = new sbyte[byteArrayStr.Length];
for(int i=0;i<sbyteArray.Length ;i++)
{
sbyteArray[i] = SByte.Parse(byteArrayStr.GetValue(i).ToString());
}
byte[] byteArray = new byte[sbyteArray.Length];
Buffer.BlockCopy(sbyteArray, 0, byteArray, 0, sbyteArray.Length);
return byteArray;
Ajay,如果您收到带有,
分隔符的Ansi字符串,则可以使用StringReplace
函数删除此char。
YourNewString:=StringReplace(YourOriginalString,',','',[rfReplaceAll]);
从这里您可以访问新字符串的元素,就像任何基于1索引的数组一样。
如果您出于其他原因将这些字符串转换为字节数组,可以编写这样的函数;
type
TLongByteArray= array of Byte;
function StringToByteArray(InString:string):TLongByteArray;
Var
Size : Integer;
begin
//remove the ,
InString:=StringReplace(InString,',','',[rfReplaceAll]);
Size:=length(InString);
//set the length of the resulting array of bytes
SetLength(Result,Size);
Move(InString[1],Result[0],Size);
end;
使用样本
var
L : TLongByteArray;
s : String;
begin
s:='A,B,C,D,E';
L:=StringToByteArray(s);
uses JclStringLists;
function StringListToList(List: IJclStringList): TIntegerDynArray;
var
I: Integer;
begin
SetLength(Result, List.Count);
for I := 0 to List.LastIndex do Result[I] := StrToInt(List[I]);
end;
function StrToIntegerList(const List: string): TIntegerDynArray;
begin
Result := StringListToList(
JclStringList.ExtractWords(List, [',']).DeleteBlanks.KeepIntegers
);
end;
var
MyIntegerArray: TIntegerDynArray;
begin
MyIntegerArray := StrToIntegerList('1,5,6,7,1,23,123,12,9');
end.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.