Jump to content

Recommended Posts

I have inherited some code for use on my project, and for the first time in my Delphi programming career, I am seeing a String value being copied (using Move) into a LongInt variable. 

 

The code follows:

var

 S: AnsiString;

 I: LongInt;

begin

 S := 'rCf';

 Move(S[1], I, Length(S));

In the code above, after the line with `Move` is executed, the variable I contains a numeric value. But .. I don't understand what operations are taking place on String and what the converted value represents.

 

I am guessing that some string conversion is taking place resulting in the bit values of the string's characters being written to the memory address of I, with LongInt in Delphi 7 being 4 bytes (or 32 bits) in length. But I'm not sure!

 

I would appreciate if any of the gurus could explain the operations or steps (algorithm) being carried out on the string that result in the number value?

 

TIA.

 

 

Edited by steve faleiro

Share this post


Link to post

It's copying three bytes of the ANSI encoded text into three bytes of the integer. The fourth byte of the integer is indeterminate, and could be anything. Just look up the ASCII table for those three letters, to see the ordinal values being assigned.  

 

It's hard to know why this would be done without seeing what happens next, e.g. to the integer variable.

Share this post


Link to post

Hi David,

Thanks for responding. So, the 24 significant bytes of the value in `I` will contain the numeric Ascii code values representing the string's characters. Ok great. Thanks! 

Share this post


Link to post
18 minutes ago, steve faleiro said:

So, the 24 significant bytes

Bits, not bytes.

I don't know the rest of the code, but I would suggest to initialize variable I to zero before the move so that the 4th byte is not at an unknown value.

Also note that this code is not portable across processor architecture. Intel and AMD processors are "little endian" while ARM processor is "little endian" by default but may be changed. No idea if Android and iOS change the default value.

 

To have predictable value and portable value, it is better to assemble the bytes your self.

The code you have may be replaced by:

I := Byte(S[1]) + ( Byte(S[2]) shl 8 )+ ( Byte(S[3]) shl 16 );

or event this, if the variable S is really a constant:

I := Byte('r') + ( Byte('C') shl 8 )+ ( Byte('f') shl 16 );

This latest expression is constant and once compiled result in only a single CPU instruction.

 

Share this post


Link to post
45 minutes ago, steve faleiro said:

Hi David,

Thanks for responding. So, the 24 significant bytes of the value in `I` will contain the numeric Ascii code values representing the string's characters. Ok great. Thanks! 

Bits not bytes. And not the most significant bits. The code runs on little endian machines. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×