Integer sizes save the day

I was looking to convert some string data into integers – the data was bytes & I needed it in GB.  The value I needed was buried in the middle of a string so I needed to do some string processing. Some other conditions forced me to use the Parse method of the integer class

Lets say I get a number like 2147483169 back in the data & I need to divide by 1gb

£> [int]::Parse("2147483169") / 1gb
1.99999955389649

I’ve put the string value directly in here rather than the complicated string processing I was actually using.

The point came when I realised that I could have much bigger numbers than 2Gb so what was the maximum I could work with

£> [int]::maxValue
2147483647
£> [int]::maxValue / 1gb
1.99999999906868

 

OK thats not enough.  The standard [int] is 32bits & I know its big brother has 64 bits so how big is big brother

£> [int64]::maxValue
9223372036854775807
£> [int64]::maxValue / 1gb
8589934592

That’s more than big enough so I went with 64 bit integers.

This isn’t just a developer type thing because I was working with Exchange mail box sizes.  So the moral of the story is to think about the possible values you’ll see in you data while you are coding to avoid errors or failure when you are running the script.

Leave a Reply