Help needed rewriting some bitshift code

Dear All,

Can someone please help me in converting this Delphi/Pascal code to C#
or comment all the code so i can read it and convert it myself ?
I need to write the reverse of this function too

--
type
My_real = Array [1..4] of Byte;

Function Ms_to_Ie(Var ms) : Real;
  Var
    ie : Array [1..6] of Byte Absolute r;
    m    : My_real Absolute ms;
    r    : Real;
begin
  FillChar(r, sizeof(r), 0);
  ie[1] := m[4];
  ie[4] := m[1];
  ie[5] := m[2];
  ie[6] := m[3];
  Ms_to_ie := r;
end;  
--

Thanks for any help
Brian
LVL 44
bruintjeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NicoLaanCommented:
Original thing is a 4 bytes object:
A B C D
This is transformed to a new type of object using 6 bytes:
A B C D E F

The mapping of the old object on the new is like this:
D 0 0 A B C
0
Pierre CorneliusCommented:
Not sure about the C# syntax/coding but here's my rundown of the function:


Interpretation
=========
Lets say you have the following bytes in memory:
  SomeAddress:  1,2,3,4,5,6

Calling the Ms_to_Ie function will return the follow:
  SomeAddress:  4,2,3,1,2,3


Commented code:
===========

type
//decalre a new array type called My-real containg 4 byte values indexed from 1 to 4
My_real = Array [1..4] of Byte;

Function Ms_to_Ie(Var ms) : Real; //function declaration accepting address of parameter (pointer value) returning variable of type real
  Var
    //declare local variable ie as array type containg 6 byte values indexed
    //  from 1 to 6 at the same memory address as the variable r;
    ie : Array [1..6] of Byte Absolute r;

    //declare new local variable m of type My_real at same address as parameter ms
    m    : My_real Absolute ms;

    //declare local variable r as type real; should be above ie declaration to avoid compilation error
    r    : Real;
begin
  //fill variable r with 0; depending on the version, this could be 6 or 8 bytes
  FillChar(r, sizeof(r), 0); //essentially same as r:= 0;

  ie[1] := m[4]; //do byte setting/swapping as explained above under interpretation
  ie[4] := m[1]; //do byte setting/swapping as explained above under interpretation
  ie[5] := m[2]; //do byte setting/swapping as explained above under interpretation
  ie[6] := m[3]; //do byte setting/swapping as explained above under interpretation

  Ms_to_ie := r; //set return value equal to r
end;  

Regards
Pierre
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pierre CorneliusCommented:
sorry, correction:
 
"Calling the Ms_to_Ie function will return the follow:
  SomeAddress:  4,2,3,1,2,3"

Should be
"Calling the Ms_to_Ie function will return the follow:
  SomeAddress:  4,0,0,1,2,3"
0
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

bruintjeAuthor Commented:
thanks both

@PierreC, when you say at the same memory address as the variable x what does that do?

are you declaring a new variable pointing at the same address as an existing variable? if so why do you want to do that?

sorry for asking all this but it would help me understand whats happening here, thanks
0
atul_parmarCommented:
It is data type conversion routine. what will convert a data type of 4 byte (e.g. single or any other type of equal length) to real (6 byte).

type
My_real = Array [1..4] of Byte; // new user defined data type

Function Ms_to_Ie(Var ms) : Real;
  Var
    r    : Real; // this line must be here
    ie : Array [1..6] of Byte Absolute r; // declared an array of byte what points to the address location of r (above declared variable)
    m    : My_real Absolute ms; // m points to the address location of ms (the parameter passed)
begin
  FillChar(r, sizeof(r), 0); // initialize r to zero
  ie[1] := m[4]; }
  ie[4] := m[1]; } copy the value stored at source location to destination
  ie[5] := m[2]; }
  ie[6] := m[3]; }
  Ms_to_ie := r;
end;  

To test
var
  s : single; // can be any other 4 byte data type
  ms : my_real absolute s; // will point to s
  r : real;
  t : array[1..6] of byte absolute r; // will point to r
begin
  s := 98989898;
  ms := ms;// place a break point here and exmine the byte value
  r := Ms_to_Ie(s);
  t := t; // exmine t
end
0
NicoLaanCommented:
Does this have anything to do with float conversions?
See also:
http://en.wikipedia.org/wiki/IEEE_floating-point_standard

and another Q on EE:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_10419726.html

I got that idea from
www.borsaedati.it/document/DataFormat.doc
Using google: Function Ms_to_Ie

It appears the exponent used to be the last byte, position 4, and is now the first byte.
0
atul_parmarCommented:
>>are you declaring a new variable pointing at the same address as an existing variable? if so why do you want to do that?

To copy byte by byte

0
bruintjeAuthor Commented:
thanks a lot!
0
atul_parmarCommented:
To convert it to c# you will need to use pointers then the logic will remain the same.
0
Pierre CorneliusCommented:
To explain:

lets say I wanted to know the values of each byte of a string instead of the char values, I could do something like this:

var
  s: string[4];
  b: array[1..4] of byte absolute s;

what the absolute directive does is make that b and s reference the same area of memory so, s[1] will be the same byte in memory as b[1], etc.

So if I had s:= 'blah'
 
  then s[1] = 'b', s[2] = 'l', s[3] = 'a' and s[4] = 'h'

  whereas b[1] = 98, b[2] = 108, b[3]=97 and b[4] = 104

0
NicoLaanCommented:
Thanks bruintje!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.