0

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.

red icon

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...

import java.util.Scanner;
import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class fileToBin {
    public static void main(String[] args) throws Exception {

        StringBuilder sb = new StringBuilder();

        Scanner ana = new Scanner(System.in);
        System.out.println("File?");
        String fileName = ana.nextLine();

        try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(fileName))) {

            for (int b; (b = is.read()) != -1;) {

                String s = "0000000" + Integer.toBinaryString(b);

                s = s.substring(s.length() - 8); 
                sb.append(s);
            }
        }

        System.out.println(sb);
    }
}

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

100010010101000001001110010001110000110100001010000110100000101000000000000000000000000000001101010010010100100001000100010100100000000000000000000000001000000000000000000000000000000010000000000010000000011000000000000000000000000011000011001111100110000111001011000000000000000000000000000000010111001101010010010001110100001000000000101011101100111000011100111010010000000000000000000000000000010001100111010000010100110101000001000000000000000010110001100011110000101111111100011000010000010100000000000000000000000000001001011100000100100001011001011100110000000000000000000011101100001100000000000000000000111011000011000000011100011101101111101010000110010000000000000000000000000100111001010010010100010001000001010101000111100001011110111011011101001000110001000000010000000000000000000011001100001110100000111110100011011110111101000010010000100100000111000011101101100001101101010001111001011100000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010011001000010110110011110110101011011010001100001110111001011110010011001011111011001101010000000000000000000000000000000000100100101000101010011100100010010101110010000100110000010000010

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 :)

goldenrati0
  • 332
  • 1
  • 2
  • 8

1 Answers1

1

Given a String of binary called str and some sort of OutputStream (e.g. a FileOutputStream) called out:

For every 8 characters in str, get the byte's numerical value with Integer.parseInt, and write it to out.

String str = ...;
OutputStream out = ...;

for (int i = 0; i < str.length; i += 8) {
    String byteStr = str.substring(i, i+8);
    int byteVal = Integer.parseInt(byteStr, 2);
    out.write(byteVal);
}

Note that this will cause an IndexOutOfBoundsException if str.length isn't a multiple of 8.

qxz
  • 3,814
  • 1
  • 14
  • 29
  • Let's say `str.length` is not a multiple of 8 then should I add zeros to the `str`? If YES then where should I add those? At the beginning or at the end of the `str`? – goldenrati0 Dec 10 '16 at 23:29
  • You could do either; both would give you valid sequences of bytes. Where is this data coming from? – qxz Dec 10 '16 at 23:35