Link to home
Start Free TrialLog in
Avatar of abdate
abdateFlag for Taiwan, Province of China

asked on

How to transfer from string to byte array

Hi,All!

I try to transfer from string to Byte then send to a device, but I don't exactly know how to do it. My previous codeing as followss:

procedure TForm1.Button1Click(Sender: TObject);
var
    mBuffer:array[0..100] of Byte;
    m1:String;
    s1:char;
    i:integer;
begin
  m1:='Hello,World!'
  for i:=0 to lenght(m1) do
  begin
     s1:= copy(m1,i,1);                {Has a exception code here.
                                                  I don't know how to code. It should be some function to
                                                 transfer from string to char}
     mBuffer[i]:=ORD(s1);
  end;
end;

Thanks all.
Abdate
Avatar of LRHGuy
LRHGuy

Try:

Move(M1[1],MBuffer[0],length(M1));

or

Move(M1[0],MBuffer[0],length(M1)+1);
If the buffer is not changed by the call to the function then a PByte(PChar(m1)) should be good enough.
you might be interested in www.delphibasics.co.uk

lots of info on delphi data types and conversions - essential reading/refereance
ASKER CERTIFIED SOLUTION
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
procedure TForm1.Button1Click(Sender: TObject);
var
  mBuffer:array of Byte;
  m1:     string;
  I:      Integer;
  L:      Integer;
begin
  m1 := 'Hello,World!';
  L := Length(m1);
  SetLength(mBuffer, L);
  for I := 0 to L-1 do
  begin
    mBuffer[I]:=ORD(m1[I+1]);
    ListBox1.Items.Add(Char(mBuffer[I]));
  end;
end;
Avatar of abdate

ASKER

Sorry I don't know how to split points to LRHGuy.
Although his answer is good too.
[kretzschmar],
please split the points to [LRHGuy] and [esoftbg] as [abdate] wants to do it.
Emil
i just copy and paste the author's comment:

Comment from abdate
Date: 10/25/2004 12:33AM EEST                                                                                                     Author Comment  

Sorry I don't know how to split points to LRHGuy.
Although his answer is good too

//........
So, his desire it was to split the points, but he did not know how ....