Solved

Help needed rewriting some bitshift code

Posted on 2006-06-20
11
288 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now