当前位置:编程学习 > C#/ASP.NET >>

关于C语言转换的问题是高手的话--请进

--------------------编程问答--------------------
       unsafe void* QtyToBuf(char* buf, UInt32 qty)
        {
            *buf++ =(char) qty;
            *buf++ = (char)(qty >> 8);
            *buf++ = (char)(qty >> 16);
            *buf++ = (char)(qty >> 24);
            return (buf);
        }



要打开不安全代码开关。
菜单“项目”--“属性”--“生成”--“允许不安全代码”前面打勾 --------------------编程问答--------------------
引用 1 楼 xingyuebuyu 的回复:
C# code
       unsafe void* QtyToBuf(char* buf, UInt32 qty)
        {
            *buf++ =(char) qty;
            *buf++ = (char)(qty >> 8);
            *buf++ = (char)(qty >> 16);
            *……



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

--------------------编程问答--------------------
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,好确定结果。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,