Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8829051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:44:47+00:00 2026-06-14T07:44:47+00:00

I want to write (and later retrieve) data in binary format. I am trying

  • 0

I want to write (and later retrieve) data in binary format. I am trying to get a minimal example to work at least to the level of the smell test (read input should look like written output), but I haven’t gotten it just, and consistently right. My machine is a linux with little endian, but since that is constant here, I ommitted it from the calls. I was also not sure if it was better to specify the size argument in the write, or leave it out. At any rate, the loaded input doesn’t look like out:

out<-seq(1,50,2)

##write
write<-file('~/output.txt','wb')
writeBin(out,con=write,size=4)
close(write)

##read
read<-file('~/output.txt','rb')
readBin(con=read,what=numeric(),n=length(out))
# [1] 3.200001e+01 3.276801e+04 1.048576e+06 1.677722e+07 1.006633e+08 4.026532e+08     1.610613e+09 6.442452e+09 1.503239e+10 3.006478e+10 6.012955e+10 1.202591e+11
close(read)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:44:48+00:00Added an answer on June 14, 2026 at 7:44 am

    Here is a worked example:

    R> dat <- seq(1,50,2)
    R> dat
     [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
    R> 
    

    We just write the object to a binary connection — omitting your third parameter:

    R> con <- file("/tmp/foo.bin", "wb")
    R> writeBin(dat, con)
    R> close(con)
    R> 
    

    And can then read back:

    R> con <- file("/tmp/foo.bin", "rb")
    R> dat2 <- readBin(con, what="numeric", n=length(dat))
    R> dat2
     [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
    R>
    R> all.equal(dat, dat2)
    [1] TRUE
    

    However, you need to store the length. I have used an internal file format which first writes a fixed (known) number of integers about rows, cols, “format number”, … and then read / writes a total of rows * cols numerics. We also wraps this into a gzip-ed file connection.

    You could even generalize this to writing columns as in a data frame — but if R is your only reader / writer then the already exisiting serialization via save() is better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a parser for NBT (Named Binary Tags) structures. The format
I want write a simple query which will fetch data from a table (which
I would like to encode data into a binary format in a buffer which
Suppose I have a text file with some data I want to retrieve lost
Hi I want to write a grammer( Using ANTLRWORKS ) that accept later (
I want to write a parser for Blender's .obj file. The file format seems
I want write a little code analyzer which parses nested structures and translates into
Hello I want write my own desktop sharing application in Java. The application should
I want to write a batch script statement where: FINDSTR has to check for
I want to write a program in Prolog that confirms if a b-tree of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.