社区
C#
帖子详情
哪位高人有好的struct转换成byte[]然后byte[]转成struct的解决方案啊
flashccie
2006-10-22 10:36:17
从GOOGLE里找到
http://community.csdn.net/Expert/topic/3466/3466293.xml?temp=.6052515
可以“比较”完美得实现 struct <-> byte[] 的互相转换。
可是已经是错误页面了。
如果哪位高人解决。。麻烦再贴一次好不?
...全文
294
14
打赏
收藏
哪位高人有好的struct转换成byte[]然后byte[]转成struct的解决方案啊
从GOOGLE里找到 http://community.csdn.net/Expert/topic/3466/3466293.xml?temp=.6052515 可以“比较”完美得实现 struct byte[] 的互相转换。 可是已经是错误页面了。 如果哪位高人解决。。麻烦再贴一次好不?
复制链接
扫一扫
分享
举报
写回复
配置赞助广告
14 条
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
打赏红包
runrunrun
2006-10-23
打赏
举报
回复
http://community.csdn.net/Expert/TopicView2.asp?id=3466293
真相重于对错
2006-10-23
打赏
举报
回复
直接用string 何必用char[]呢??
ms-help://MS.MSDNQTR.2003FEB.2052/cpguide/html/cpconstructssample.htm
flashccie
2006-10-23
打赏
举报
回复
string key = "sdfasd32473fe12wq423sdfaga56663468hmk@123%dsfadsf&";
byte[] buf;
testStruct stuTest = new testStruct();
stuTest.Key = new char[128];
char[] tmpKey=key.ToCharArray();
for (int i = 0; i < tmpKey.Length; i++)
{
stuTest.Key[i]=tmpKey[i];
}
stuTest.i = 2;
buf = BinarySerializer.Struct2Bytes<testStruct>(stuTest);
flashccie
2006-10-23
打赏
举报
回复
Sunmast(速马|回到未来)
可以转了
可是服务器接受提示长度超出了。
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct testStruct
{
public int i;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public char[] Key;
}
这个总长度应当是132.转出来之后也是132.可是服务器接受的时候。提示超长了。不知道是我的错还是服务器哪边的问题。。。
速马
2006-10-23
打赏
举报
回复
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public char[] Key;
- or -
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string Key;
flashccie
2006-10-23
打赏
举报
回复
我的结构体定义如下
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct testStruct
{
public int i;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public char[] Key;
}
在执行Marshal.SizeOf(structObj);时抛出
{"类型“testStruct”不能作为非托管结构进行封送处理;无法计算有意义的大小或偏移量。"}
在执行Marshal.StructureToPtr(structObj, buffer, true);抛出
{"无法封送处理类型为“testStruct”的字段“Key”: 无效的托管/非托管类型组合(数组字段必须与 ByValArray 或 SafeArray 成对出现)。":""}
速马
2006-10-23
打赏
举报
回复
http://blog.joycode.com/sunmast/archive/2005/12/13/dotnet20_pinvoke_enhance.aspx
aafshzj
2006-10-23
打赏
举报
回复
序列化是不错的方法。
flashccie
2006-10-23
打赏
举报
回复
runrunrun(做最好的自己)
的好像不行呀。。。
flashccie
2006-10-23
打赏
举报
回复
是.net 和C++
.net 这边用C#
Knight94
2006-10-23
打赏
举报
回复
你主要是什么程序间的数据传递,如果是.net的话,可以使用序列化进行传递。
例如:
[Serializable]
struct yourStruct
{
...
}
//Struct to byte[]
yourStruct myData = new yourStruct();
BinaryFormatter Bf = new BinaryFormatter();
MemoryStream Ms = new MemoryStream();
Bf.Serialize(Ms,myData);
byte[] bData = ms.GetBuffer();
soaringbird
2006-10-23
打赏
举报
回复
关键字:unsafe代码、fixed数组、指针,有了这些,就可以像C/C++那样发送和接收结构的内容了。
rtdb
2006-10-22
打赏
举报
回复
-->XML-->String
flashccie
2006-10-22
打赏
举报
回复
主要是用于Socket发送用的
相关推荐
Python之路——
struct
模块
struct
模块 #
struct
模块 用来将数字字符串等
转
换
成
固定长度的字节 # format: # x: pad
byte
(no data); c:char; b:signed
byte
; B:unsigned
byte
; # ?: _Bool (requires C99; if not available, char is used instead) # h:sho...
struct
变量 实例
转
换
成
byte
[] 类型
Go语言中结构体
struct
与字节数组[]
byte
的相互
转
换
三种方式的[]
byte
与
struct
的类型
转
换
,尤其是对于非固定大小的类型的一种
解决
方案
将python中的一个float变量
转
成
内存的4个字节值
#coding=utf-8 from
struct
import pack,unpack
byte
=pack('f',1.5) print(
byte
) print([i for i in
byte
])
byte
=pack('f',123432.523424) print(
byte
) print([i for i in
byte
]) 输出 b'\x00\x...
C#里使用CopyMemory
Socket接收到的
byte
[]要
转
换
成
自定义的
struct
/ 自定义
Struct
转
换
成
byte
[]都相当麻烦用循环去
转
换
太浪费时间了……于是想到用CopyMemory,Google一圈终于搞定下面的代码是在Snippet Compiler里编译通过的C#代码 #region Imports using System; using S
发帖
C#
C#
.NET技术 C#
复制链接
扫一扫
10.8w+
社区成员
64.2w+
社区内容
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
帖子事件
创建了帖子
2006-10-22 10:36
社区公告
让您成为最强悍的C#开发者