Recent

Author Topic: Byte Manipulation  (Read 15758 times)

flywire

  • Jr. Member
  • **
  • Posts: 85
Byte Manipulation
« on: February 08, 2015, 02:15:48 pm »
How do I rewrite this delphi code for lazurus using lo:

buffer[1]:=LoByte(LoWord(TOCfilesCount));
...
buffer[4]:=HiByte(HiWord(TOCfilesCount));

Where buffer is a byte array and TOCfilesCount is a LongInt.
« Last Edit: February 08, 2015, 02:22:21 pm by flywire »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Byte Manipulation
« Reply #1 on: February 08, 2015, 02:41:30 pm »
Code: [Select]
  PLongInt(@buffer[1])^ := TOCfilesCount;

balazsszekely

  • Guest
Re: Byte Manipulation
« Reply #2 on: February 08, 2015, 02:43:01 pm »
If you are on windows just add windows to the uses clauses and you're good to go.

Code: [Select]
uses windows;
//...
   buffer[1]:=LoByte(LoWord(TOCfilesCount));           
   buffer[4]:=HiByte(HiWord(TOCfilesCount));
//...

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Byte Manipulation
« Reply #3 on: February 08, 2015, 02:50:03 pm »
The OP wants buffer[2] and buffer[3] as well.

Awkward

  • Full Member
  • ***
  • Posts: 154
Re: Byte Manipulation
« Reply #4 on: February 08, 2015, 03:00:37 pm »
as alternative:

type
  tTempRecord = record
    case boolean of
      true: (a:longint);
      false: (b:array [0..3] of byte);
  end;

and in code:
  buffer[1]:=tTempRecord(TOCfilesCount).b[0];

as example. no?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Byte Manipulation
« Reply #5 on: February 08, 2015, 03:13:20 pm »
Yes, sure!

I wonder if the OP is trying to implement BEtoN.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12857
  • FPC developer.
Re: Byte Manipulation
« Reply #6 on: February 08, 2015, 05:38:32 pm »
Code: [Select]
  PLongInt(@buffer[1])^ := TOCfilesCount;


Note that this is not entirely equivalent, for some architectures throw exceptions when you access 32-bits values that are not aligned. In such case the former works.

Putting
Code: [Select]
unaligned(  PLongInt(@buffer[1])^) := TOCfilesCount;

Might work too

http://www.freepascal.org/docs-html/ref/refse73.html

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Byte Manipulation
« Reply #7 on: February 08, 2015, 06:30:41 pm »
I'm not familiar with unaligned, it makes perfect sense. Thanks!

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12857
  • FPC developer.
Re: Byte Manipulation
« Reply #8 on: February 08, 2015, 07:34:20 pm »
I'm not familiar with unaligned, it makes perfect sense. Thanks!

I know you are a more intermediate user who gives a lot of support, so I just thought I mention it so you have at least heard of it once. I also mostly know it exists, and am not doing it routinely correct myself.

That can be easily seen because my use (written from memory) is different from the manual, the doc seem to indicate that you need to put unaligned over the pointer (the @ expression). At least according to docs.

In general even large parts of packages/ are not ready for this.

« Last Edit: February 08, 2015, 10:01:37 pm by marcov »

flywire

  • Jr. Member
  • **
  • Posts: 85
Re: Byte Manipulation
« Reply #9 on: February 08, 2015, 09:34:19 pm »
If you are on windows just add windows to the uses clauses and you're good to go.

Thanks all ... we have a winner! [Where is the emoticon for remove hat and take a bow?]

BeniBela

  • Hero Member
  • *****
  • Posts: 959
    • homepage
Re: Byte Manipulation
« Reply #10 on: February 08, 2015, 10:13:01 pm »
On which platforms does this matter?

ARM?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12857
  • FPC developer.
Re: Byte Manipulation
« Reply #11 on: February 08, 2015, 10:19:05 pm »
On which platforms does this matter?

ARM?

Older ARMs, and some of the more embedded ARMs inherit this.

Nitorami

  • Hero Member
  • *****
  • Posts: 605
Re: Byte Manipulation
« Reply #12 on: February 09, 2015, 11:56:41 am »
Is there a specific reason why we would not take over over the code directly, just replacing "LoByte" etc. by Lo and Hi ?

Code: [Select]
 
  buffer[1] := Lo(Lo(TOCFilesCount));
  buffer[2] := Lo(Hi(TOCFilesCount));
  buffer[3] := Hi(Lo(TOCFilesCount));
  buffer[4] := Hi(Hi(TOCFilesCount));

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12857
  • FPC developer.
Re: Byte Manipulation
« Reply #13 on: February 09, 2015, 12:15:52 pm »
IIRC some of those are overloaded, which makes it easy to make errors. I avoid them for that reason.

iword and loword etc are Windows API macros. Both FPC and Delphi probably have them in unit Windows.

flywire

  • Jr. Member
  • **
  • Posts: 85
Re: Byte Manipulation - Delphi code Conversion Bug
« Reply #14 on: February 11, 2015, 01:54:37 pm »
If you are on windows just add windows to the uses clauses and you're good to go.

Code: [Select]
uses windows;
//...
   buffer[1]:=LoByte(LoWord(TOCfilesCount));           
   buffer[4]:=HiByte(HiWord(TOCfilesCount));
//...

There seems to be a bug with the Delphi conversion tool. The original Delphi code (too large to load) has 'windows' in the uses clauses but when I convert it the 'windows' clause is removed leading to an error when I compile it. When I reenter it then the code compiles without error. One for the development team?

 

TinyPortal © 2005-2018