Marshal C++ float to C# float accuracy problem -


i have dbase iv database. each row has memo field ascii encoded string holds 2 serialized borland c++ structures. able pull data using oledb, re-encode ascii using asciiencoding class, convert bytes using binaryreader, , cast c# struct using marshal.ptrtostructure. data correct float big in database wrong when cast c#. example, value of 1149.00 cast 764.9844 value 64.00 cast fine. can post of code , structures figured tried keep short @ first. know floats precise 7 digits i'm confused why i'm seeing because values under limit.

edit:

struct cplusplusstruct // c++ code {   int   number;   float p;   float zp;   float hours;   int   month;   int   day;   int   year;   int   hour;   int   minute;   int   second;   ulong upctime;   int   b;   char  name[21];   float l;   float h;   float s; }   [structlayout(layoutkind.sequential, pack = 1)] public struct csharpstruct  //the c# struct created {      public int number;      public float pr;      public float zp;      public float hours;      public int month;      public int day;      public int year;      public int hour;      public int minute;      public int second;      public uint32 upctime;      public int b;  [marshalasattribute(unmanagedtype.byvaltstr, sizeconst = 21)]      public string name;      public float l;      public float h;      public float s;  }   //ole db connection , query ...  //casting data struct asciiencoding encoding = new asciiencoding(); byte[] blob = encoding.getbytes(memostring); memorystream memorystream = new memorystream(blob); binaryreader binaryreader = new binaryreader(memorystream);  int datasize = marshal.sizeof(typeof(csharpstruct)); gchandle handle = gchandle.alloc(binaryreader.readbytes(datasize), gchandletype.pinned); csharpstruct data = (csharpstruct) marshal.ptrtostructure(handle.addrofpinnedobject(), typeof(csharpstruct)); 

edit: following java code read data fine without use of casting.

org.xbasej.dbf dbf = new org.xbasej.dbf(dbpath); org.xbasej.dbf dbf = new org.xbasej.dbf(dbpath); memofield m = (memofield) dbf.getfield("memofield");  charset charset = charset.forname("us-ascii"); charsetdecoder decoder = charset.newdecoder(); bytebuffer trendbytes = bytebuffer.wrap(m.getbytes()); trendbytes.order(byteorder.little_endian); trendbytes.getint(); trendbytes.getfloat(); 

you have pack = 1 in c# struct, have not said whether c++ struct packed. since have odd-sized field before floats (the 21-character string) might cause trouble , mean floats being read mis-aligned. before there 4 bytes long packing less cause problems. make sure packing matches in both c# , c++ before going further.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -