开发者

I need the equivalent code for Delphi 5/6

There is the code below converting String Array to Byte Array. I am trying to use a WebService to get a binary file. It returns the content of file in string, and i have to c开发者_如何学JAVAhange it to byte array.. I have been able to do it by the code below in C#, but not in Delphi 5.. Ple

str is some string. And i have splited it by delimiter ','.

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, if you are receiving an Ansi string with a , delimiter you can remove this char using the StringReplace function.

 YourNewString:=StringReplace(YourOriginalString,',','',[rfReplaceAll]);

from here you can access the elements of the new string just like any array based in 1 index.

if you want convert these string to an array of bytes for any other particular reason you can write a function like this ;

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;

a sample of use

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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜