Solved

If there a quick way to convert a cardinal value into 4, 8-bit bytes?

Posted on 2011-02-15
7
518 Views
Last Modified: 2012-05-11
I would like to be able to break apart, unsigned 32-bit integer values into 4 "byte" values of 8-bits each. (For best viewing please paste below into notepad and use courier new font).

Example: cardinal value is:  

    0 1 0 0 1 0 1 0   0 1 0 1 1 0 1 0   0 1 0 1 0 0 1 1   1 1 0 1 0 1 0 0    (1247433684 dec)

and I like to break it apart into 4 8-bit bytes:

    0 1 0 0 1 0 1 0  ( 74)
    0 1 0 1 1 0 1 0  ( 90)
    0 1 0 1 0 0 1 1  ( 83)
    1 1 0 1 0 1 0 0  (212)

I will ask a follow-on question to re-combine the 4 bytes into 1 cardinal value the most direct way (without multipying?).

TIA,  Ed
0
Comment
Question by:Ed-SSA
  • 5
  • 2
7 Comments
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility
type cast to ByteArray


var
  X: cardinal;
  I, J: Byte;
begin
  X := 1247433684;
  I := PByteArray(PCardinal(@X))[0];
  J := PByteArray(PCardinal(@X))[1]; 
  ShowMessage(IntToStr(I) + '   ' + IntToStr(I) );

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility
You can add IntToBinary function
function IntToBinary(Int: Integer; Digits: Byte): string;
const
  CONVERT:array[0..1] of Char = '01';
var
  cnt: Integer;
begin
  SetLength(Result, Digits);
  for cnt := Digits downto 1 do
  begin
    Result[cnt] := CONVERT[Abs(Int) mod 2];
    Int := Int shr 1;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  X: cardinal;
  I: Byte;
begin
  X := 1247433684;
  I := PByteArray(PCardinal(@X))[0];
  ShowMessage(IntToBinary(I, 8));
end;

Open in new window

0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
Comment Utility
Here is a better example

function IntToBinary(Int: Integer; Digits: Byte): string;
const
  CONVERT:array[0..1] of Char = '01';
var
  Cnt: Integer;
begin
  SetLength(Result, Digits);
  for Cnt := Digits downto 1 do
  begin
    Result[Cnt] := CONVERT[Abs(Int) mod 2];
    Int := Int shr 1;
  end;
end;

procedure TForm3.Button1Click(Sender: TObject);
var
  I: Integer;
  BArray: array[0..3] of Byte;
  Value: Cardinal;
  TempStr: string;
begin
  Value := 1247433684;
  for I := 0 to 3 do
    BArray[I] := PByteArray(PCardinal(@Value))[I];

  //display
  for I := 0 to 3 do
    TempStr := TempStr + IntToBinary(BArray[I], 8) +  ' - ' + IntToStr(BArray[I]) + #13#10;
  ShowMessage(TempStr);
end;

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 32

Expert Comment

by:ewangoya
Comment Utility
To convert the bytes back to cardinal, just do the reverse
procedure TForm3.Button2Click(Sender: TObject);
var
  BArray: array[0..3] of Byte;
  Value: Cardinal;
begin
  BArray[0] := 212;
  BArray[1] := 83;
  BArray[2] := 90;
  BArray[3] := 74;

  Value := PCardinal(PByteArray(@BArray[0]))^;
  ShowMessage(IntToStr(Value));
end;

Open in new window

0
 

Author Comment

by:Ed-SSA
Comment Utility
wow, wow, wow.
Other the begin and end, I don't understand one line of your function code, but it works and works so darn well, I just can't believe what I'm seeing here.  I had to do 1,000,000,000 iterations before I noticed about a 3 second pause. That's over 1,000,000,000 conversions per second. And I'm using a 2.6 GHz PC, not exactly the fastest on the block . . . or even in the room!!!

ewangoya - I just can't thank you enough. Thank you, thank you, thank you . . . . .

Ed

var
  X: cardinal;
  I, J, K, L: Byte;
begin
  X := 1247433524;
  for x := 0147433524 to 1247433524 do begin
     I := PByteArray(PCardinal(@X))[0];
     J := PByteArray(PCardinal(@X))[1];
     K := PByteArray(PCardinal(@X))[2];
     L := PByteArray(PCardinal(@X))[3];
  end;
  ShowMessage(IntToBinary(I,8)+#13#10+IntToBinary(J,8)+#13#10+IntToBinary(K,8)+#13#10+IntToBinary(L,8) );
  ShowMessage(IntToStr(I)+IntToStr(J)+IntToStr(K)+IntToStr(L) );
  ShowMessage(Chr(I)+Chr(J)+Chr(K)+Chr(L) );

Open in new window

0
 

Author Closing Comment

by:Ed-SSA
Comment Utility
The solution involved Assembler, which I'll never understand but I will ALWAYS appreciate. Without it, my code would be an order of magnitude (or more) slower.

EWANGOYA rocks !!
0
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility

Your welcome
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned. This example is designed t…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now