• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1199
  • Last Modified:

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(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;




Thanks for your time

Luke
end;
0
lukeMH
Asked:
lukeMH
  • 4
  • 2
  • 2
  • +4
3 Solutions
 
kretzschmarCommented:
usual your problem is here

    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
 
LRHGuyCommented:
How about:


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
 
lukeMHAuthor Commented:
Hi kretzschmar,

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

Kind Regards

Luke
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
DavidBirch2dotComCommented:
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
 
vadim_tiCommented:
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
 
Wim ten BrinkSelf-employed developerCommented:
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
 
Lee_NoverCommented:
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
 
kretzschmarCommented:
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?
0
 
lukeMHAuthor Commented:
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
 
lukeMHAuthor Commented:
Yes please unaccept

Kind Regards

Luke
0
 
lukeMHAuthor Commented:
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
 
DavidBirch2dotComCommented:
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
 
Wim ten BrinkSelf-employed developerCommented:
Thanks :-)
And don't worry, no one is perfect...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now