Shankar Guru
Shankar Guru

Reputation: 1161

Convert to long from a given string and return back same string when decoded

I need to convert a string like "2015114734WO|https://example.com/myresearch/2015114734WO/work/data:/1" to a long data type and vice versa. It should get converted to a long data type and when algorithm is re-applied, I need to get back "2015114734WO|https://example.com/myresearch/2015114734WO/work/data:/1". Please let me know if there is any ready algorithm I can use?

Thanks.

Upvotes: 0

Views: 46

Answers (1)

g00se
g00se

Reputation: 4296

Assuming you mean 'long' as in integral, you could use BigInteger. e.g.

BigInteger bi = new BigInteger(s.getBytes("UTF-8"));

'Decode'

            byte[] b = bi.toByteArray();
            for(int i = 0;i < b.length;i++) {  
                System.out.print((char)b[i]);
            }

Upvotes: 2

Related Questions