lukeMH
asked on
Convert an integer into 4 individual hex bytes
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(in teger1: 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;
Thanks for your time
Luke
end;
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(in
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;
Thanks for your time
Luke
end;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi kretzschmar,
Do the characters of a string start at [1] then ... I thought it always starts at [0]
Kind Regards
Luke
Do the characters of a string start at [1] then ... I thought it always starts at [0]
Kind Regards
Luke
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
function convertInt(src:Integer):In teger;
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;
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;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:=SomeCardina lValue;
do stuff with FB.B1 and others :)
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:=SomeCardina
do stuff with FB.B1 and others :)
thanks for the grade, glad to helped you ;-)
but also the other experts had good suggestions
do you not wanted to do a point split?
but also the other experts had good suggestions
do you not wanted to do a point split?
ASKER
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
Kretzschmar, thanks, you were first in with the correct answer for spotting my mistake very quickly
Thankyou everyone else for your valid contributions.
Luke
ASKER
Yes please unaccept
Kind Regards
Luke
Kind Regards
Luke
ASKER
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
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
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
David
Thanks :-)
And don't worry, no one is perfect...
And don't worry, no one is perfect...
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])*25