[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mysql-java
Subject:    Re: Negative values being returned incorrectly
From:       "Mark Matthews" <mmatthew () thematthews ! org>
Date:       2002-04-11 17:45:58
[Download RAW message or body]


----- Original Message -----
From: "Jerry Jalenak" <Jerry.Jalenak@LABONE.com>
To: <java@lists.mysql.com>
Sent: Thursday, April 11, 2002 10:51 AM
Subject: Negative values being returned incorrectly


> I am using Java (j2sdk1.4.0) on a Windows NT platform with mySQL version
> 3.23.  I am generating a random 12 byte array using the SecureRandom
method.
> I then convert this byte array to a String object, and store it in mySQL
as
> a BLOB type.  The code is
>
>                     byte[] newDigestKey = new byte[12];
>                     SecureRandom sr = new SecureRandom();
>                     sr.nextBytes(newDigestKey);
>
>                     String newdigestKey = new String(newDigestKey);
>
> Typical byte array values being returned are as follows:
>
> -18 104 119 -19 -122 87 -76 114 81 -94 96 -34
>
> I have verified that I can convert the byte array to a String object, and
> then convert it back to a byte array and get the original values.
>
> When I use a Select statement to retrieve the values from mySQL and
convert
> the returned String object back to a byte array, I am getting different
> values.  This code is
>
> byte[] currentDigestKey;
> currentDigestKey = rs.getString("digestKey").getBytes();
>
> The values that are returned are
>
> -18 104 119 -19 32 87 -76 114 81 -94 96 -34
>
> The value for the 5th byte has changed.  I suspect it has something to do
> with values greater than -99 not being 'escaped' correctly, thereby
> resulting in an different value.  Can someone point me in the right
> direction on how to handle this?


You can't expect the byte->string or string->byte conversion to be opaque,
as Strings are made of characters, and there are interactions between your
platform's unicode encoding, and what MySQL is using.

You need to use the setBytes()/getBytes() methods to make this work
correctly.

    -Mark


---------------------------------------------------------------------
Please check "http://www.mysql.com/Manual_chapter/manual_toc.html" before
posting. To request this thread, e-mail java-thread3595@lists.mysql.com

To unsubscribe, send a message to the address shown in the
List-Unsubscribe header of this message. If you cannot see it,
e-mail java-unsubscribe@lists.mysql.com instead.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic