Solved

Help needed rewriting some bitshift code

Posted on 2006-06-20
11
290 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
  • 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

832 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