Convert Binary String to Integer

Swift Binary Representation

How do you convert Binary String to it’s Integer Value?

There are many approaches to convert Binary strings to Integers. One approach could be to iterate through all of the characters in the string and add the value of 2 raised to the power of the index.

Why do I need to convert Binary?

Just for the heck of it, or maybe you might need to convert Hex values to Integers. However, back to the task at hand of converting binary strings, you might have to also include code to check for invalid inputs. A cleverly written algorithm can achieve this in fewer lines of code but all of this can be achieved in practically the minimal lines of code.

Show me the code

There you have it, the easiest way to convert binary to Integer. You can also use the same strtoul function to convert hex values or octal values, rather any from any base you want. The better part is that function is availbe as part of the BSD libraries.

Easy as that, if you have a pure Swift cool technique that helps you achieve the same, please let us know.

Views All Time
Views All Time
Views Today
Views Today
Posted in Basics, Tip, Tutorial and tagged , , , , , , .