converting binary string into integer

1,738 views
Skip to first unread message

SYoon

unread,
Oct 29, 2013, 1:30:21 PM10/29/13
to julia...@googlegroups.com
I have a binary number in string

a = "0b1001100"

How can I convert this into integer?
Thanks for any help.

andrew cooke

unread,
Oct 29, 2013, 1:37:19 PM10/29/13
to julia...@googlegroups.com
julia> parseint("0b1001"[3:], 2)
9

Patrick O'Leary

unread,
Oct 29, 2013, 1:37:19 PM10/29/13
to julia...@googlegroups.com

julia> a = "0b1001100"
"0b1001100"
julia> parse(a)
0x4c

SYoon

unread,
Oct 29, 2013, 1:41:44 PM10/29/13
to julia...@googlegroups.com
Great!
Thanks for both of you.

andrew cooke

unread,
Oct 29, 2013, 1:53:41 PM10/29/13
to julia...@googlegroups.com
this confused me for a moment; if it also confuses you it's because  julia parses non-decimal integers as unsigned, and prints unsigned integers as hex.

Stefan Karpinski

unread,
Oct 29, 2013, 2:04:46 PM10/29/13
to julia...@googlegroups.com
I would highly recommend that if you only need to parse integers you use parseint rather than parse. It will be faster and much safer.

Patrick O'Leary

unread,
Oct 29, 2013, 2:30:23 PM10/29/13
to julia...@googlegroups.com
On Tuesday, October 29, 2013 1:04:46 PM UTC-5, Stefan Karpinski wrote:
I would highly recommend that if you only need to parse integers you use parseint rather than parse. It will be faster and much safer.

Yes, that is generally more correct. You would need logic to handle prefixes, though, if they vary, unless that's been rolled into parseint()? I don't have an up-to-date Julia to play with atm (did this in the Forio web repl).

Stefan Karpinski

unread,
Oct 29, 2013, 2:40:13 PM10/29/13
to julia...@googlegroups.com
That's not a bad idea – when the base isn't given, we could infer it from the prefix.

Stefan Karpinski

unread,
Oct 29, 2013, 5:13:54 PM10/29/13
to julia...@googlegroups.com
This patch *kind* of makes this work, but it's not really good enough to commit to base: https://gist.github.com/StefanKarpinski/68a2b5f4e5d51a72c358.

Stefan Karpinski

unread,
Nov 23, 2013, 12:00:48 PM11/23/13
to julia...@googlegroups.com
Btw, my recent enhancements to parseint not only added overflow detection (modulo lack of 128-bit support in LLVM), but also added full support for base autodetection via 0b, 0o and 0x prefixes.

John Myles White

unread,
Nov 23, 2013, 12:32:26 PM11/23/13
to julia...@googlegroups.com
Nice!

— John

Stefan Karpinski

unread,
Nov 23, 2013, 12:33:59 PM11/23/13
to Julia Users
It should be noted that the base autodetection only happens when the base is not explicitly given. If an explicit base is given, the number may only contain digits in that base.

Alberto Barradas

unread,
Nov 6, 2016, 3:18:03 PM11/6/16
to julia-users
Hi guys,
Now that `parseint()` got removed for version 0.5, Is `parse()` the only way to do this?
 How could I parse binary into a BigInt? More specifically, I want to see the integer number of the arecibo message. (73x23 so 1679 binary digits into a big int)

Milan Bouchet-Valat

unread,
Nov 6, 2016, 4:14:03 PM11/6/16
to julia...@googlegroups.com
parse() is meant to be called on Julia expressions.
Use parse(Int, ...) or parse(BigInt, ...) depending on your needs.



Regards

David P. Sanders

unread,
Nov 6, 2016, 5:03:49 PM11/6/16
to julia-users
julia> s = join(rand(0:1, 64))
"1001011000111000000010011110001010010001111110100101001111100111"

julia> parse(Int64, s, 2)
------ OverflowError ------------------- Stacktrace (most recent call last)

julia> parse(BigInt, s, 2)   # the 2 at the end is the base (here, binary)
10824412573101347815 

Alberto Barradas

unread,
Nov 6, 2016, 6:58:05 PM11/6/16
to julia-users

Thank you guys! This looks like a cleaner and more graceful solution!
Reply all
Reply to author
Forward
0 new messages