Solved

Word to byte

Posted on 2006-11-09
8
4,603 Views
Last Modified: 2008-03-15
Hi,

How to convert Word into array of bytes?
0
Comment
Question by:S_Warrior
8 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 25 total points
ID: 17909234
just use Lo() and Hi()
eg.

  var
    w: word;
    i: integer;
    results: array[0..1] of byte;
  begin
    w := 12345;
    results[0] := lo(w);
    results[1] := hi(w);
    for i := Low(results) to high(results) do
      memo1.lines.add(inttostr(results[i]));
0
 
LVL 26

Assisted Solution

by:Russell Libby
Russell Libby earned 25 total points
ID: 17911260

You can also move the data, or just perform a simple cast of the word data.

Russell

-----------

eg:

var  w:       word;
     i:       integer;
     results: Array[0..1] of byte;
begin

  w:=12345;

  // Split high and low bytes
  results[0] := lo(w);
  results[1] := hi(w);
  for i := Low(results) to high(results) do
     memo1.lines.add(inttostr(results[i]));

  // Move data
  Move(w, results, SizeOf(Word));
  for i := Low(results) to high(results) do
     memo1.lines.add(inttostr(results[i]));

  // Cast data
  for i:=0 to 1 do
     memo1.lines.add(IntToStr(PByteArray(@w)^[i]));

end;
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 17912088
Another option is this:

type

  TWordBytes = record
    byte1, byte2: byte;
  end;

...
var ba: TWordBytes;
    w: word;
begin
  w:= 1000;
  ba:= TWordBytes(w);
  ShowMessage(IntToStr(ba.byte1)+', '+IntToStr(ba.byte2));
0
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.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 17912177
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 17912974
Or, the most interesting solution:

program Project3;
{$APPTYPE CONSOLE}
var
  W: Word;
  B: array[0..1] of byte absolute W;
begin
  W := $1234;
  WriteLn(B[0]);
  WriteLn(B[1]);
  ReadLn;
end.

Keep in mind that words are stored with the low-byte first. Thus B[0] is the low byte and B[1] is the high byte. The absolute directive just tells Delphi that B is located at exactly the same address as W. Thus, no need to assign one to another or do some typecasting...

The use of Lo() and Hi() -as suggested by others- does tend to be a bit more reliable though, since your Pascal code might one day be compiled for a processor that doesn't do Little-Ednian storage of data. (Intel uses Little-Endian, meaning that in memory, the low byte comes before the high byte.)
0
 
LVL 28

Expert Comment

by:2266180
ID: 21094900
note: sorry, but the points (5) were not enough to split between all 4 of you so I went with the first 2 solutions.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21133170
Forced accept.

Computer101
EE Admin
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
Delphi 2010 Export to pdf 2 287
Newbie Thread Programming 1 139
delphi exception 7 62
Multiple image collision 13 69
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 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

23 Experts available now in Live!

Get 1:1 Help Now