Questions tagged [bindata]

A declarative way to read and write structured binary data in Ruby.

BinData is a Ruby gem described in its documentation as:

A declarative way to read and write structured binary data. ... It supports all the common datatypes that are found in structured binary data. Support for dependent and variable length fields is built in.

Again, from the documentation, instead of writing code looking like:

io = File.open(...)
len = io.read(2).unpack("v")[0]
name = io.read(len)
width, height = io.read(8).unpack("VV")
puts "Rectangle #{name} is #{width} x #{height}"

BinData lets you rewrite it in a more idiomatic and readable way:

class Rectangle < BinData::Record
  endian :little
  uint16 :len
  string :name, :read_length => :len
  uint32 :width
  uint32 :height
end

io = File.open(...)
r = Rectangle.read(io)
puts "Rectangle #{r.name} is #{r.width} x #{r.height}"
28 questions
19
votes
2 answers

Write array of radix-2 numeric strings to binary file in Ruby

I've written a simple Huffman encoding in Ruby. As output I've got an array, for example: ["010", "1111", "10", "10", "110", "1110", "001", "110", "000", "10", "011"] I need to write, and then read, it to and from a file. I tried several…
Ivan Kozlov
  • 561
  • 2
  • 8
  • 19
6
votes
3 answers

How do I start reading at an offset in binary data?

I have a C-like struct like this: SomeStruct << BinData::Record endian :little uint32 :offsetOfName uint32 :offsetOfLastname #... uint32 :lenVars struct :Person, :length => :lenVars string :name string :lname #... end I have a bunch of…
omninonsense
  • 6,644
  • 9
  • 45
  • 66
5
votes
2 answers

How to calculate marginal probabilities for generating correlated binary variables

Let's say I want to create 50 binary variables of length 100 that are each correlated with each other. For I create a correlation matrix with the specified rho's: cor.mat <- matrix(0.2,nrow=50, ncol=50) diag(cor.mat) <- 1 next I use…
user1984076
  • 777
  • 1
  • 8
  • 16
2
votes
0 answers

View BinData in MongoDB as text

In MongoDB I have row with field name is content as BinData type, I want to view the content of this fields as text, does MongoDB support that in mongoshell. My row content: { "_id" : "adsdadad9809kjsa-", "fetchInterval" :…
Tin Huynh
  • 41
  • 6
2
votes
0 answers

How to generate a binomial vector of n correlated items?

I want to generate a binomial vector based on a number of correlated items each with a defined probability. When I use e. g. rbinom(1e3, size = 4, prob = c(p.x1, p.x2, p.x3, p.x4)) I'm getting something like 3 3 0 0 2 4 1 0 4 4 0 1 4.... Now these…
jay.sf
  • 60,139
  • 8
  • 53
  • 110
2
votes
2 answers

Reusing BinData Records with different data

If have multiple BinData Records that take the following form, here are a few examples: class DebugInfo < BinData::Record endian :little int32 :num array :data, :type => :debug_log, initial_length: :num end class GoalInfo <…
Fianite
  • 319
  • 2
  • 9
2
votes
0 answers

How do I use BinData?

I'm trying to parse a binary file, and I can't quite figure out how to use BinData properly. The way the binary is set up is like such: first 4 bytes (UINT32) represent the length of the property name. next 8 * length represent the property name…
Fianite
  • 319
  • 2
  • 9
2
votes
1 answer

Creating user defined primitive type from binary data with BinData?

I have a group of files which I have to download from a legacy Cobol system each night. I convert these files from binary data files into MySql tables. I wrote a Ruby program to do this using BinData for the individual file structures. There are…
user271916
  • 81
  • 3
1
vote
1 answer

How to convert MongoDB BinData to Guid in c#

i have a BinData in MongoDB and i would convert in Guid in c#. For example this is my Guid in MongoDB document: BinData(3, 'E7hI3meCkEC5C/KU8w7BRQ==') I would convert in c# Guid or c# string. Thanks I try to Convert with ConvertFromBase64String but…
1
vote
0 answers

How to decode MongoDB bindata type to UUID string in SQL Server

I am migrating data from MongoDB to SQL server using an ETL tool (SSIS). There are bindata type 0 fields that are brought across as $binary and $type pairs. I want to know how I can decode this on SQL server side to represent it as a UUID…
1
vote
0 answers

Load DLL directly from go-bindata byte-slice

I'm building a windows application that uses a third-party DLL. In order to bundle everything into one executable, I have embedded said DLL as a go-bindata asset into my binary. Currently I drop the embedded DLL to the local file system when the…
Christian
  • 1,589
  • 1
  • 18
  • 36
1
vote
3 answers

Using Ruby BinData gem to read choices

I am implementing a data structure using Ruby and the BinData gem. I need to implement a Choice value. According to the BinData documentation, a choice can be implemented as: class MyData < BinData::Record uint8 :type choice :data, :selection…
Indika K
  • 1,332
  • 17
  • 27
1
vote
0 answers

Rails encode control characters and send over a socket

Using Eventmachine I have been able to build a simple client/server program that successfully sends data over a socket. But the server expects that data is a specific format. In order to communicate with the server, the first step is to send a…
Arif
  • 1,369
  • 14
  • 39
1
vote
1 answer

Structuring BinData Records in Ruby

I'm not sure choices is exactly what I need here, but I'll explain what I'm trying to do. I have the following BinData structure that works fine. class Property < BinData::Record endian :little int32 :name_len string :name,…
Fianite
  • 319
  • 2
  • 9
1
vote
1 answer

Validating a BinData::Record on initialization

Is there a way to validate the fields being set on a BinData::Record during initialization? The check_value for each parameter appears to only be evaluated after reading from an input stream class Foo < BinData::Record uint8…
FooManChew
  • 21
  • 2
1
2