• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I store/load 0s and 1s into memory  RSS feed

 
Tahmid Choyon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to store some 0s and 1s into memory
I do not know how to explain this clearly but I will try my best to do so.
Let's say, I have an IMAGE file of around 420bytes.

I want to visualize its binary code meaning I want to see the 0s and 1s. I run this piece of code to do that and this works just fine...


I send FF0000.png as input and got the following as output...


I understand that this is the memory orientation(please correct me if I am wrong about any of these terms) of this particular file.

Now, let's say I do not have nay image file and I did not retrieved and binary code of any image file. The only thing I have is this 0s and 1s and I do not know whether this set of 0s and 1s actually represent a file or not. I have no idea what this represents.

I want to insert/load this 0s and 1s into computer memory. How can I do that?
This can be called the reverse process of my earlier action where I retrieved binary code from a file. Now, I want to insert some 0s and 1s into memory and save it as a file. That does not need to be an IMAGE file, any file extension can be okay. Because I assumed that I am not aware of the presence of any image file.

So, my main task is I have some 0s and 1s and I want to load it to memory and save as a file. Is it possible to do that? How can I do this with Java or any other programming language? How does this memory and binary representation work?

Sorry for my noobness and thank you for your patience
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look in the Java™ Tutorials and see whether that section helps.
I cannot see any evidence of red in that horrible long String of 1s and 0s, irrespective of endian‑ness. I would expect to see
0000_0000_1111_1111_0000_0000_0000_0000
in bigendian and in littleendian,
0000_0000_0000_0000_1111_1111_0000_0000
You don't have repeated runs of four 1s.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A png file uses compression and it might be using a header. Many programs recognize a png file even if the extension is not png. And although the image looks red, maybe only the indices in a 256 color table are stored.

In general, reading the bits of a file is useless if you do not know the file format in question.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's compressed. As you say, there is no point in trying to read the individual bits.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many files, by convention, use the first 4 bytes as a "magic" number that are usually ASCII characters relating to the file type.
I believe the bytes for a JPG file are "jfif". There isn't always rhyme or reason to the 4 bytes but if you are working with many
of the same type of file you might see a pattern. Sometimes you can see what the bytes are by forcing it to open with a text editor.

EDIT: Only somewhat correct. There may be some number, more or less than 4, that make up the magic number. Here's a document that mentions some of them. https://en.wikipedia.org/wiki/List_of_file_signatures

For a PNG file the first eight bytes, in hex, are: 89 50 4E 47 0D 0A 1A 0A
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the idea of writing the data out in binary format, so you could see the 0's and 1's. But I don't understand the idea of saving and restoring the file in that format. That seems like the hard way to do it, not to mention that it takes up 8 times as much space on disk. So may I ask what's the purpose of this exercise? Most people would just write the bytes out to disk and read them in from disk, nothing more than that.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
EDIT: Only somewhat correct. There may be some number, more or less than 4, that make up the magic number. Here's a document that mentions some of them. https://en.wikipedia.org/wiki/List_of_file_signatures

For a PNG file the first eight bytes, in hex, are: 89 50 4E 47 0D 0A 1A 0A

Java class files: CAFE BABE
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!