簡體   English   中英

二進制到Byte []數組

[英]Binary to Byte[] Array

我將字節數組轉換為二進制字符串。 然后,當我嘗試將其從二進制字符串轉換回字節數組時,其結果與初始的coverObjData略有不同。

public class StandardLSB implements Stegano{
    private File coverObject;
    private String getBits(Byte b) {
        String result = "";
        for(int i=7;i>=0;--i)
            result += (b&(1<<i))==0 ? "0" : "1";
        return result;
    }

    private String getBits(byte b[]) {
        String result = "";
        for(int i=0;i<b.length;++i)
            result += getBits(b[i]);
        return result;
    }    

    public static void main(String args[]){
        byte[] coverObjData = Files.readAllBytes(coverObject.toPath());
        String coverBits = getBits(coverObjData);
        byte[] stegoData = new BigInteger(coverBits.toString(), 2).toByteArray();
    }
}

初始coverObjData在記事本中如下所示:

ffd8 ffe1 1683 4578 6966 0000 4949 2a00
0800 0000 0f00 0001 0300 0100 0000 8002
...
...
3a80 907d 971f 8f7f d593 6b4c 81da b8a0
bb15 762a 85d4 e710 584f 2568 4290 0fb9
d87e bc

stegodata在記事本中看起來像這樣:

00ff d8ff e116 8345 7869 6600 0049 492a
0008 0000 000f 0000 0103 0001 0000 0080
...
...
fa3a 8090 7d97 1f8f 7fd5 936b 4c81 dab8
a0bb 1576 2a85 d4e7 1058 4f25 6842 900f
b9d8 7e

如何解決此問題?

您可以簡單地使用需要字節數組的構造函數:

public static void main(String args[]){
    byte[] coverObjData = Files.readAllBytes(coverObject.toPath());
    byte[] stegoData = new BigInteger(coverObjData).toByteArray();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM