Solved

How do I convert a TString to TBytes in Delphi 2009?

Posted on 2009-07-16
5
4,286 Views
Last Modified: 2012-05-07
Hi,

Can anyone tell me how to convert a TString (of known length) into TBytes?

Thanks,

James
0
Comment
Question by:jatkin
5 Comments
 
LVL 14

Accepted Solution

by:
SteveBay earned 125 total points
ID: 24872192
This link explains how:
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_10140367.html
but because D2009 uses Wide Strings this may not work exactly right.
This is a link containing a discussion about Wide Strings vs. Regular Strings.
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_20680402.html
0
 
LVL 2

Assisted Solution

by:preppydude
preppydude earned 125 total points
ID: 24873673
What's the definition of TString? I've got D2009, too, and I can't find it anywhere. I found some examples at about.com using it, though. So if it's the same, try the following.

Anyways, I hope it works out for you. If not, tell me where I can find that string object you're talking about. :)
function StringToByteArray(Str: String): TBytes; overload;

begin

  Result := TBytes(Str);

end;
 

function StringToByteArray(Str: TString): TBytes; overload;

begin

  Result := TBytes(Str.Str);

end;
 

function StringToByteArray(Str: TStrings): TBytes; overload;

begin

  Result := TBytes(Str.GetText);

end;

Open in new window

0
 
LVL 8

Assisted Solution

by:thiagoblimeira
thiagoblimeira earned 125 total points
ID: 24874254
Here is what you need, the form and the code:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 214
  ClientWidth = 231
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Convert: TButton
    Left = 8
    Top = 181
    Width = 75
    Height = 25
    Caption = 'Convert'
    TabOrder = 0
    OnClick = ConvertClick
  end
  object Memo1: TMemo
    Left = 8
    Top = 8
    Width = 217
    Height = 167
    Lines.Strings = (
      'This is a test')
    TabOrder = 1
  end
end

unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Convert: TButton;
    Memo1: TMemo;
    procedure ConvertClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ConvertClick(Sender: TObject);
var
Bytes: TBytes;
i: integer;
begin
  Caption := '';
  SetLength(Bytes,length(Memo1.Lines[0]));
  for i := 0 to length(Memo1.Lines[0]) - 1 do
    begin
    Bytes[i] := Ord(Memo1.Lines[0][i+1]);
    Caption := Caption + Chr(Bytes[i]);
    end;
end;

end.

0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 125 total points
ID: 24875236
you probably are using some indy components

look in the IdGlobal unit for conversion functions
(you may need to specify the encoding for the bytes to string)

unit Unit3;
 

interface
 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, IdUDPServer, IdBaseComponent, IdComponent, IdUDPBase,

  IdUDPClient, IdGlobal;
 

type

  TForm3 = class(TForm)

    IdUDPClient1: TIdUDPClient;

    IdUDPServer1: TIdUDPServer;

    Button1: TButton;

    Memo1: TMemo;

    Memo2: TMemo;

    procedure Button1Click(Sender: TObject);

    procedure IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; AData: TBytes;

      ABinding: TIdSocketHandle);

  end;
 

var

  Form3: TForm3;
 

implementation
 

{$R *.dfm}
 

procedure TForm3.Button1Click(Sender: TObject);

begin

  with idUdpClient1 do

    Binding.Send(ToBytes(Memo1.Text);

end;
 

procedure TForm3.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread;

  AData: TBytes; ABinding: TIdSocketHandle);

begin

  Memo2.Lines.Add(BytesToString(AData));

end;
 

end.

Open in new window

0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 31604170
4 great answers!!!! Thanks.
Hope you are ok with splitting the points?
Best regards,
James
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
proper way to parse text with delphi 7 101
Printing problem 2 80
tvirtualstringtree freeze when load too manny images 10 52
Firemonkey android show image from resource ? 1 33
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

15 Experts available now in Live!

Get 1:1 Help Now