java - How to pack 2 long values in one single string and get them back -
please me ideas, how in java:
input:
long id1 = 123456 (any long value) long id2 = 4490390904 (any long value)
output:
a94dhjfh4990-d49044 (whatever, string)
and then, based on output id1
, id2
. so, kind of enconding, hashing. not obvious (like 123456###4490390904) don't want encryption level...
wonder if there can use provided java library (so no 3rd party libraries) or algorithms (code snippets).
update similar : https://github.com/peet/hashids.java
a shot in dark, since not precise on requirements. following provides 2 functions encode both value in string
, decode string
array of long
. each long value formatted in hexadecimal.
public class idencoder { static final string sep = "-"; public static string encode(long id1, long id2) { return long.tohexstring(id1)+sep+long.tohexstring(id2); } // assume paramater @ right format. // several exceptions can thrown. add exception handling necessary... public static long[] decode(string id) { string[] ids = id.split(sep); long[] idl = new long[2]; idl[0] = long.parseunsignedlong(ids[0], 16); idl[1] = long.parseunsignedlong(ids[1], 16); return idl; } public static void main(string[] args) { string ids = encode(123456, 675432187); system.out .println(ids); long[] idl = decode(ids); system.out .println(arrays.tostring(idl)); } }
prints
1e240-284246fb
[123456, 675432187]
Comments
Post a Comment