Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted (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 by IngisKahn
"Who is John Galt?"
Posted

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;
}

Posted
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

 

:)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Posted

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']
Posted
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.
"Who is John Galt?"
  • Leaders
Posted

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)

[sIGPIC]e[/sIGPIC]
Posted

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 ;)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...