在 Julia 中将二进制转换为十进制 [英] Convert binary to decimal in Julia

查看:13
本文介绍了在 Julia 中将二进制转换为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Julia 中将二进制转换为十进制.看起来 parseint() 已被弃用.

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

推荐答案

你是从字符串开始的吗?那么是的,就是这样.如果您只是想用二进制编写一个常量,那么使用 0b111 语法会容易得多.默认情况下,它构造一个无符号整数(以十六进制显示),但您可以使用 Int(0b111) 轻松将其转换为有符号整数.

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

这篇关于在 Julia 中将二进制转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆