java - How to decode a 9 digit integer to some random 4 digit word -


how encode 7 digit integer 4 digit string in java?

i have base36 decoder, generating 6 characters,

ex:230150206 converted 3t0x1a.

the code follows:

string f = "230150206"; int d = integer.parseint(f.tostring());     stringbuffer b36num = new stringbuffer();     {         b36num.insert(0,(base36(d%36)));         d = d/ 36;     } while (d > 36);     b36num.insert(0,(base36(d)));     system.out.println(b36num.tostring());     }      /**     take number between 0 , 35 , return character reprsenting     number. 0 0, 1 1, 10 a, 11 b... 35 z     @param int number change base36     @return character resprenting number in base36     */     private static character base36 (int x) {     if (x == 10)          x = 48;     else if (x < 10)         x = x + 48;     else          x = x + 54;      return new character((char)x);    } 

can 1 share me other way achieve this?.

the obtained string can made in substring, looking other way it.

here method, in simple test program. method allows string represent digits result. initial print shows, 62 digits should sufficient cover 7 decimal digit numbers no more 4 character output, recommend decimal digits, lower case alpha , upper case alpha 7 digit case.

to cover 9 decimal digits in 4 encoded digits need @ least 178 characters, not possible using 7-bit ascii characters. have decide additional characters use digits.

public class test {   public static void main(string[] args) {     string characters = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";     system.out.println(math.pow(characters.length(), 4));     testit(230150206, "0123456789abcdefghijklmnopqrstuvwxyz");     testit(230150206, characters);   }    private static void testit(int num, string characters){     system.out.println(num + " "+compact(num, characters));   }    public static string compact(int num, string characters){     stringbuffer compacted = new stringbuffer();     while(num != 0){       compacted.insert(0, characters.charat(num % characters.length()));       num /= characters.length();     }     return compacted.tostring();   } } 

output:

1.4776336e7 230150206 3t0x1a 230150206 fzga6 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -