473,224 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

marshal binary data file, written with C++, with my C# code

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

Jan 19 '06 #1
14 4087
bb
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);
}
}
}

Jan 19 '06 #2
Thanks bb,

And what about integers float and doubles, how to marshal them ?

:)

Jan 19 '06 #3
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

Jan 19 '06 #4
Vertilka wrote:
And what about integers float and doubles, how to marshal them ?


Have a look at Marshal.ReadInt32 (etc) for integers. For doubles you
should use Marshal.ReadInt64 and then call
BitConverter.Int64BitsToDouble. I'm not guaranteeing it will work, but
it's certainly worth a try.

Unfortunately there's no direct equivalent for that process for floats
(at least not in 1.1 - I haven't checked 2.0) - instead you'd probably
want to use BitConverter.ToSingle(byte[]) having read 4 bytes into a
byte array first.

Jon

Jan 19 '06 #5
You can always use the overload of the static Copy method which takes an
IntPtr and populates an array of doubles (or floats, there is an overload
for that as well). If you want one double/float, you can just declare an
array of one element (a little overkill, I know, but less code, right?).

Also, if the OP is willing to use unsafe code, then it could be much
easier (just cast to the appropriate pointer type and dereference).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11********************@g44g2000cwa.googlegrou ps.com...
Vertilka wrote:
And what about integers float and doubles, how to marshal them ?


Have a look at Marshal.ReadInt32 (etc) for integers. For doubles you
should use Marshal.ReadInt64 and then call
BitConverter.Int64BitsToDouble. I'm not guaranteeing it will work, but
it's certainly worth a try.

Unfortunately there's no direct equivalent for that process for floats
(at least not in 1.1 - I haven't checked 2.0) - instead you'd probably
want to use BitConverter.ToSingle(byte[]) having read 4 bytes into a
byte array first.

Jon

Jan 19 '06 #6
Jon, Nicholas,
Thanks for your answers.

Can you both post code snippets.
Lets say that i have 8 bytes that i want to get a bouble from them.

Thanks.

Jan 19 '06 #7
bb
yes its just used to get a known small length string from my device
driver

although im not sure thats an excuse for hideous code ;-)

Jan 19 '06 #8
Jon,

I tried you advise.
How do i convert array of bytes to IntPtr, to use with
Marshal.ReadInt32 ?

=== Code snippet ===

FileStream fs = new FileStream(@"C:\myfile.bin", FileMode.Open);

byte[] buffer = new byte[1024];
fs.Read(buffer, 0, 8);

Marshal.ReadInt64( ???? );

??? BitConverter.Int64BitsToDouble();

=== Code snippet ===

Jan 19 '06 #9

"Vertilka" <ve******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
| Jon,
|
| I tried you advise.
| How do i convert array of bytes to IntPtr, to use with
| Marshal.ReadInt32 ?
|
| === Code snippet ===
|
| FileStream fs = new FileStream(@"C:\myfile.bin", FileMode.Open);
|
| byte[] buffer = new byte[1024];
| fs.Read(buffer, 0, 8);
|
| Marshal.ReadInt64( ???? );
|
| ??? BitConverter.Int64BitsToDouble();
|
| === Code snippet ===
|

Use the BinaryReader and wrap your FileStream, there is no need to call
Marshal methods, you simply need to call the right read method. Just beware
of the char encoding when reading cjar array's and strings.
string s1;
byte b1;
int i1;
float f1;
double d1;
char[] ca;

....
using(BinaryReader binReader =
new BinaryReader(File.Open(@".\myfile.bin", FileMode.Open)))
{
try {
while (true)
{
s1 = binReader.ReadString();
b1 = binReader.ReadByte();
i1 = binReader.ReadInt32();
f1 = binReader.ReadSingle();
d1 = binReader.ReadDouble();
ca = binReader.ReadChars(5);
Console.WriteLine(f1);
}
}
catch(EndOfStreamException ex)
{
// end of file reached
}
}

Willy.

Jan 19 '06 #10
Willy,

I don't know if you notice but the binary data file was written by C++.
I am not sure that type bits written into the binary file, using C++,
is the same as type bits writen using .NET.

Vertilka

Jan 19 '06 #11

"Vertilka" <ve******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
| Willy,
|
| I don't know if you notice but the binary data file was written by C++.
| I am not sure that type bits written into the binary file, using C++,
| is the same as type bits writen using .NET.
|
| Vertilka
|

This is the first thing you should try to find out, right? You can't read a
file if you don't know the data types used in the file.
You said it's a binary data file, where is the data file written? On what
system (Windows, Unix, mainframe). Things to look for are endianess, char
encoding and eventual padding. If the file is written on Windows system,
then you are almost certain that the binary and floating point types are
exactly the same, more problematic are the char arrays and strings (which
are basically the same as arrays of chars once serialized), here you have to
know the encoding used so that you know how to read and to decode them.

Willy.


Jan 19 '06 #12
Vertilka <ve******@gmail.com> wrote:
Jon, Nicholas,
Thanks for your answers.

Can you both post code snippets.
Lets say that i have 8 bytes that i want to get a bouble from them.


double d = BitConverter.Int64BitsToDouble(Marshall.ReadInt64( ...));

Of course, that assumes it's in the right format. Worth trying to see.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 19 '06 #13
again, friends...

i have a byte array with data.
i know for example that the first 8 bytes are double.

now.
with this line:
double d = BitConverter.Int64BitsToDouble(Marshall.ReadInt64( ...));
i need to give intptr to ReadInt64.
how to convert byte array to intptr

thanks.

Jan 20 '06 #14
Vertilka <ve******@gmail.com> wrote:
again, friends...

i have a byte array with data.
i know for example that the first 8 bytes are double.
In that case I'm not sure where the Marshal class comes in at all.
That's what's been confusing us, I suspect.
now.
with this line:
double d = BitConverter.Int64BitsToDouble(Marshall.ReadInt64( ...));
i need to give intptr to ReadInt64.
how to convert byte array to intptr


Use BitConverter.ToDouble then.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 20 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
4
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the...
2
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of...
4
by: Michael McGarry | last post by:
Hi, I am using the marshal module in python to save a data structure to a file. It does not appear to be portable. The data is saved on a Linux machine. Loading that same data on a Mac gives me...
21
by: Mike | last post by:
Hi, The example below shows that result of a marshaled data structure is nothing but a string >>> data = {2:'two', 3:'three'} >>> import marshal >>> bytes = marshal.dumps(data) >>>...
2
by: Edvin | last post by:
Why is binary array written to a file different than when converting the binary array to string and then writing it to file! For example: // --- Allocate byte array byte arrByte = new byte;...
3
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.