Unicode to numerical value ?

H

hapyfishrmn

Hello,
I am trying to get a unicode string into a numerical value.

Example: "\u0001\u0012" = 65554

Is there a method that can take the string and convert it to a long/int
or even a string with the numerical value.
Thanks in advance

_hap
 
P

Piotr Pejas

Hello
I don't know method that will convert String to long, but you can use:
java.lang.Character.getNumericValue( char )
to get integer representation of character and mix this value with
65536^n where n is position of character in string and sum all values:

---cut---
String s = "abc";
long value = 0;
for( int n = 0; n < s.length(); n++ ) {
System.out.println( Character.getNumericValue( s.charAt( n ) ) );
value += Character.getNumericValue( s.charAt(n)) * Math.pow( 65536, n );
}

System.out.println( value );
while( value / 65536 > 0 ) {
System.out.println( value % 65536 );
value /= 65536;
}
System.out.println( value );
---cut---

result:
10
11
12
51540328458
10
11
12

value grows very quickly...

Best Regards
PP
--
Piotr Pejas
Java developer
Yumasoft sp. z o. o
www.yumasoft.pl
tel. +48 71 78 44 123

(e-mail address removed) napisał(a):
 
O

Oliver Wong

Piotr Pejas said:
Hello
I don't know method that will convert String to long, but you can use:
java.lang.Character.getNumericValue( char )
to get integer representation of character and mix this value with 65536^n
where n is position of character in string and sum all values:

I don't think this method does what the OP wants. Give '\u216C' (the
roman numeral 50), it will return 50 instead of 8556 (the decimal
representation of 0x216C).

- Oliver
 
O

Oliver Wong

Hello,
I am trying to get a unicode string into a numerical value.

Example: "\u0001\u0012" = 65554

Is there a method that can take the string and convert it to a long/int
or even a string with the numerical value.

I suspect you'll have to write your own, iterating through each
character, and performing the appropriate conversion, adding it to a growing
sum. The sum will grow big very quickly with respect to the length of the
String, so you'll probably have to use BigInteger, nevermind long/int.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top