Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Word to byte

Posted on 2006-11-09
Medium Priority
4,946 Views
Hi,

How to convert Word into array of bytes?
0
Question by:S_Warrior
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 17

Accepted Solution

TheRealLoki earned 100 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
0

LVL 26

Assisted Solution

Russell Libby earned 100 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

// Move data
Move(w, results, SizeOf(Word));
for i := Low(results) to high(results) do

// Cast data
for i:=0 to 1 do

end;
0

LVL 14

Expert Comment

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

LVL 27

Expert Comment

ID: 17912177
0

LVL 17

Expert Comment

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]);
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

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

ID: 21133170
Forced accept.

Computer101
0

## Featured Post

Question has a verified solution.

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

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â€¦
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usuaâ€¦
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on tâ€¦
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increasedâ€¦
###### Suggested Courses
Course of the Month10 days, 4 hours left to enroll