Saturday, August 20, 2016

Converting byte array to string without loosing data


Recently I had to deal with poor Redis interface, that didn't allowed to store bytes and it was not possible to update it or change. Redis handles bytes perfectly for keys and data, but there was no way to pass it. Standard solution for this task is to use Base64, but for my case overhead was too big as I had to store hundreds of millions of records. UTF is not good too, as it is ruining data. Finally, I found that right encoding for this task is to use ISO-8859-1 encoding and it works just fine.


new String([10,20,-30,126] as byte[], "ISO-8859-1").getBytes("ISO-8859-1");