Geek Hell


(this post is pure geek - you can move along to the next blog in your RSA feed : )


                                                                


I have always liked the Java programming language; still do, but today I found a weird one.

I was writing code to send stream of bytes to an LDAP (Lightweight Directory Access Protocol) server, doing address resolutions - nothing fancy. Standard BSD socket stuff. I was reading the bytes to read as hex integers out of a text file - 04 5a 4b 40 etc etc etc - and converting each pair of characters to a byte value.

I'd come up with a way that seemed towork when I ran into a weird one. I was reading these two-digit strings in and using parseByte to turn them into byte values:

            s = byteFile.readLine();                            
                         //this reads in a line of text full of those two-char bytes - "04 64 4c 6b 6f..."
            String[] byteStrings = s.split(" ");              
                         //this creates an array of short strings, each two bytes long."04", "64", "4c", "6b", "6f"....
            for (int i=0; i<byteStrings.length;i++)
                 byte b = Byte.parseByte(byteStrings[i],16);
                               //this goes through and turns each of those short strings into a byte value


to get the bytes to stream out through the socket. Suddenly I get an error -

            Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"a5" Radix:16

Yep, that's right. a1 in hex - 161 to you and me, in decimal - is outside of the byte range, which is (I thought) 0-255.

Okay, sure - it's "signed byte", right? And that's using up one of the bits, making the range -128 to 127?

Doesn't seem like that's the problem.

I messed with this for quite a while, and couldn't figure it out; finally a friend suggested this route of investigation:

                    byte b = (byte) Integer.parseInt(byteStrings[i],16);

...and that WORKS.

Java is saying "Hey, I can't turn that string into a byte for you, but I can turn it into an INTEGER, and I can turn THAT Integer into a byte".

It reminds me of when my eldest brother, Chuck, was very sick as a small child - maybe age 4 or 5. He had some sort of respiratory illness that wouldn't go away - it just kept hanging on and on.

Eventually the doctor told my father to take Chuck and douse him in icewater.

Dad said "But...but that'll give him pneumonia!"

The doctor said "Yes, it will - and pneumonia we can cure!"

If you can't solve one problem, turn that problem into a problem you can solve.


         

 del.icio.us  Stumbleupon  Technorati  Digg 

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this entry.
Comments

  • 7/2/2009 8:37 PM Jenny wrote:
    Awwww...This post made me feel like you were sitting in the cube next to me again...I think I'm gonna cry!
    Reply to this
  • 7/3/2009 9:07 AM Damon wrote:
    I had a similar one displying a PDF generated by iText back to a user through Struts and JSP. The solution had always worked and suddenly stopped.

    It made no sense that it wouldn't work any more (yes, we had upgraded the JVM, but nothing was deprecated). I simply re-wrote it another way, pulling the bytes from the server file to the display in a completely different manner. Both should have worked as far as I can tell, but only one of them did. But, I made my users and another programmer very happy when I did solve the problem, so that's good enough.
    Reply to this
Leave a comment

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.