4

I expected printf %s '\<octal_character_value>' to do the trick, but it doesn't:

printf %s '\101'

Outputs:

\101
kos
  • 41,268

3 Answers3

4

Presumably you want %b. From help printf:

In addition to the standard format specifications described in printf(1),
printf interprets:

  %b    expand backslash escape sequences in the corresponding argument

And:

$ printf "%b\n" '\101'
A

I don't know if it works for Unicode characters in general.

muru
  • 207,228
1

You can use Awk:

$ awk 'BEGIN {print "\107"}'
G

Or the awk Velour library:

$ velour -n 'print n_chr(+n_baseconv(107, 8, 10))'
G
Zombo
  • 1
0

It works with unicode as long as you type out the byte.

$ gprintf '\353\200\200' | odview

0000000 8421611
"" ** **
353 200 200
? 80 80
235 128 128
eb 80 80
8421611

0000003

The %b operator isn't necessary (as long as your octals aren't \0NNN form, at least in the macOS 11.5 bash3 built-in, the macOS BSD-UNIX printf, and also gnu-printf 8.32.

This character is U+B000 (n=45,056). The decomposed one is more verbose but works the same

gprintf '\341\204\201\341\205\260\341\206\267' | odview

0000000 3783361761 2262937733 183
ᄁ ** ** ᅰ ** ** ᆷ ** **
341 204 201 341 205 260 341 206 267
? 84 81 ? 85 ? ? 86 ?
225 132 129 225 133 176 225 134 183
e1 84 81 e1 85 b0 e1 86 b7
-511605535 -2032029563 183

Greenonline
  • 2,182