Skip to content

Instantly share code, notes, and snippets.

@naturallucky
Created May 31, 2017 17:02
Show Gist options
  • Save naturallucky/47614cfd0ec575bcca9bbdddeed3ed05 to your computer and use it in GitHub Desktop.
Save naturallucky/47614cfd0ec575bcca9bbdddeed3ed05 to your computer and use it in GitHub Desktop.
//60,67
//45 81
//32,95
private String convertCharPack(int value, int charnum , int base , int packsize){
int b;
char[] ret = new char[charnum];
for (int i = 0; i < charnum; i++) {
b = value % packsize;
ret[i] = (char)(b+base);
value = value / packsize;
}
return new String(ret);
}
private int convertCharDepack(String chars, int base , int packsize){
int l = chars.length();
int v = chars.charAt(l-1)-base;
for (int i = l-2; i >= 0; i--) {
v = v * packsize + chars.charAt(i)-base;
}
return v;
}
//60 -> 64 = 2^6 , ? % 64 = ? & 63
private String convertCharPack2(int value, int charnum , int base, int packbeki, int packm){
int b;
int fb = value;
char[] ret = new char[charnum];
if (value < 0){
Log.d(LOGID, "not plus parameter: pack:" + value);
}
for (int i = 0; i < charnum; i++) {
b = value & packm;
ret[i] = (char)(b+base);
value = value >> packbeki;
}
if (value > 0){
Log.d(LOGID, "too big parameter: pack:" + fb);
}
return new String(ret);
}
private int convertCharDepack2(String chars, int base, int packbeki){
int l = chars.length();
int v = chars.charAt(l-1)-base;
for (int i = l-2; i >= 0; i--) {
v = (v <<packbeki) + chars.charAt(i)-base;
}
return v;
}
@naturallucky
Copy link
Author

Outline:
Convert string to N-index.
like perl's pack64 function.
There are two type method and each type has two method.
version 1:More Flexible.
version 2:More Fast. (It can pack only 2^n pack-range type)

数値を文字に変換し圧縮します。
2typeあり、柔軟性のあるバージョンと、2のべき乗に制限した高速バージョンです。

Usecase:
For compressing string at save or through network.
保存時/ 通信時などの文字圧縮などに。

IN/OUT example
60 <-> x (base 60 : pack 64 (2^6), charnum 1 : int range 64^1)
333333 <-> QTM= (base 60 : pack 64 (2^6), charnum 4: int range 64^4)
assemble from upper side 'Q' (with small int value '333333 mod 64')

range example:
char('<') ASCII(60) ~ char('~') ASCII(60+67)
char('A') ASCII(65) ~ char('Z') ASCII(65+25) : ONLY BIG LETTER ASCII

Usage:
sb.append(convertCharPack2( value,4,60,6,63))
or
convertCharDepack2(data.substring(i + 1,i+3),60,6)

Recommend pack pattern
//60,67 base 60 ~ 60+67
//45 81
//32,95

License:
For free at your own risk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment