How to convert an IBAN to an Integer?

451 Views Asked by At

Note, this is not about using libraries or any specific programming language. I just want to understand the following concept, from Wikipedia, Validating the IBAN:

Example (fictitious United Kingdom bank, sort code 12-34-56, account number 98765432):

  • IBAN:

       GB82 WEST 1234 5698 7654 32  
    
  • Rearrange:

       W E S T12345698765432 G B82  
    
  • Convert to integer:

      3214282912345698765432161182  
    
  • Compute remainder:

      3214282912345698765432161182  mod 97 = 1
    

The bold step is causing me headache. I'm assuming the IBAN is alphanumeric, i.e., Base-36 encoded (10 numbers, 26 letters).

Now, when I convert the base-36 to base-10 (this is how I understand this step), my result is way off. Here, for example, in Ruby:

 $ irb
irb(main):001:0> "WEST12345698765432GB82".to_i(36)
=> 15597194993925618867946544653683410

Now, please, someone enlighten me, what is really meant by convert to integer?

1

There are 1 best solutions below

0
On

Oh! The formatting on Wikipedia gave me a hint: It's not a single, whole, big number but each character has to be converted on its own:

"WEST12345698765432GB82".split("").each do |c| 
  print c.to_i(36)
end

Result:

3214282912345698765432161182
=> ["W", "E", "S", "T", "1", "2", "3", "4", "5", "6", "9", "8", "7", "6", "5", "4", "3", "2", "G", "B", "8", "2"]

Note, the W is the 32th integer in Base-36:

"W".to_i(36)
=> 32

And so on.