Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Read binary data (ones and seros) from file/stream?

 
Greenhorn
Posts: 7
MySQL Database PHP Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure how to word this, but I'd like to get the actual binary data from a file/stream.

I know I can get the int representation of a byte, but is it possible to get the actual binary string or more preferably, an array of int (1's and 0's) without needing to convert to a string or hex?
 
Marshal
Posts: 27379
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it be fair to assume that you want to take each byte from the file and represent it as eight digits which are either 0 or 1? If so then the BigInteger method toByteArray() looks like a pretty straightforward way to do that. Although if you want to treat the bytes as unsigned, you'll have to do some bit-fiddling before you give it to the BigInteger.
 
Andrew Breksa
Greenhorn
Posts: 7
MySQL Database PHP Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. I want to:

byte[] -> byte -> int[] of 1/0 (for each byte)

I apologize if I'm not understanding the docs correctly (or your answer), but it seems BigInteger returns byte[] from toByteArray(), not sure how to get the int[] from that.
 
Paul Clapham
Marshal
Posts: 27379
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to get an int[] from a byte[] you just write a loop which copies each byte from the byte[] into the corresponding place in the int[].
 
Paul Clapham
Marshal
Posts: 27379
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clarify, let's try an example: Suppose the first byte you read from the file is 27 (as a decimal value). Then you want to convert it to an array of ints like [1, 1, 0, 1, 1]? Do I have that right?

I'm also curious why you want to avoid using strings or hexadecimal to make this conversion happen.
 
Andrew Breksa
Greenhorn
Posts: 7
MySQL Database PHP Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, exactly.

Mostly for speed, although I haven't tested the speed of conversions using Integer.toBinaryString, or the extra step of hex. I actually need the values as int, and assumed the extra step of using a string would introduce unneeded overhead. Correct me if I'm wrong.
 
Paul Clapham
Marshal
Posts: 27379
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may be wrong, but I have no idea about how fast you need to do this operation or whether you've tested the speed of various alternatives. For example you might find that changing the size of the buffer you use for reading the file might make speed improvements which dwarf anything you might do by choosing various in-memory operations. So I can't say you're wrong. It's quite possible though.

If it were me I would just write some reasonable-looking code and then see whether I thought it was fast enough. I wouldn't optimize code I hadn't written yet.
 
Andrew Breksa
Greenhorn
Posts: 7
MySQL Database PHP Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Makes sense, I'll give this a go and see what I come up with.

Thanks for the help and pointers!

(Is it common practice/allowed to keep threads open for updates? Or should I open future questions regarding this on separate threads and close this one?)
 
Paul Clapham
Marshal
Posts: 27379
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get the feeling that you don't really have a problem with this conversion, but if you still do then this thread would be the right place to ask about it. However if you come across new problems then a new thread would probably be better.

(The moderators can merge threads if they think it would be better, so don't worry about starting new threads.)

Good luck with your project!
 
Andrew Breksa
Greenhorn
Posts: 7
MySQL Database PHP Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds good. Thanks!
 
Marshal
Posts: 76487
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult for this forum. Moving discussion.
 
And then the flying monkeys attacked. My only defense was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic