| 1 | open Core |
| 2 | include Bin_prot.Std |
| 3 | |
| 4 | let hex_char n = |
| 5 | if n < 10 |
| 6 | then Char.of_int_exn (n + Char.to_int '0') |
| 7 | else Char.of_int_exn (n - 10 + Char.to_int 'a') |
| 8 | ;; |
| 9 | |
| 10 | let to_hex buf max_len = |
| 11 | let len = String.length buf in |
| 12 | assert (len <= max_len); |
| 13 | let str = Bytes.make ((max_len * 3) - 1) ' ' in |
| 14 | for column = 0 to max_len - 1 do |
| 15 | let ofs = max_len - 1 - column in |
| 16 | if ofs >= len |
| 17 | then ( |
| 18 | Bytes.set str ((column * 3) + 0) '.'; |
| 19 | Bytes.set str ((column * 3) + 1) '.') |
| 20 | else ( |
| 21 | let byte = Char.to_int buf.[ofs] in |
| 22 | Bytes.set str ((column * 3) + 0) (hex_char (byte lsr 4)); |
| 23 | Bytes.set str ((column * 3) + 1) (hex_char (byte land 0xf))) |
| 24 | done; |
| 25 | Bytes.unsafe_to_string ~no_mutation_while_string_reachable:str |
| 26 | ;; |