Solved

# Convert an integer into 4 individual hex bytes

Posted on 2004-10-27
736 Views
Hi,

I need to convert an integer (which in delphi is 4 bytes) into 4 actual individual bytes.

For example the integer number 1 in 4 bytes would be \$00 \$00 \$00 \$01

but I actually need it as big endian so \$01 \$00 \$00 \$00

Any ideas?

Here is what I came up with myself, but its too long winded and doesn't work

procedure TMainForm.FourByteSplit(integer1: integer);
var
s1,s2,s3,s4,s5 : string;

begin
try
s5 := IntToHex(integer1,8); //converts the incoming integer into 8 digits of hex
s1 := '\$'+Copy(s5,0,2);     //copies each pair of hex into s1,s2,s3,s4
s2 := '\$'+Copy(s5,2,2);     //
s3 := '\$'+Copy(s5,4,2);     //
s4 := '\$'+Copy(s5,6,2);     //
byte1Out := StrToInt(s1);   //converts the first pair into an integer and assigns it to byte1out
byte2Out := StrToInt(s2);   //
byte3Out := StrToInt(s3);   //
byte4Out := StrToInt(s4);   //
except
on exception do
{nothing}
end;

Luke
end;
0
Question by:lukeMH

LVL 27

Assisted Solution

s1 := '\$'+Copy(s5,0,2);     //copies each pair of hex into s1,s2,s3,s4
s2 := '\$'+Copy(s5,2,2);     //
s3 := '\$'+Copy(s5,4,2);     //
s4 := '\$'+Copy(s5,6,2);     //

should be

s1 := '\$'+Copy(s5,1,2);     //copies each pair of hex into s1,s2,s3,s4
s2 := '\$'+Copy(s5,3,2);     //
s3 := '\$'+Copy(s5,5,2);     //
s4 := '\$'+Copy(s5,7,2);     //

meikl ;-)
0

LVL 7

Expert Comment

var
I:integer;
B:array[1..4] of byte absolute I;

I:=1; {integervalue};
B[1]  has 01
B[2]  has 00
B[3]  has 00
B[4]  has 00

Use them however you want:

//individual byte strings
s1='\$'+inttohex(b[1],2)

//flipped byte order
J:=(((((b[1]*256)+B[2])*256)+B[3])*256)+B[4]
0

Author Comment

Hi kretzschmar,

Do the characters of a string start at [1] then ... I thought it always starts at [0]

Kind Regards

Luke
0

LVL 7

Accepted Solution

If i dare elaborate on kretzschmar's comment  ;)

the problem arose because of mis-using the copy function - find some more info on it here

http://www.delphibasics.co.uk/RTL.asp?Name=Copy

The first character of a string has index = 1.

Up to Count characters are copied from the StartChar of the Source string to the returned string.

Less than Count characters if the end of the Source string is encountered before Count characters have been copied.

---------------------------------

www.delphibasics.co.uk is a great site for solving things like this

David
0

LVL 6

Expert Comment

function convertInt(src:Integer):Integer;
type
tArr = array [1..4] of byte;
pArr = ^tArr;
var
a, b: pArr;
i:integer;
begin
a := @src;
b := @result;
for i:=1 to 4 do
b[5-i] := a[i];
end;
0

LVL 17

Assisted Solution

procedure Split(Value: Integer);
var B1, B2, B3, B4: Byte;
begin
B1 := (Value and \$000000FF);
B2 := (Value and \$0000FF00) shr 8;
B3 := (Value and \$00FF0000) shr 16;
B4 := (Value and \$FF000000) shr 24;
end;

B1 is the lowest significant value and B4 the highest.

@LRHGuy. your solution would work reasonable good too, btw. But the problem is that it doesn't keep track of the byte-order used by the Intel/AMD processor. On the intel, the low-byte value comes first while on other processors, the high-byte value might come first. Your code will be okay since Delphi only runs on Intel/AMD systems anyway. But mine is just slightly better practice. ;-)
0

LVL 12

Expert Comment

I usualy create variant records .. for this case:

TFourBytes = record
case Integer of
0: (AsCardinal: Cardinal); // as a single cardinal var
1: (B1, B2, B3, B4: Byte);  // bytes
2: (R4, R2, R3, R1: Byte); // reversed bytes - just the names :)
end;

use it like:

var FB: TFourBytes;
...
FB.AsCardinal:=SomeCardinalValue;
do stuff with FB.B1 and others :)
0

LVL 27

Expert Comment

but also the other experts had good suggestions

do you not wanted to do a point split?
0

Author Comment

I meant to do that, but I think I slipped with the mouse or am just far too tired. I wanted to give workshop alex 50% for a nice clean way of doing it.

Kretzschmar, thanks, you were first in with the correct answer for spotting my mistake very quickly

Thankyou everyone else for your valid contributions.

Luke

0

Author Comment

Kind Regards

Luke
0

Author Comment

Arrgh! sorry gone wrong again. I swear I set the radio button against your reply for the accepted answer. Probably doesn't make any difference as I selected DavidBirch2dotCom, but he was explaining your answer, so ... anyone who searches for this post will get the right idea of what the solution for the problem was.

Right here is the final explaination of the points (and sorry for the mess-ups)

40points to kretzschmar who spotted my mistake very quickly and solved my problem.

20 points to DavidBirch2dotCom who explained the problem so I understood what I did wrong

40 points to Workshop_Alex who provided me with a cleaner alternative to what I wanted to achieve

Thanks to everyone

Luke
0

LVL 7

Expert Comment

you are right it doesnt matter statistics wise who gets "accepted answer" it still shows up as an "assisted answer" thanks for the points (you did give me what you wanted to btw)

David
0

LVL 17

Expert Comment

Thanks :-)
And don't worry, no one is perfect...
0

## Featured Post

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
how to add IIS SMTP to handle application/Scanner relays into office 365.