Integer handling

Apr 11, 2008 at 7:23 AM
Edited Apr 11, 2008 at 7:24 AM

I saw your blog post on Linq, SimpleDB and integers.

It got me thinking and there seem to be two problems (sorry if you already handled these cases):

1. Zero-padding to ensure lexicographical comparision. That is fairly easy and the number of paddings you need should be easy to extract from the datatype in use. E.g. Int32 will need 10 digits at the most (11 for sign).
It might be an idea to store the numbers in the hex equivalents as you would need a lot less digits then.

2. Negative numbers.
What you want is to be able to query for numbers and have < and > work properly.
I see two solutions for this:

2a) If you are working with signed Int32 then begin to work with unsigned Int32 instead, and the number you add should be uint32.Max/2, then you can handle everything that int32 can handle. Same scheme will work for decimals, etc.

2b) If you "encode" your numbers this way, everything should work as expected, but you of course need to handle the encoding/decoding automatically.
First, pad all positive numbers as before, but add an extra "0"
Second, negative numbers have one less padding - instead they start with "-"
Third, the digits in the negative number are reversed, so the padding actually is to the right of the number.
(4+1 digit for clarity)

1 = 00001
20 = 00020

-1 = -1000
-20= -0200

Please note that "-" has a lower ascii number than "0", so you should add an extra "0" to the positive numbers.

Either way could handle your integer problem. What to choose? Your choice.
Jun 25, 2009 at 6:27 PM

Hi Justin,

I also read your post on data-type conversions with SimpleDB (

You may be interested in checking my approach to this for Nice job on this library, by the way.