[英]How can I convert a bitstring to the binary form in Julia
I am using bitstring to perform an xor operation on the ith bit of a string:我正在使用位串对字符串的第 i 个位执行异或运算:
string = bitstring(string ⊻ 1 <<i)
However the result will be a string, so I cannot continue with other i.但是结果将是一个字符串,所以我不能继续使用其他 i.
So I want to know how do I convert a bitstring (of the form “000000000000000000000001001”) to (0b1001)?所以我想知道如何将位串(格式为“000000000000000000000001001”)转换为 (0b1001)?
Thanks谢谢
You can useparse
to create an integer from the string, and then use string
(alt. bitstring
)to go the other way.您可以使用parse
从字符串创建 integer,然后使用string
( bitstring
) 以另一种方式创建 go。 Examples:例子:
julia> str = "000000000000000000000001001";
julia> x = parse(UInt, str; base=2) # parse as UInt from input in base 2
0x0000000000000009
julia> x == 0b1001
true
julia> string(x; base=2) # stringify in base 2
"1001"
julia> bitstring(x) # stringify as bits (64 bits since UInt64 is 64 bits)
"0000000000000000000000000000000000000000000000000000000000001001"
don't use bitstring
.不要使用bitstring
。 You can either do the math with a BitVector
or just a UInt
.您可以使用BitVector
或仅使用UInt
进行数学计算。 No reason to bring a String
into it.没有理由将String
带入其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.