How do I convert a large binary String to byte array java?

后端 未结 3 1985
谎友^
谎友^ 2020-12-11 22:28

I have a large binary string \"101101110...\", and I am trying to store it into a byte array. what is the best way of doing it?

Lets say I have largeString = \"01001

3条回答
  •  清歌不尽
    2020-12-11 22:53

    Do it in a loop. Split the string at 8-character chunks and convert them separately. In "pseudocode" it's something like:

    byte[] result = new byte[subs.size()];
    
    int i = 0;
    int j = 0;
    while(i+8 <= s.length){
        result[j] = new Byte.valueOf(largeString.substring(i, i+8), 2);
        i+=8;
        j++;
    }
    
    result[j] = new Byte.valueOf(largeString.substring(i, largeString.length));
    

提交回复
热议问题