reading binary data to structure

This is a discussion on reading binary data to structure within the ml forums in Programming Languages category; When I've read in some bytes from a binary file using: let fd = Unix.openfile "some.bin" mode 0o644;; let str = String.create 53;; let n = Unix.read fd str 0 53;; how do I map the content (binary) of 'str' into a data structure of e.g. the following type: type entry = { a : int; b : int; c : int; d : int list; } thank you jonathan heusser -- Key fingerprint = 2A55 EB7C B7EA 6336 7767 4A47 910A 307B 1333 BD6C...

Go Back   Application Development Forum > Programming Languages > ml

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 07-14-2003, 12:22 PM
Jonathan Heusser
Guest
 
Default reading binary data to structure

When I've read in some bytes from a binary file
using:

let fd = Unix.openfile "some.bin" mode 0o644;;
let str = String.create 53;;
let n = Unix.read fd str 0 53;;

how do I map the content (binary) of 'str' into a data structure of
e.g. the following type:

type entry = {
a : int;
b : int;
c : int;
d : int list;
}

thank you
jonathan heusser

--
Key fingerprint = 2A55 EB7C B7EA 6336 7767 4A47 910A 307B 1333 BD6C


Reply With Quote
  #2  
Old 07-15-2003, 12:31 PM
Peter \Firefly\ Lund
Guest
 
Default Re: reading binary data to structure

On Mon, 14 Jul 2003, Jonathan Heusser wrote:

> how do I map the content (binary) of 'str' into a data structure of
> e.g. the following type:


The problem is severely underconstrained as you state it...

Since the code doesn't look like standard ML to me I guess it must be
OCaml.

Do you know how to get a character at a specific index in a string?

Do you know how to get the ordinal value of a character (i.e. turn a
character into an integer)?

If you know how to do that the rest becomes a small matter of programming
-- and of finding out what the problem /really/ is:

- how many characters per integer?
- what is the byte order?
- is there a count of the number of integers in d?
- or is there a fixed number?
- or are they terminated by a sentinel?

.... there are /many/ other variants if you really want to be perverse.

-Peter

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:46 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.