Problems with Java'’s parseInt

Posted on 2010-04-01 21:41 in Blog • Tagged with java, programming

Background

I work in an industry that generates a lot of binary encode data. Primarily from the lab at my office, but occasionally we get data that was collected at a government test range. In order to understand the data, it goes through a process called, “Data Reduction”, which converts the binary/ hex encoded data (test data) into simple decimal numbers (engineering data) that can get used by excel. The data fields vary in size, from a byte up to a long, and it is usually unsigned data. A tool was written last spring (2009) with the help of a regional university to automate the data conversion process and it is currently being extended (2010).

Problem

The students discovered that negative numbers were not being parsed correctly. Sometimes giving an invalid value (byte: 0x80), other times they caused a NumberFormatException (int: 0xFFFFFFFF). I told them I would look into the problem. It quickly became apparent what the problem was; Integer.parseInt doesn’t under two’s complement values.

parseInt takes a hex encoded string and returns an int. In the case of negative numbers, parseInt looks for a negative sign at the beginning of the string. It does not examine the hex code within the string. When a negative byte or short was parsed, the method treated it as a positive int, and parsed accordingly. When a negative int was provided, the method threw an exception.

Cause of the Exception

The Java int has “two” fields: 31 bits of data, and 1 bit of sign. When a negative hex string was provided, parseInt saw 32 bits of data, determined it was an overflow condition, and threw the exception.

Solution

With the root cause of the problem in hand, I was quickly able to write a solution. I first wrote a routine that determined if the proved string was negative, based on its type (or size). If it was negative, I replace the original data string with its two’s complement and added a negative sign to the front. Now, parseInt returns the expected results.

Code

/** Test if string is negative  
 *  
 * @param number - hex string containing data  
 * @param bytes - number of bytes in data type (8 bit vs 16 bit, etc)  
 *  
 * @ return True means the number is negative  
 */  
public static boolean isNumberNegative(String number, int bytes){  
    if((number.length() / 2) < bytes ){ // check if top bit even has a value in it  
        return false;  
    }

    // extract first two characters into number  
    int bit = Integer.parseInt( number.substring(0,2), 16);  
    bit >>= 7; // down shift seven bits

    return (bit == 1); // top bit set == negative  
}

/**  
 * Make the hex string negative by performing two's complement  
 * @param hex the string to make negative  
 * @return input string after 2's complement reversal  
 */  
public static String makeNegativeInt(String hex){  
    String finalString = "";  
    int[] list = new int[ hex.length() / 2 ];

    // copy hex string into array  
    for(int i = 0; i < hex.length() / 2; i++){  
        list[i] = Integer.parseInt( hex.substring(i*2, i*2 +2), 16);  
    }

    // make each block negative  
    int add = 1;  
    int topBitOld = 0;  
    int topBitNew = 0;

    for(int i = list.length -1 ; i &gt;= 0; i--){  
        list[i] ^= 0xFF; // flip bits  
        topBitOld = (list[i] >> 7) & 1;             // save old top bit  
        list[i] += add;                             // possibly add bit  
        topBitNew = (list[i] >> 7) & 1;             // save new top bit  
        if((topBitOld == 1) && (topBitNew == 0)){   // check if overflow occurred  
            add = 1;  
        } else {  
            add = 0;  
        }  
        list[i] &= 0xFF;  
    }

    // rebuild hex string  
    for(int i = 0; i &lt; list.length; i++){  
        finalString += padHex(list[i], 2);  
    }

    return finalString;  
}

/**  
 * This pads a number with zeros, after it has been converted to  
 * hexidecimal format, sans 0x.  
 *  
 * @param number Number to be converted to hexidecimal  
 * @param length Final length of padded string  
 * @return String after padding  
 */  
public static String padHex(int number, int length){  
    char padding = '0';  
    String s = Integer.toHexString(number);  
    while(s.length() < length){  
        s = padding + s;  
    }  
    return s;  
}  

Continue reading

XmlHttpRequest

Posted on 2010-03-31 23:10 in Blog • Tagged with javascript, programming

I was reading about Websockets and their usefulness the other day when I realized how little of their precursor, the XmlHttpRequest, I actually knew. I performed some research and code a few quick and dirty example pages and discovered a new realm of capabilities that I was previously ignorant about.

For years, I had assumed AJAX technology was only useful if there were server side scripts interpreting the requests, gathering the needed data, and returning well formatted responses. These responses were then used to update the page content.  While coding one of the examples, I had an ‘Ah-ah moment’.  AJAX capabilities could be utilized to load static content when updating the page.  Instead of having to address PHP or ASP pages, plain old HTML files could be the target.

Within minutes, I had seamlessly written a basic homepage, consisting of several sections that loaded all the content without a single page refresh. This game me a liberating feeling. Now I can update the content without losing my JavaScript variable values.

Quick Example

The simplest way to dynamically load content is to specify

/* Create the request object */  
if (window.XMLHttpRequest) {        // Modern browsers  
    xhttp = new XMLHttpRequest();  
} else {                            // Old Internet Explorer  
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");  
}
/* Perform the request */  
xhttp.open("GET","external\_file.txt",false);  
xhttp.send("");  
destinationElement.innerHTML = xhttp.responseText;

This creates a new XmlHttpRequest object across multiple browser types.  The ‘open’ command defines how the file will be retrieved (GET vs. POST) and the URL for the file.  Send performs the actual communication with the server.  After the communication is complete, responseText is extracted and displayed to the user.

This block of code is very straightforward and very misleading.  It performs no error handling (file not found) and does not take into account the amount of time needed to retrieve file (server load).

Better Example

A better approach would be to use a JavaScript library that is designed for this type work, JQuery.

$(".destinationElement").load("external_file.txt");

This single line of JavaScript performs the same function as the block of code above. It says load the contents of the files, external_file.txt, into the DOM element whose title is ‘destinationElement’.

Again, this over looks the same errors the first block contained, errors while loading the file and what not.  Luckily, JQuery has built in error handling events. As a programmer, we can define what action should be taken when a query fails.

$(window).error(function(msg, url, line){  
    // handle the error  
});  

Quick note: The above block will catch all errors that occur, not just errors AJAX errors.


Continue reading

Mercurial

Posted on 2010-03-31 22:54 in Blog • Tagged with programming, competition, version control

For the last three weeks, development on my news program, Iceberg Races, has been in high gear. Each night before bed, I simply zipped up my work and archived it away. This approach was fine at first, as I was hammering out the design as I coded, which meant I took a lot of dead end paths. Large chunks of code and entire files were created and then deleted the next day (as I discovered better implementation methods.) It’s true that the best way to learn a language is to dive right in by writing ‘useful’ program.

On Wednesday, I realized that my code structure had stabilized a week prior and I was overdue for tracking my code in a source revision tool. Luckily, I ran across an amazing Mercurial tutorial and decided to give it a try.

So far I’m loving it. The commands are all straight forward. It was extremely quick to setup, and has perform admirably for the past two three days that I’ve been using it. I’ve summarized the most useful commands below (this is not an exhaustive list):

Command Description
hg * All commands start with hg followed by…
init Create a new repository in current directory.
add Add new files to repository
remove Remove file from repository
status View the status of the files in the repository.
diff View the differences between two different versions of a file.
commit Save the current state of the files to the repository.
rollback Undo the last one commit.
merge Resolve conflicts in a file.
log View the revision history.
update Check out files to a specified revision
push Copy changes in local repository to a remote repository.
pull Copy changes in a remote repository and bring them into your local repository.

I’ve bolded the commands that come in the handiest. This includes the operations to save the current state of the repository and how to restore a past state.


Continue reading

A home at last!

Posted on 2010-03-31 22:35 in Blog • Tagged with blog

After many years adrift in the ocean of the Internet, my iceberg has finally run aground. That's right, I now have my vary own website. It has literally been years in the making. Over the past several years, I have put together a dozen or so small websites for fun. I wrote one for my anime group, Kitsune-Udon, the Agnostic club I was a member of while in college, a warehouse containing my anime collection, and several other small projects. In addition, I've also had two unsuccessful blogs from different providers.

Finally, I have a single place to collect all my various pieces of data. My plan is for this site to be a dump of everything that is me. I'll blog about my interests, discuss my current job happenings, share my thoughts with the rest of the software community, and hopefully gain a following which I can use to conquer the world. One subscriber at a time...


Continue reading

Interesting Equation

Posted on 2009-06-24 20:59 in Blog • Tagged with math

111111111 x 111111111 = 12345678987654321

Continue reading