문제

I would like to get the binary literal corresponding from a given integer, in this way:

4.to_literal => 0b100

Is there such to_literal method?

도움이 되었습니까?

해결책

Use to_s with its optional base parameter to get a string.

4.to_s(2) #=> "100"

You can't get a literal as output.

다른 팁

Use String#% or Kernel#sprintf (%#b as format specifier):

'%#b' % 4
# => "0b100"
sprintf('%#b', 4)
# => "0b100"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top