I want to print a padded to 32 bits word in binary in Racket. I know about printf
and "~b", but I want it padded to be 32 bits long each time. How do I do this?
Example
(printf "~b" 42)
=> 101010
Want: 00000000000000000000000000101010
Here's a concise way to do it with Racket 5.3.1 and above:
Welcome to Racket v5.3.2.3.
-> (require racket/format)
-> (~r 42 #:base 2 #:min-width 32 #:pad-string "0")
"00000000000000000000000000101010"
See racket/format for more details.
In older Racket versions, you can do this:
Welcome to Racket v5.3.
-> (require srfi/13)
-> (string-pad (number->string 42 2) 32 #\0)
"00000000000000000000000000101010"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With