This question already has answers here:
How do I convert a large binary String to byte array java?

(3个答案)


3年前关闭。




我想将0和1存储到内存中

我不知道如何清楚地解释这一点,但我会尽力做到这一点。
假设我有一个大约420bytes的IMAGE文件。

red icon

我想可视化其二进制代码,这意味着我想看到0和1。我运行这段代码来做到这一点,并且效果很好...

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);
    }
}


我发送FF0000.png作为输入,并获得以下输出...

100010010101000001001110010001110000110100001010000110100000101000000000000000000000000000001101010010010100100001000100010100100000000000000000000000001000000000000000000000000000000010000000000010000000011000000000000000000000000011000011001111100110000111001011000000000000000000000000000000010111001101010010010001110100001000000000101011101100111000011100111010010000000000000000000000000000010001100111010000010100110101000001000000000000000010110001100011110000101111111100011000010000010100000000000000000000000000001001011100000100100001011001011100110000000000000000000011101100001100000000000000000000111011000011000000011100011101101111101010000110010000000000000000000000000100111001010010010100010001000001010101000111100001011110111011011101001000110001000000010000000000000000000011001100001110100000111110100011011110111101000010010000100100000111000011101101100001101101010001111001011100000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010011001000010110110011110110101011011010001100001110111001011110010011001011111011001101010000000000000000000000000000000000100100101000101010011100100010010101110010000100110000010000010


我了解这是此特定文件的存储方向(如果我对这些术语有误,请纠正我)。

现在,假设我没有图像文件,也没有检索任何图像文件的二进制代码。我唯一拥有的是0和1,而我不知道这组0和1是否实际上代表一个文件。我不知道这代表什么。

我想将此0和1插入/加载到计算机内存中。我怎样才能做到这一点?

这可以称为我先前的操作的反向过程,在该过程中,我从文件中检索了二进制代码。现在,我想在内存中插入一些0和1,并将其另存为文件。那不需要是IMAGE文件,任何文件扩展名都可以。因为我假设我不知道任何图像文件的存在。

因此,我的主要任务是设置0和1,并将其加载到内存中并另存为文件。有可能这样做吗?如何使用Java或任何其他编程语言来做到这一点?此内存和二进制表示如何工作?

对不起,我很笨,谢谢您的耐心:)

最佳答案

给定名为String的二进制文件str和名为OutputStream的某种FileOutputStream(例如out):

对于str中的每8个字符,使用Integer.parseInt获取字节的数值,然后将write转换为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);
}


请注意,如果IndexOutOfBoundsException不是8的倍数,这将导致str.length

09-30 15:17