2

The following output is as I expected:

125> [97, 98, 99].
"abc"

126> [97, 98, 0]. 
[97,98,0]

But the output using ~s is not what I expected:

127> io:format("~s~n", [[97, 98, 0]]).
ab^@
ok

How do I interpret that output?

7stud
  • 46,922
  • 14
  • 101
  • 127

1 Answers1

5

The ~s control sequence expects to get a string, a binary or an atom, and prints it "with string syntax". As Erlang strings are just lists of integers, it tries to print [97, 98, 0] in this example as a string as well. The shell on the other hand tries to guess whether this list of integers is meant to be a string.

^@ represents the NUL character. You might be familiar with the caret notation, where ^A represents byte 1, since A is the 1st letter in the alphabet - or in other words, it represents the byte whose value is 64 less than the ASCII value of the character, since A is 65 in ASCII. Extrapolate it to the 0 byte, and you'll find @, which is 64 in ASCII.

Community
  • 1
  • 1
legoscia
  • 39,593
  • 22
  • 116
  • 167