jspringe Posted July 29, 2004 Report Posted July 29, 2004 I'm new to C#. Using the methods provided in the CSHARP SDK I have been able to fetch and use most datatypes, but I'm having trouble with the 8 byte FLOAT64 values. The overloaded methods provided are: public bool FSUIPC_Get(ref int Token, ref byte Result) public bool FSUIPC_Get(ref int Token, ref short Result) public bool FSUIPC_Get(ref int Token, ref int Result) public bool FSUIPC_Get(ref int Token, ref long Result) public bool FSUIPC_Get(ref int Token, int dwSize, ref byte[] Result) I assume to fetch an 8 byte value that I will use a new byte[8] array. So the question: how do I convert the 8 separate bytes returned from the get into a C# double value. Thanks for any help, Jason
Pete Dowson Posted July 29, 2004 Report Posted July 29, 2004 I'm new to C#. That's more than me! :wink: I assume to fetch an 8 byte value that I will use a new byte[8] array. So the question: how do I convert the 8 separate bytes returned from the get into a C# double value. If C# supports the double type, why can't you use that directly, like the other numerical types? Can't you add a further 'overload' (whatever that is, forgive my ignorance)? Regards, Pete
jspringe Posted July 29, 2004 Author Report Posted July 29, 2004 Pete, Just got up some courage to look inside Scott McCrory's C# library. Looks simple enough to add a FSUIPC_Get double method. However, he is using a builtin class called "Marshal" that can read the datatypes he provided methods for. The Marshal class does not have a method to read type double. This is what I have so far and where I'm stuck: public bool FSUIPC_Get(ref int Token, ref double Result) { int Size = 8; // 8 bytes in a double if ((Token < 0) || (Token > IPC_BUFFER_SIZE - (4 + Size)) ) { //Token out of range Result = 0; return false; } IntPtr heapbuf = Marshal.AllocHGlobal(Size); Marshal.Copy(IPC, Token + 4, heapbuf, Size); Result = Marshal.Read??????(heapbuf); Marshal.FreeHGlobal(heapbuf); if (IPCdr[Token] ) { IPCdr[Token] = false; // reset data ready flag return true; } else { // if (data ready flag not set) return false; } } Marshal supports the following reads: Marshal.ReadByte Marshal.ReadInt16 Marshal.ReadInt32 Marshal.ReadInt64 Marshal.ReadIntPtr So, I either need an example of how to call one of the existing methods that Scott provided (my original question on how to convert a byte[] to double) or I need an answer on how to add a FSUIPC_Get that takes a "double". Thanks, Jason
Pete Dowson Posted July 29, 2004 Report Posted July 29, 2004 This is what I have so far and where I'm stuck: Ah, I'm afraid I would be stuck even before you! But: "Marshal.ReadInt64" will presumably read an 8-byte (64-bit) integer. Can you use casts in C#? If so just copy the int64 you read into a double using an explicit cast. Regards, Pete
jspringe Posted July 30, 2004 Author Report Posted July 30, 2004 Well, I finally came up with an answer. There is a conversion method from byte[] to type double (took forever to find it). It is the "BitConverter" class. byte[] buffer = new byte[255]; // get loaded weight bResult = m_fsuipc.FSUIPC_Read(0x30C0, 8, ref token, ref dwResult); bResult = m_fsuipc.FSUIPC_Process(ref dwResult); bResult = m_fsuipc.FSUIPC_Get(ref token, 8, ref buffer); double dLoadedWeight = BitConverter.ToDouble(buffer, 0); Jason
Pete Dowson Posted July 30, 2004 Report Posted July 30, 2004 Well, I finally came up with an answer. There is a conversion method from byte[] to type double (took forever to find it). It is the "BitConverter" class. Well done! All this complication and obscure inefficient-looking code does make me wonder -- why choose C#? It seems to have the worst of VB and C++ with none of the flexibility and power of the latter or ease of the former. Regards, Pete
jspringe Posted July 30, 2004 Author Report Posted July 30, 2004 Why use C#? Well good or bad, it is the closest thing to Java and true OO (I still prefer Java). Really, it is extremely simple. The only time it can be difficult is when you need to interface to legacy modules (and that's just because it is my first time developing an app in C#). The syntax and structure is much like Java which makes it easier to switch between the two on different projects (I have completely forgotten my c programming skills from 20 years ago -- char* far * what?). A long time ago I vowed never to program in VB. Just not ready to eat crow on that one yet. Jason
Pete Dowson Posted July 30, 2004 Report Posted July 30, 2004 The only time it can be difficult is when you need to interface to legacy modules "Legacy"? Is C and C++ relegated to a "legacy" category now? Most all of FS2004 is written in C++ (some parts are C and ASM leftovers, but very little now). And pretty much all of Windows is too. How can all these modern modules be classed as "legacy"? Regards, Pete
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now