## How to print integer literals in binary or hex in haskell?

putstrln

How to print integer literals in binary or hex in haskell?

```printBinary 5 => "0101"

printHex 5 => "05"
```

Which libraries/functions allow this?

I came across the Numeric module and its showIntAtBase function but have been unable to use it correctly.

```> :t showIntAtBase

showIntAtBase :: (Integral a) => a -> (Int -> Char) -> a -> String -> String
```

The Numeric module includes several functions for showing an Integral type at various bases, including `showIntAtBase`. Here are some examples of use:

```import Numeric (showHex, showIntAtBase)
import Data.Char (intToDigit)

putStrLn \$ showHex 12 "" -- prints "c"
putStrLn \$ showIntAtBase 2 intToDigit 12 "" -- prints "1100"
```

How to print integer literals in binary or hex in haskell?, How to print integer literals in binary or hex in haskell? printBinary 5 => "0101" printHex 5 => "05". Which libraries/functions allow this? I came across the  You can convert integer to binary with something like the following: decToBin x = reverse \$ decToBin' x where decToBin' 0 = [] decToBin' y = let (a,b) = quotRem y 2 in [b] ++ decToBin' a. usage in GHCi: Prelude> decToBin 10 [1,0,1,0] You could define your own recursive functions like:

You may also use printf of the printf package to format your output with c style format descriptors:

```import Text.Printf

main = do

let i = 65535 :: Int

putStrLn \$ printf "The value of %d in hex is: 0x%08x" i i
putStrLn \$ printf "The html color code would be: #%06X" i
putStrLn \$ printf "The value of %d in binary is: %b" i i
```

Output:

The value of 65535 in hex is: 0x0000ffff The html color code would be: #00FFFF The value of 65535 in binary is: 1111111111111111

Data.GHex, Portability, non-portable (GHC Extensions) This module defines operations for an interactive hex-caluclator using GHCi. in hexadecimal format by default; Suppress type annotation of numeric literals by Generate bits and bytes with position It is useful for reading from various command output, such as od, hexdump  PostScript. Integer literals in PostScript can be either standard decimal literals or in the form base # number. base can be any decimal integer between 2 and 36, number can then use digits from 0 to base − 1. Digits above 9 are replaced by A through Z and case does not matter. 123 % 123. 8#1777 % 1023.

If you import the `Numeric` and `Data.Char` modules, you can do this:

```showIntAtBase 2 intToDigit 10 "" => "1010"
showIntAtBase 16 intToDigit 1023 "" => "3ff"
```

This will work for any bases up to 16, since this is all that `intToDigit` works for. The reason for the extra empty string argument in the examples above is that `showIntAtBase` returns a function of type `ShowS`, which will concatenate the display representation onto an existing string.

Literals/Integer, Binary literals are of type BITS, and need to be converted to INT using the operator ABS. main:( print((" INT:", dec, hex, oct, bin, new line)); 3) Hexadecimal integer literal (base 16, the first digit is the most significant, the letters 'a' through 'f' represent values (decimal) 10 through 15) 4) Binary integer literal (base 2, the first digit is the most significant)

You can convert integer to binary with something like the following:

```decToBin x = reverse \$ decToBin' x
where
decToBin' 0 = []
decToBin' y = let (a,b) = quotRem y 2 in [b] ++ decToBin' a
```

usage in GHCi:

```Prelude> decToBin 10
[1,0,1,0]
```

Haskell: Library function to convert hexadecimal to binary, Not sure if you could use this: How to print integer literals in binary or hex in haskell? Haskell: recursively convert hex string to integer? I don't think that there is a  As of VB.NET 15 there is now support for binary literals: Dim mask As Integer = &B00101010. You can also include underscores as digit separators to make the number more readable without changing the value: Dim mask As Integer = &B0010_1010. share.

Hex can be written with `0x` and binary with `0b` prefix e.g.:

```> 0xff
255
>:set -XBinaryLiterals
> 0b11
3
```

Note that binary requires the `BinaryLiterals` extension.

Haskell: Could not find module `Data.List.Split' - haskell - php, Not sure if you could use this: How to print integer literals in binary or hex in haskell? Haskell: recursively convert hex string to integer? I don't think that there is a  is that the print function that's used for this purpose always guarantees a format that's safe to use again in Haskell code, hence it puts strings in quotes and escapes all tricky characters including newlines. If you simply want to dump a string to the terminal as-is, use putStrLn instead of print.

Haskell showhex, recall the following type definitions from the Prelude: Use Haskell to display a ByteString as hex. How to print integer literals in binary or hex in haskell? With the default declaration, integer literals are inferred as Hex types. ghci> default (Hex) ghci> 255 0x0000_0000_0000_00ff Produced by hackage and Cabal 2.4.2.99.

Integer literal, In computer science, an integer literal is a kind of literal for an integer whose value is directly For example, in C++ 0x10ULL indicates the value 16 (because hexadecimal) as binary numbers (base-2) in four digit groups (one nibble, representing one of 16 "Glasgow Haskell Compiler User's Guide: 11.3.7. Print/export. Int, which fixed-width machine-specific integers with a minimum guaranteed range of −2 29 to 2 29 − 1. In practice, its range can be much larger: on the x86-64 version of Glasgow Haskell Compiler, it can store any signed 64-bit integer.

Some answers of the homework in the slides · GitHub, http://stackoverflow.com/questions/1959715/how-to-print-integer-literals-in-binary​-or-hex-in-haskell. binExp :: Int -> String. binExp x = showIntAtBase 2 intToDigit  Read a hexadecimal integer, consisting of an optional leading "0x" followed by at least one hexadecimal digit. Input is consumed until a non-hex-digit or end of string is reached.

• For anyone lazy like me who didn't scroll down, the printf example is much more concise and flexible, and can do other useful stuff like e.g. give a constant length string and all other printf features. Instead of above, just: `printf "%032b" 5`
• @mozboz, `printf` in Haskell is more like a magic trick than a function to be used in serious code. The format string is parsed at runtime (which may produce runtime errors) and the whole mechanism is a bit slow.
• ` printf "The value of %d in hex is: 0x%08x" i i ` is ok because printf can be both `IO ()` and `String`
• Shouldn't 0 give back 0? `decToBin' 0 = [0]` maybe?
• I think you can use `("0123456789abcdef" !!)` instead of `(\n -> "0123456789abcdef" !! n)`.
• All the other answers ignore `Data.Word`. And lets get real here - once you do "bit hammering", e.g. using `Data.Bits`, you are more likely to want to see hex or binary numbers AND you are more likely not working with `Int` but much rather with `Word8, Word16, ..`. +1
• although it is a curious choice he made regarding using Word8 for the base instead of for the number... Could be improved... Even better if it worked for all `Bits a`.