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

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

By Zarko Gajic, About.com

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

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

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

Easy ways to connect two computers for networking purposes. 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.