Difference in a key yielded from a string, using a long or aBigInteger

  • Thread starter Sébastien de Mapias
  • Start date
S

Sébastien de Mapias

Hi,
I'd like to make a numeric key value from a String I receive.
I've tried these 2 ways with a different result:

String s3 = "ABCD";
char s2c3[] = s3.toCharArray();
long l2 = 0;
BigInteger bi = new BigInteger("0");

for (int i=0; i<s2c3.length; i++) {
Integer j = Character.getNumericValue(s2c3);
bi = (bi.shiftLeft(8)).add(new BigInteger(j.toString()));
}
System.out.println("ABCD vaut "+bi.longValue());

for (int i=0; i<s2c3.length; i++) {
l2 = (l2<<8) + (s2c3&0xff);
}
System.out.println("ABCD vaut "+l2);

I get the following output:
ABCD vaut 168496141
ABCD vaut 1094861636

Why are the results different ? What am I missing here ?

Thanks.
Regards,
SR
 
L

Lothar Kimmeringer

Sébastien de Mapias said:
I'd like to make a numeric key value from a String I receive.
I've tried these 2 ways with a different result:
[...]
Integer j = Character.getNumericValue(s2c3); [...]
l2 = (l2<<8) + (s2c3&0xff); [...]
Why are the results different ? What am I missing here ?


You should read the Javadoc-description of getNumericValue:

| The letters A-Z in their uppercase ('\u0041' through '\u005A'),#
| lowercase ('\u0061' through '\u007A'), and full width variant
| ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms
| have numeric values from 10 through 35. This is independent of the
| Unicode specification, which does not assign numeric values to
| these char values.

So the BigInteger-value will be created with the values
10, 11, 12, 13 while the primitve-long value will be created
with the values 65, 66, 67, 68


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
S

softwarepearls_com

Hi,
I'd like to make a numeric key value from a String I receive.
I've tried these 2 ways with a different result:

    String s3 = "ABCD";
    char s2c3[] = s3.toCharArray();
    long l2 = 0;
    BigInteger bi = new BigInteger("0");

    for (int i=0; i<s2c3.length; i++) {
        Integer j = Character.getNumericValue(s2c3);
        bi = (bi.shiftLeft(8)).add(new BigInteger(j.toString()));
    }
    System.out.println("ABCD vaut "+bi.longValue());

    for (int i=0; i<s2c3.length; i++) {
      l2 = (l2<<8) + (s2c3&0xff);
    }
    System.out.println("ABCD vaut "+l2);

I get the following output:
ABCD vaut 168496141
ABCD vaut 1094861636

Why are the results different ? What am I missing here ?

Thanks.
Regards,
SR


Do your keys have to be unique? If not, how about using String's
hashCode() method?
 
R

Roedy Green

Integer j = Character.getNumericValue(s2c3);


Unicode is so designed that '0' is not 0, but rather 48 aka 0x30.

So to convert a numeric char to binary you need to subtract '0'.

'A' is not 10 but rather 65 aka 0x41, for A-F hex chars you must
subtract ( 'A' + 10 )
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top