Convert binary to decimal in Julia
Asked Answered
N

2

7

I'd like to convert binary to decimal in Julia. It looks like parseint() became deprecated.

Is the below method the best way to do this?

julia> parse(Int,"111",2)
7
Noelyn answered 27/9, 2017 at 15:30 Comment(0)
A
12

Are you starting with a string? Then yes, that's the way. If you're just wanting to write a constant in binary, then it's much easier to just use the 0b111 syntax. By default, it constructs an unsigned integer (which is displayed in hexadecimal), but you can easily convert it to a signed integer with Int(0b111).

julia> 0b110111
0x37

julia> Int(0b110111)
55
Analeptic answered 27/9, 2017 at 18:55 Comment(0)
B
0
julia> convertbinarystring2decimal(s) = parse(Int64,"0b" * s)
convertbinarystring2decimal (generic function with 1 method)

julia> convertbinarystring2decimal("110")
6
Bluebill answered 1/10, 2024 at 4:1 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Synder

© 2022 - 2025 — McMap. All rights reserved.