本帖最后由 yale28 于 2010-04-02 16:43:45 编辑

解决方案 »

  1.   

           unsafe void* QtyToBuf(char* buf, UInt32 qty)
            {
                *buf++ =(char) qty;
                *buf++ = (char)(qty >> 8);
                *buf++ = (char)(qty >> 16);
                *buf++ = (char)(qty >> 24);
                return (buf);
            }
    要打开不安全代码开关。
    菜单“项目”--“属性”--“生成”--“允许不安全代码”前面打勾
      

  2.   

    我的想法是把一个最大为999999.999 的数字压缩放进 byte[4]  里面发送到下位机。 不知该如何调用这函数。愚昧啊!!!!!
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
             
                float ft = 999999.999f;            byte[] bytes = BitConverter.GetBytes(ft);            unsafe
                {               IntPtr i2= Marshal.AllocHGlobal(bytes.Length);
                   Marshal.Copy(bytes, 0, i2, bytes.Length);
                   byte *buf1 = (byte*)QtyToBuf((byte *)i2, 8);            }
            }        unsafe void* QtyToBuf(byte* buf, UInt32 qty)
            {
                *buf++ = (byte)qty;
                *buf++ = (byte)(qty >> 8);
                *buf++ = (byte)(qty >> 16);
                *buf++ = (byte)(qty >> 24);
                return (buf);
            }
        }
    }不确定qty的值,随便写的一个值。
    有没有C的DEMO,好确定结果。