Sunday, 26 December 2010

atoi do it yourself

This interesting post came up on my twitter feed today. Given it's the holidays and I have now finished my PhD, I don't know why I read it, but I did.

You can find it here

But in essence this is a discussion on how to convert a string to an int without making use of the built-in tools to do this - in fact, in Python, this is usually as simple as casting;

a = "1234"
b = int(a)

but in this blog we are shown how to use the ord method to get the int value of each character, then multiply by 10, 100, 1000 etc depending on the position in the string.

Happy Christmas everyone!

No comments:

Post a Comment