How to write hexadecimal strings?


#1

I want to get \x60\x7f as output. If I do "\\x60\\x7f" as input, I get that exactly as output (with 2 backslashes). Wrapped in a multiline string I get new lines I don’t want. What am I missing here?

This is related to Alacritty terminal key mappings so I do need the hexadecimal string here (see https://github.com/alacritty/alacritty/wiki/Keyboard-mappings)


#2

"\\x60\\x7f" should be correct, if I’m understanding what you’re wanting. The printed output in a terminal or whatever will still have the escape sequences in it, but if you actually output it as text it is the correct \x60\x7f. If you’re discussing outputting to JSON, then there is still going to be the double slash in the JSON output, because JSON forbids invalid escape sequences like "\x".

> dhall text
"\\x60\\x7f"

\x60\x7f


> dhall-to-json
"\\x60\\x7f"

"\\x60\\x7f"

#3

The answer that @SiriusStarr gave is correct. I just want to further clarify how to use this in your Dhall binding.

Assuming that you are using Dhall to template an Alacritty key mapping, then your code would look like this:

-- ./example.dhall

let chars = "\\x5C"

in  ''
      - { key: Caret, mods: Alt, chars: "${chars}" }
    ''

… which produces the correct result when the template is rendered using dhall text:

$ dhall text --file ./example.dhall 
  - { key: Caret, mods: Alt, chars: "\x5C" }

#4

My initial example was a bit haphazard, sorry. I typed it rather in a hurry before going to bed.

The problem with your example @Gabriel439 is that I want to render it to a .yaml file with dhall-to-yaml.

let text = "\\x60"

in { chars = "${text}"}

gives me

 $ dhall-to-yaml --file escape.dhall
chars: "\\x60"

If I omit the double backslash (in effect no longer escaping it), compilation fails. My intended outcome though would be something like the following:

let text = "????x60"

in { chars = "${text}"}

gives me

 $ dhall-to-yaml --file escape.dhall
chars: "\x60"

And I don’t know what to fill in for the question marks.


#5

While the \x41 notation is not valid for JSON, it seems correct for YAML, e.g. ns-esc-8-bit from https://yaml.org/spec/1.2/spec.html#escaping/in%20double-quoted%20scalars/

According to https://docs.dhall-lang.org/tutorials/Language-Tour.html#text , another solution would be to use ns-esc-16-bit unicode escape:

{ chars = "\u0060", non-print = "\u0001" }

which results in

$ dhall-to-yaml --file escape.dhall
chars: "`"
non-print: "\u0001"

#6

You can also use braced escape sequence to more closely resemble the original Hex escape:

{ chars = "\u{60}", non-print = "\u{01}" }

#7

Thanks for all the suggestions. Using the unicode escape sequences does indeed work. It’s a bit strange since it’s mentioned repeatedly that you need to use hexadecimal escapes in various alacritty documents and blog posts.

With that being said, it appears to simply not be possible in Dhall to solve my initial problem. The solutions here are a solution to the general question of how to generate alacritty chars entries which, at least for my setup, achieve the same thing as the hex sequences I had before. But the combination of dhall-to-yaml and outputting a .yaml file containing hex escapes seems to be incompatible.