public static String compressHexString ( String inString ) { String in = inString + "^" ; // Add this onto the end since the last group of // characters is always dropped... This carat // will be dropped from the output string. StringBuilder out = new StringBuilder ( ) ; char lastChar = ' ' ; int count = 0 ; for ( int i = 0 ; i < in . length ( ) ; i ++ ) { char thisChar = in . charAt ( i ) ; if ( thisChar == lastChar && count < 35 ) { // Maybe we're starting a string // of repeating chars... count ++ ; } else { if ( count > 3 ) { out . append ( ' ' ) ; out . append ( lastChar ) ; out . append ( "abcdefghijklmnopqrstuvwxyz01234567890" . charAt ( count ) ) ; } else { for ( int j = 0 ; j < count ; j ++ ) out . append ( lastChar ) ; } count = 1 ; lastChar = thisChar ; } } String outString = out . toString ( ) ; return outString ; }
Very simplistic compression method that encodes repeating characters into a smaller character so 0000000000 becomes ^0j where the ^ escapes the next two characters 0 is the repeating character and j encodes 10 repetitions .