0

Other way to encode byte[] to String and decode String to byte[] without using Base64.

Because when I encode a byte[] to String and then I compress the String using LZW. I can't decode it back to byte[] using Base64. Is there an encoder or decoder which can keep decode a String although the String has modified by LZW?

Satria Winarah
  • 55
  • 1
  • 10
  • Why would you convert byte array to string before compression? It is very confusing what you are trying to achieve... Especially without language tag and [MCVE]. – Alexei Levenkov May 27 '16 at 05:46
  • Because my LZW algorithm need string for input: lzw.compress(myString); – Satria Winarah May 27 '16 at 05:52
  • Not helping... If you come up with some algorithm how do you expect people to answer question about it if there is no information provided in the post about your work? Please make sure to edit your post with language tag, [MCVE] and details of "your LZW algorithm" so someone can try to answer. (Note: I suspect that language you are using considers characters to be 16-bits while regular LZW talks about 1-byte characters which may be one of the problems in that mysterious code). – Alexei Levenkov May 27 '16 at 06:04
  • Yes but I don't mean you to answer about my algorithm. I just want to know, is there other way to encode String to byte[] and byte[] to string without using Base64. – Satria Winarah May 27 '16 at 06:27

1 Answers1

0

Not practical, but easy to implement and easily reversible encoding of bytes to Unicode characters is to encode each byte into (offset+byte_value) in such a way that all 256 values fit into some valid Unicode block.

I.e. looking at Unicode blocks range 2200..22FF (Mathematical Operators) is quite reasonable for such operation (C# sample):

 char EncodeByte(byte x) { return (char)(0x2200 + x);}
 byte DecodeByte(char x) { return (byte)(x - 0x2200);}

Note: regular LZW manipulates sequences of bytes - so no encoding necessary when starting from bytes.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179