Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hello stackoverflow community,

I need to convert a byte array to a binary byte-array (yes, binary bytes). See this example:

byte[] source = new byte[] {0x0A, 0x00};
//shall be converted to this:
byte[] target = new byte[] {0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00};

//this would be ok as well:
also[0] = new byte[] {0x00, 0x00, 0x10, 0x10};
also[1] = new byte[] {0x00, 0x00, 0x00, 0x00};

At the moment I'm solving this by using Integer.toBinaryString to get the binary string and hexStringToByteArray to convert the binary string to a byte array:

for(int o = 0; o < cPage.getCpData().length; o+=cPage.getWidth()) {
    String fBinString = "";
    for(int u = 0; u < cPage.getWidth(); u++) {
        byte[] value =  new byte[1];
        raBa.read(value);
        Byte part = new BigInteger(value).byteValue();
        String binString = Integer.toBinaryString(0xFF & part);
        fBinString+=("00000000" + binString).substring(binString.length());
    }
    cPage.addBinaryString(fBinString);
    //binaryCodepage.add(fBinString);
    testwidth = fBinString.length();
    //System.out.println(fBinString);
}
//other class:
public byte[][] getBinaryAsByteArray() {
    Object[] binary =  getBinaryStrings().toArray();
    byte[][] binAsHex = new byte[binary.length][getWidth()*8];
    for(int i = 0; i < binary.length; i++) {
        binAsHex[i] = ByteUtil.hexStringToByteArray(((String) binary[i]));
    }
    return binAsHex;
}

This works fine for small source byte-arrays but takes ages for large byte-arrays. Thats probably caused by the conversion to binary String and back. Any ideas how to improve this by not converting the source to a String?

share|improve this question
    
On what basis should 0x0A00 become 0X00001010000000? What does 'binary byte-array' mean? What is a 'bit byte-array'? –  EJP Aug 24 '14 at 11:40
    
@EJP 0x0A == 00 00 10 10 in binary. –  Marko Topolnik Aug 24 '14 at 11:50
    
So basically, OP, you are spreading the 8 bits from one original byte into a bitfield of length 32, such that each 4th bit is used. –  Marko Topolnik Aug 24 '14 at 11:52
1  
I would suggest you don't confuse the readers by calling this process "expansion to binary". You are starting from binary form and expanding into another (and quite atypical) binary form. –  Marko Topolnik Aug 24 '14 at 11:55
    
I need to expand every byte to 8 bit binary. And the result (binary) is a representation of a new byte array. 2 bits of the binary = 1 new byte 00 = 0x00 10 = 0x10 –  var2081302 Aug 24 '14 at 11:55

1 Answer 1

up vote 1 down vote accepted

I don't know what's the motivation to this strange conversion, but you could do something similar to the implementation of Integer.toBinaryString :

private static byte[] toFourBytes(byte i) {
    byte[] buf = new byte[4];
    int bytePos = 4;
    do {
        buf[--bytePos] = i & 0x3;
        i >>>= 2;
    } while (i != 0);
    return buf;
 }

This should convert (I haven't tested it) each byte to a 4 byte array, in which each byte contains 2 bits of the original byte.

EDIT:

I may have missed the exact requirement. If the two bits extracted from the input byte should be in positions 0 and 4 of the output byte, the code would change to :

private static byte[] toFourBytes(byte i) {
    byte[] buf = new byte[4];
    int bytePos = 4;
    do {
        byte temp = i & 0x1;
        i >>>= 1;
        buf[--bytePos] = ((i & 0x1) << 4) | temp;
        i >>>= 1;
    } while (i != 0);
    return buf;
 }
share|improve this answer
    
It's not just that each output byte contains 2 bits. Those bits should be in position 4 and 0 (hex 00, 01, 10, 11; decimal 0, 1, 16, 17). –  immibis Aug 24 '14 at 13:16
    
@immibis I missed that part of the requirement. Edited the answer to account for that. –  Eran Aug 24 '14 at 13:32
    
I see. This solution looks good and should work. Will try later. In case you are curious why I need this: It's needed to decompress data stored in an embedded os firmware. –  var2081302 Aug 24 '14 at 14:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.