htrada Posted May 2, 2005 Posted May 2, 2005 Hi, I´m currently developing an application wich transmit floiting point numbers (trough a serial port). The numbers, wich are 32 bits long, are transmited in 4 bytes. Is there any way to use something like pointers in C???? what I want to do is get those 4 bytes and asign them to the 32 bit single variable. Quote
IngisKahn Posted May 2, 2005 Posted May 2, 2005 (edited) Sure: byte[] floatBytes = new byte[4]; float val; // assign values unsafe // remember to enable unsafe mode in the build options { fixed (byte* pfloatBytes = floatBytes) // all managed types must be fixed to use a pointer val = *(float*)pfloatBytes; } Edited May 2, 2005 by IngisKahn Quote "Who is John Galt?"
htrada Posted May 2, 2005 Author Posted May 2, 2005 thanks, but I think that is C# code. What would be the visual basic equivalent?? Sure: byte[] floatBytes = new byte[4]; float val; // assign values unsafe // remember to enable unsafe mode in the build options { fixed (byte* pfloatBytes = floatBytes) // all managed types must be fixed to use a pointer val = *(float*)pfloatBytes; } Quote
IngisKahn Posted May 3, 2005 Posted May 3, 2005 Hehe. There is none. Embrace .NET! You can have VB access C# dlls, you can even have VB and C# in the same assembly. Quote "Who is John Galt?"
mark007 Posted May 3, 2005 Posted May 3, 2005 Hehe. There is none. Embrace .NET! You can have VB access C# dlls, you can even have VB and C# in the same assembly. Not quite true. You can use the system.runtime.interopservices marshall class. For example this code takes apart an integer into bytes and puts it back together again. You can of course do similar with a floating point number. Sub test() Dim x As System.Int32 = 32400 Dim b(3) As System.Byte Dim i As New System.IntPtr 'take apart i = Marshal.AllocHGlobal(4) Marshal.WriteInt32(i, x) For j As Integer = 0 To 3 b(j) = Marshal.ReadByte(i, j) System.Windows.Forms.MessageBox.Show("Part: " & b(j)) Next 'put back together Dim y As System.Int32 Dim py As System.IntPtr py = Marshal.AllocHGlobal(4) For j As Integer = 0 To 3 Marshal.WriteByte(py, j, b(j)) Next y = Marshal.ReadInt32(py) System.Windows.Forms.MessageBox.Show("Result: " & y) End Sub HTH :) Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
IngisKahn Posted May 3, 2005 Posted May 3, 2005 Ya, but I'd suggest to stay away from Marshal wherever possible. Quote "Who is John Galt?"
htrada Posted May 3, 2005 Author Posted May 3, 2005 ok, I´ll see wich option suits my applicacion. Thanks both of you for your help. Ya' date=' but I'd suggest to stay away from Marshal wherever possible.[/quote'] Quote
mark007 Posted May 3, 2005 Posted May 3, 2005 Ya' date=' but I'd suggest to stay away from Marshal wherever possible.[/quote'] I assume you have a reason for this? Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
IngisKahn Posted May 3, 2005 Posted May 3, 2005 Take a look at the MSIL for each version. Quote "Who is John Galt?"
mark007 Posted May 4, 2005 Posted May 4, 2005 Which will reveal? Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
Administrators PlausiblyDamp Posted May 4, 2005 Administrators Posted May 4, 2005 Do you have a sample of what the values and their 4 byte representaion would be? Or would a .Net single or double be suitable anyway? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
IngisKahn Posted May 4, 2005 Posted May 4, 2005 Along those lines, would you be better served by a BinaryReader? Quote "Who is John Galt?"
mark007 Posted May 4, 2005 Posted May 4, 2005 Not wanting to antagonise in any way, just would like to know... Take a look at the MSIL for each version. Which will reveal? Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
IngisKahn Posted May 4, 2005 Posted May 4, 2005 It would reveal some of the overhead involved. But anyway, I don't think you should be using InteropServices when a simple stream would do. Quote "Who is John Galt?"
Leaders snarfblam Posted May 4, 2005 Leaders Posted May 4, 2005 If you want to get the bytes that make up most any numeric format, try using the System.BitConverter class. Alternatively you could create a "union" of sorts using the <System.Runtime.InteropServices.FieldOffset> and <System.Runtime.InteropServices.StructLayout> attributes on a struct. <System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)> _ Structure Union_SingleByte <System.Runtime.InteropServices.FieldOffset(0)> _ Dim [single] As Single <System.Runtime.InteropServices.FieldOffset(0)> _ Dim Byte0 As Single <System.Runtime.InteropServices.FieldOffset(1)> _ Dim Byte1 As Single <System.Runtime.InteropServices.FieldOffset(2)> _ Dim Byte2 As Single <System.Runtime.InteropServices.FieldOffset(3)> _ Dim Byte3 As Single 'You can assign the bytes and read the single, or 'assign the single and read the bytes End Structure 'Even easier Dim Bytes As Byte() = BitConverter.GetBytes(MySingle) Quote [sIGPIC]e[/sIGPIC]
mark007 Posted May 4, 2005 Posted May 4, 2005 So your reasoning is it's slow? What sort of proportion compared to using c# pointers? Any other issues? Admittedly, this may not matter for this problem particularly but would be useful for others searching on the subject, and myself ;) Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
htrada Posted May 4, 2005 Author Posted May 4, 2005 thanks, I think Ill go for te union if it works right it seems to be more flexible Quote
htrada Posted May 4, 2005 Author Posted May 4, 2005 the bit converter class is great!, I´ve tried it and has all conversions included Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.