用C ++编写的编组二进制数据文件和我的C#代码 [英] marshal binary data file, written with C++, with my C# code
问题描述
我需要使用我的C#
应用程序读取由C ++程序编写的二进制数据文件。
如何编组我用C#代码读取的字节。 NET类型。
数据是数字(整数浮点数等等)和字符串。
我查看了Marshal类,但是不知道如何使用它与文件。
请添加几行代码。
非常感谢
这里是我用来读取由C ++存储的字符串作为IntPtr的东西
公共类MarshalUtil
{
IntPtr m_objData = IntPtr.Zero;
int m_nPosition = 0;
public MarshalUtil(IntPtr objData)
{
m_objData = objData;
}
public string ReadString()
{
ArrayList objBytes = new ArrayList();
byte byt;
string strData ="" ;;
while((byt = Marshal.ReadByte( m_objData,m_nPosition))!= 0)
{
strData + =(char)byt;
m_nPosition + = 2;
}
m_nPosition + = 2;
返回strData;
}
public bool MoreData
{
get
{
return(Marshal) .ReadByte(m_objData,m_nPosition)!= 0);
}
}
}
谢谢bb,
那么整数漂浮和双打怎么样,如何整理它们?
:)
bb写道:这里是我用来读取C ++作为IntPtr存储的字符串
<公共类MarshalUtil
{
IntPtr m_objData = IntPtr.Zero;
int m_nPosition = 0;
公共MarshalUtil(IntPtr objData)
{
m_objData = objData;
}
公共字符串ReadString()
{/> ArrayList objBytes = new ArrayList ();
字节byt;
字符串strData ="" ;;
while((byt = Marshal.ReadByte(m_objData,m_nPosition))!= 0)
{
strData + =(char)byt;
m_nPosition + = 2;
}
m_nPosition + = 2;
返回strData;
}
对于大字符串来说,这将是非常低效的 - 你应该在
至少使用一个StringBuilder的。你似乎也忽略了每隔一个字节的
- 我不相信上面的字符串将用于包含U + 00FF以上的Unicode字符的字符串
。相反,你应该阅读
*两个*字节,然后向左移动一个8位。
Jon
I need to read binary data file written by C++ program, using my C#
application.
How do i marshal the bytes i read with my C# code to .NET types.
The data is numbers (integers float doubles etc..) and strings.
I looked at the Marshal class but did not know how to use it with file.
Please add few code lines.
Thanks alot
here is something i use for reading a string stored by C++ as an IntPtr
public class MarshalUtil
{
IntPtr m_objData = IntPtr.Zero;
int m_nPosition = 0;
public MarshalUtil(IntPtr objData)
{
m_objData = objData;
}
public string ReadString()
{
ArrayList objBytes = new ArrayList();
byte byt;
string strData = "";
while((byt = Marshal.ReadByte(m_objData, m_nPosition)) != 0)
{
strData += (char) byt;
m_nPosition += 2;
}
m_nPosition += 2;
return strData;
}
public bool MoreData
{
get
{
return (Marshal.ReadByte(m_objData, m_nPosition) != 0);
}
}
}
Thanks bb,
And what about integers float and doubles, how to marshal them ?
:)
bb wrote:here is something i use for reading a string stored by C++ as an IntPtr
public class MarshalUtil
{
IntPtr m_objData = IntPtr.Zero;
int m_nPosition = 0;
public MarshalUtil(IntPtr objData)
{
m_objData = objData;
}
public string ReadString()
{
ArrayList objBytes = new ArrayList();
byte byt;
string strData = "";
while((byt = Marshal.ReadByte(m_objData, m_nPosition)) != 0)
{
strData += (char) byt;
m_nPosition += 2;
}
m_nPosition += 2;
return strData;
}
That will be hideously inefficient with large strings - you should at
the very least be using a StringBuilder. You also seem to be ignoring
every other byte - I don''t believe the above will work with strings
containing Unicode characters above U+00FF. Instead, you should read
*two* bytes, and shift one left by 8 bits.
Jon
这篇关于用C ++编写的编组二进制数据文件和我的C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!