Solved

How to transfer from string to byte array

Posted on 2004-10-24
7,838 Views
Last Modified: 2010-07-27
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
0
Question by:abdate
    8 Comments
     
    LVL 7

    Expert Comment

    by:LRHGuy
    Try:

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

    or

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

    Expert Comment

    by:robert_marquardt
    If the buffer is not changed by the call to the function then a PByte(PChar(m1)) should be good enough.
    0
     
    LVL 7

    Expert Comment

    by:DavidBirch2dotCom
    you might be interested in www.delphibasics.co.uk

    lots of info on delphi data types and conversions - essential reading/refereance
    0
     
    LVL 12

    Accepted Solution

    by:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      mBuffer:array[0..100] of Byte;
      m1:String;
      i:integer;
    begin
      m1:='Hello,World!'
      for i:=1 to lenght(m1) do
      begin
         mBuffer[i]:=ORD(m1[i]);
      end;
    end;
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    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;
    0
     

    Author Comment

    by:abdate
    Sorry I don't know how to split points to LRHGuy.
    Although his answer is good too.
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    [kretzschmar],
    please split the points to [LRHGuy] and [esoftbg] as [abdate] wants to do it.
    Emil
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    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 ....
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    delphi Login Form 15 107
    Virtualstringtree carsh after invalidate for while 5 58
    proper way to parse text with delphi 7 64
    code issue 8 59
    A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
    In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
    This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now