Solved

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

Posted on 2011-02-15
7
527 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 Covney
[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
  • 5
  • 2
7 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34901167
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:Ephraim Wangoya
ID: 34901213
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:
Ephraim Wangoya earned 500 total points
ID: 34902039
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
Independent Software Vendors: 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 32

Expert Comment

by:Ephraim Wangoya
ID: 34902086
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
 
LVL 3

Author Comment

by:Ed Covney
ID: 34902117
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
 
LVL 3

Author Closing Comment

by:Ed Covney
ID: 34902148
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:Ephraim Wangoya
ID: 34902153

Your welcome
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Math homework question 5 103
Homework Math Question 1 132
Delphi and Access based Enumeration 9 87
firemonkey Android Listview Sort items 7 98
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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