Pergunta

I want to encode byte[] to Base8 String i.e) 0 to 7. Ofcourse, Base64 is commonly used and is having lesser memory footprint. However, I am in need to do Base8 encoding. Is there any API available for this or do I have to write my own? Thanks

Foi útil?

Solução

It's not clear how base 8 works if you have multiple bytes. I assume you want to minimise the size of the message so you can do

// print in base 8 (or octal)
String text = new BigInteger(1, bytes).toString(8);

To do the reverse you can do

byte[] bytes = new BigInteger(text, 8).toByteArray();
if (bytes.length > 1 && bytes[0] == 0) {
    // remove the first byte
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top