Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert an integer into 4 individual hex bytes

Posted on 2004-10-27
15
Medium Priority
?
913 Views
Last Modified: 2010-04-05
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
Comment
Question by:lukeMH
[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
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +4
15 Comments
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 160 total points
ID: 12423286
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
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12423391
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
 

Author Comment

by:lukeMH
ID: 12423640
Hi kretzschmar,

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

Kind Regards

Luke
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
DavidBirch2dotCom earned 80 total points
ID: 12423785
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

by:vadim_ti
ID: 12424707
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

by:Wim ten Brink
Wim ten Brink earned 160 total points
ID: 12426048
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

by:Lee_Nover
ID: 12430378
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

by:kretzschmar
ID: 12435524
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
 

Author Comment

by:lukeMH
ID: 12435589
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

by:lukeMH
ID: 12442056
Yes please unaccept

Kind Regards

Luke
0
 

Author Comment

by:lukeMH
ID: 12442348
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

by:DavidBirch2dotCom
ID: 12442369
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

by:Wim ten Brink
ID: 12447051
Thanks :-)
And don't worry, no one is perfect...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

636 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