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

yale28 2010-04-02 04:37:11
/*
typedef char U8;
typedef unsigned int U32;
*/
void *QtyToBuf(U8 *buf,U32 qty)
{
*buf++ = qty;
*buf++ = qty >> 8;
*buf++ = qty >> 16;
*buf++ = qty >> 24;
return (buf);
}

最近在写上位机的程序,怎么样把这段C转成用C#表达 或者 能实现同样的功能。关于C#的位移 地址 指针 这些与C还是有出入的。请精通C和C#的高手帮忙看看 谢谢
...全文
61 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingyuebuyu 2010-04-06
  • 打赏
  • 举报
回复
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,好确定结果。
yale28 2010-04-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xingyuebuyu 的回复:]
C# code
unsafe void* QtyToBuf(char* buf, UInt32 qty)
{
*buf++ =(char) qty;
*buf++ = (char)(qty >> 8);
*buf++ = (char)(qty >> 16);
*……
[/Quote]


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

xingyuebuyu 2010-04-02
  • 打赏
  • 举报
回复
       unsafe void* QtyToBuf(char* buf, UInt32 qty)
{
*buf++ =(char) qty;
*buf++ = (char)(qty >> 8);
*buf++ = (char)(qty >> 16);
*buf++ = (char)(qty >> 24);
return (buf);
}



要打开不安全代码开关。
菜单“项目”--“属性”--“生成”--“允许不安全代码”前面打勾

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧