1. Home
  2. Computing & Technology
  3. Delphi Programming

How to Convert a String of Integers into an Array of Byte

By , About.com Guide

The best solution is to dynamically create an array of byte that has length equal to that of the string. Once you have your array you can fill the array with the values from the string, however there is some offset since the ascii representation of the character '1' isn't equivalent to 1. Below is a sample of how one might return an array of byte:

~~~~~~~~~~~~~~~~~~~~~~~~~
interface
uses
   ...
type
   //dynamic array type for Array of Byte
   TByteArr = array of byte;

   ...
implementation

function ArrOfByte(AStr: String): TByteArr;
var
   j: integer;
begin
   SetLength( Result, Length(AStr)) ;
   for j := 0 to Length(AStr) - 1 do
     Result[j] := ord(AStr[j + 1]) - 48;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Connect and disconnect to/from the Internet
« How to store a TDateTime in the Registry

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2002 Delphi Tips
  7. How to Convert a String of Integers into an Array of Byte

©2009 About.com, a part of The New York Times Company.

All rights reserved.