Solved

Help needed rewriting some bitshift code

Posted on 2006-06-20
11
292 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:bruintje
[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
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 4

Expert Comment

by:NicoLaan
ID: 16942676
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
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 175 total points
ID: 16942751
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
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 16942771
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
Industry Leaders: 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 44

Author Comment

by:bruintje
ID: 16942828
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
 
LVL 10

Assisted Solution

by:atul_parmar
atul_parmar earned 175 total points
ID: 16942902
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
 
LVL 4

Assisted Solution

by:NicoLaan
NicoLaan earned 150 total points
ID: 16942906
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
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16942913
>>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
 
LVL 44

Author Comment

by:bruintje
ID: 16942981
thanks a lot!
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16942985
To convert it to c# you will need to use pointers then the logic will remain the same.
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 16942992
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
 
LVL 4

Expert Comment

by:NicoLaan
ID: 16943041
Thanks bruintje!
0

Featured Post

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!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 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