I have a varchar column in Postgres 8.3 that holds values like: ‘0100011101111000’
I need a function that would consider that string to be a number in base 2 and spits out the numeric in base 10. Makes sense?
So, for instance:
‘000001’ -> 1.0
‘000010’ -> 2.0
‘000011’ -> 3.0
Thanks!
Cast to a bit string then to an integer.
An example:
'1110'::bit(4)::integer-> 14Though you had varying length examples, and were after bigint, so instead use
bit(64)and pad the input with zeroes using thelpadfunction.lpad('0100011101111000',64,'0')::bit(64)::bigintHere’s a complete example…
The result of the select is: