0
echo hello | iconv -f ascii -t utf-16le | od -x

produces what seems to me like a big-endian result

0068    0065    006c    006c    006f    000a

whereas the same line without the 'le' produces, on a utf16le system (Osx)

echo hello | iconv -f ascii -t utf-16 | od -x

fffe 6800 6500 6c00 6c00 6f00 0a00

Does od -x change the endianness?

1 Answers1

3

od seems not changing anything, but iconv does. According to your output, this system maps utf-16 to utf-16be. Compare with output from my Ubuntu on x86-64:

$ echo hello | iconv -f ascii -t utf-16le | hd
00000000  68 00 65 00 6c 00 6c 00  6f 00 0a 00              |h.e.l.l.o...|
0000000c
$ echo hello | iconv -f ascii -t utf-16le | od -x
0000000 0068 0065 006c 006c 006f 000a
0000014
$ echo hello | iconv -f ascii -t utf-16be | hd
00000000  00 68 00 65 00 6c 00 6c  00 6f 00 0a              |.h.e.l.l.o..|
0000000c
$ echo hello | iconv -f ascii -t utf-16be | od -x
0000000 6800 6500 6c00 6c00 6f00 0a00
0000014

One could call od -t x1 instead of hd with similar output:

$ echo hello | iconv -f ascii -t utf-16be | od -t x1
0000000 00 68 00 65 00 6c 00 6c 00 6f 00 0a
0000014

it's like hd but without ASCII column.

But my Ubuntu maps utf-16 to utf-16le with BOM:

$ echo hello | iconv -f ascii -t utf-16 | od -x
0000000 feff 0068 0065 006c 006c 006f 000a
0000016
$ echo hello | iconv -f ascii -t utf-16 | hd
00000000  ff fe 68 00 65 00 6c 00  6c 00 6f 00 0a 00        |..h.e.l.l.o...|
0000000e

that is interesting why your OS X system is configured opposite way. I see no real reason for this; maybe it is some weird legacy or compile configuration error. Does it have documentation with mentioning of this moment?

Netch
  • 2,529
  • 2
    If you specify utf-16, you are saying "I don't care about the endianness, I just want UTF-16 with a BOM." The macOS version uses GNU libiconv which always outputs big-endian data no matter what. Your Ubuntu version is almost certainly the one provided by glibc instead, which uses native endianness. Not an error, just two different implementations making different choices in a situation where that is permitted – Fox Jul 22 '20 at 01:01
  • Thank you. echo hello | iconv -f ascii -t utf-16 | od -x gives little-endian (on OS X Catalina) fffe 6800 6500 6c00 6c00 6f00 0a00. The '-x' and '-t x2' give little-endian and -t x1 gives big endian, because it splits words. – Dmitry Starostin Jul 22 '20 at 18:24