我正在开发一个C#CE应用程序,以从由C ++ progam创建的二进制文件中读取数据以进行项目验证。
下面是C ++程序的编码。
// File Name: Ean2an.bin which is created by struct
struct EAN2AN_TYPE
{
__int64 ean:40; // 5 bytes, up to 12 digits
__int64 rec_no:24; // 3 bytes rec no in the c_ItemMaster File, up to 16 million records
};
// After bind data to struct, wil create the binary file
bool CreateBin_EAN2AN_TYPE()
{
if(mn_RecordCount_EAN2AN_TYPE == 0) return false;
FILE *binfile;
qsort(mc_EAN2AN_TYPE, mn_RecordCount_EAN2AN_TYPE, sizeof(struct EAN2AN_TYPE), qsort_EAN2AN_TYPE);
try
{
binfile = fopen(ms_Path_EAN2AN_TYPE, "wb");
fwrite(&mc_EAN2AN_TYPE, sizeof(struct EAN2AN_TYPE), mn_RecordCount_EAN2AN_TYPE, binfile);
}
catch(Exception ^ex)
{
TaskProgramLibrary::Message::ERR("Create EAN2AN_TYPE.bin fail!\r\n " + ex->Message);
}
finally
{
fclose(binfile);
mdw_FileSize_EAN2AN_TYPE = FileSize(ms_Path_EAN2AN_TYPE);
}
return true;
}
我试图使用二进制读取(基于位置)读取数据,并使用bitconverter转换为int64或使用Marshal.PtrToStructure,但返回的值不正确。然后,我尝试从文件中读取5个字节而不是8个字节,但是该值返回stil错误。
Below is the written C# coding
//Struct created in C#
[StructLayout(LayoutKind.Sequential)]
public struct EAN2AN_TYPE
{
[MarshalAs(UnmanagedType.I8)]
public Int64 ean;
[MarshalAs(UnmanagedType.I8)]
public Int64 rec_no;
}
//The ways i tried to read in C#
//1.Read Int64 by Binary
private void ReadByBinary()
{
using (BinaryReader b = new BinaryReader(_fs))
{
while (b.PeekChar() != 0)
{
Int64 x = b.ReadInt64();
Console.WriteLine(x.ToString());
}
}
}
//2.Using Marshal to convert the Intptr to struct's field type
private object ReadByMarshal(Type iType)
{
_oType = iType;// typeof(System.Int64);
byte[] buffer = new byte[Marshal.SizeOf(_oType)];
//byte[] buffer = new byte[5];
object oReturn = null;
try
{
_fs.Read(buffer, 0, buffer.Length);
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
oReturn = Marshal.PtrToStructure(handle.AddrOfPinnedObject(), _oType);
handle.Free();
return oReturn;
}
catch (Exception ex)
{
throw ex;
}
}
//3. Use Binary and use bit converter to convert to Int64
private void ReadByBinaryAndUseBitConverter()
{
using (BinaryReader b = new BinaryReader(_fs))
{
byte[] x = b.ReadBytes(8);
Int64 y = BitConverter.ToInt64(x, 0);
Console.WriteLine(y);
byte[] x2 = b.ReadBytes(8);
Int64 y2 = BitConverter.ToInt64(x2,0);
Console.WriteLine(y2);
}
}
//4. Use Marshal and convert to struct
public EAN2AN_TYPE GetStructValue()
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(EAN2AN_TYPE)];
EAN2AN_TYPE oReturn = new EAN2AN_TYPE();
try
{
//if (EOF) return null;
_fs.Read(buffer, 0, buffer.Length);
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
IntPtr rawDataPtr = handle.AddrOfPinnedObject();
oReturn = (EAN2AN_TYPE)Marshal.PtrToStructure(rawDataPtr, typeof(EAN2AN_TYPE));
handle.Free();
if (_fs.Position >= _fs.Length)
Close();
return oReturn;
}
catch (Exception ex)
{
throw ex;
}
}
编辑:上传二进制文件的图像
编辑:C#程序读取的前8个字节的值
编辑器显示的二进制数据
有人知道吗
提前致谢
参考方案
ean
被定义为40位实体,而rec_no
为24位,因此整个结构仅64位。您对EAN2AN_TYPE
的定义是128位,因此显然会有问题。我怀疑谁写了初始代码都没有道理,但是您的工作是找回并使用它,以便您处理自己要处理的事情。
编辑:更新为使用您指定的数据并考虑到本的投诉
这是获得相同结果的两种方法。一种是较容易理解的,因为它可以逐步移动,另一种则更快,更“正确”。我将您的示例EAN数据放入输入中以验证结果。
public struct EAN2AN_TYPE
{
public long ean; // can hold 5 bytes
public int rec_no; // can hold 3 bytes
}
byte[] incoming = new byte[] { 0x6F, 0x5D, 0x7C, 0xBA, 0xE3, 0x06, 0x07, 0x08 };
内存复制:
using(var stream = new MemoryStream(incoming))
using (var reader = new BinaryReader(stream))
{
// I leave it to you to get to the data
stream.Seek(0, SeekOrigin.Begin);
// get the data, padded to where we need for endianness
var ean_bytes = new byte[8];
// read the first 5 bytes
Buffer.BlockCopy(reader.ReadBytes(5), 0, ean_bytes, 0, 5);
var rec_no_bytes = new byte[4];
// read the last 3
Buffer.BlockCopy(reader.ReadBytes(3), 0, rec_no_bytes, 0, 3);
var ean2 = new EAN2AN_TYPE();
// convert
ean2.ean = BitConverter.ToInt64(ean_bytes, 0);
ean2.rec_no = BitConverter.ToInt32(rec_no_bytes, 0);
}
移位:
using (var stream = new MemoryStream(incoming))
using (var reader = new BinaryReader(stream))
{
// I leave it to you to get to the data
stream.Seek(0, SeekOrigin.Begin);
// get the data
var data = BitConverter.ToUInt64(reader.ReadBytes(8), 0);
var ean2 = new EAN2AN_TYPE();
// shift into our data
ean2.ean = (long)(data & ~0xFFFFFF0000000000);
ean2.rec_no = (int)(data >> 40);
}
当然,您可以将EAN2AN_TYPE
设为一个类,以8个字节的形式提供它,然后让属性访问器也为您执行移动的恶作剧。如果这必须是两方面的事情,我会这样做(即,您需要将数据放入这些结构之一中以发送回C应用程序)。
我有预先存在的python和C ++文件,其中python文件定义了许多点,而C ++代码利用其现有库进行了所需的计算。最终产品是C ++代码写入的文件。我正在寻找一种在python中获取2000点列表的方法,将其传递给函数,然后执行所有C ++代码并输出我需要的文件。其他注意事项。这必须是可以在Linux或Windows机器上工作的东西,并且最少安装新插件…
如何锁定终端运行的perl,obj c,c++,python和ruby等脚本的源代码? - python我想出售我在perl,obj c,c ++,python,ruby,bash,php等中制作的脚本等它们都在终端中运行。 (Linux)如何锁定源代码,以便无需人们访问源代码即可分发我的脚本..?换句话说,如何将在Terminal中运行的程序的源代码锁定,以便人们可以使用该程序(如果该代码已下载到他们的Linux机器上,但他们无法访问实际的源代码)?例:ex…
如何使用C#或C / C++在Windows 7中获取智能卡阅读器名称? - c#我正在尝试制作一个将使用C .dll(不幸的是,.dll没有好的文档)来访问智能卡的C#程序。 .dll的功能之一使用读取器的名称作为参数。我的问题是我不知道该如何命名。在寻找答案之后,我在这里的示例中发现了与我所需要的东西类似的内容:http://msdn.microsoft.com/en-us/library/aa379803%28VS.85%29.as…
标签名称属性带有+和引号 - javascript当数据从数据库到网格中的视图时,标签名称属性带有+和引号,但我不希望这样做:以下是我正在执行的代码@if (ViewBag.ExperienceDetailsGrid != null) { var ExprowCount = 1; Foreach (var item in ViewBag.ExperienceDetailsGrid) { <tr cla…
PyQT崩溃(基础C / C++对象已被删除)在“之后”清除QTreeWidget - python我尝试为QTreeWidget建立实时搜索,这意味着我有一个QLineEdit,并且在我键入内容时,我将在QTreeWidget中过滤显示的结果。目前,我通过QTreeWidget.findItems()获得了“仍显示的项目”foundItems = mainForm.ui.treeShips.findItems(text,QtCore.Qt.MatchCo…