Преобразование шестнадцатеричного Shift-JIS в символы

1105
Philippe Remy

Я ищу способ преобразования шестнадцатеричного значения Shift-JIS в символ в командной строке Unix / Linux.

Таблицу кодов Shift-JIS можно найти здесь .

За:

82 ae (0x82ae) 

Я бы ожидал:

Я знаю, что это как - то можно с ascii2uniи, nkfно я вроде застрял.

ПРИМЕЧАНИЕ: я мог бы сделать это, но это не ожидаемый результат:

echo "0x82BE" | ascii2uni -a X | nkf -S Result is: 1 token converted 闃セ 

Я нашел эту тему, которая предлагает использовать, iconvно в конечном итоге с тем же результатом, используя этот метод?

Можете ли вы, ребята, помочь?

2
Я нашел http://superuser.com/questions/313032/how-to-convert-a-text-file-from-shift-jis-to-utf-8-and-back-from-the-terminal, но нашел тот же результат с их методом. Philippe Remy 8 лет назад 0
Этот рецепт `iconv` должен был конвертировать Shift-JIS в UTF-8. Это отображалось как 闃 セ? Какую терминальную программу вы используете? Tom Zych 8 лет назад 0
Не обращайте внимания, это не кодировка терминала. Смотрите ответ ниже. Tom Zych 8 лет назад 0

2 ответа на вопрос

2
Tom Zych

Your file doesn't contain the binary you think it does. ascii2uni isn't encoding the way you expect it to; I'm not sure why.

echo "0x82BE" | ascii2uni -a X > test1 od -tx1 test1 0000000 e8 8a be 0a 

(Note that echo -n does the same thing. The newline 0a is coming from ascii2uni, not echo.)

Converting this with iconv:

iconv -f SHIFT-JIS -t UTF-8 test1 > test2 od -tx1 test2 0000000 e9 97 83 ef bd be 0a cat test2 闃セ 

Which is what you got. (Note also that in your ascii2uni example, you had 82BE, which is だ in Shift-JIS, instead of your original 0x82ae, which is ぐ. I'll stick with 82BE.)

The problem is that the binary wasn't right to begin with. Do it this way:

echo -en '\x82\xbe' > test3 od -tx1 test3 0000000 82 be iconv -f SHIFT-JIS -t UTF-8 test3 > test4 od -tx1 test4 0000000 e3 81 a0 cat test4 だ 
Спасибо за объяснение. Именно то, что мне было нужно. Очень признателен! Philippe Remy 8 лет назад 0
1
JakeGould

Based on this answer on the Unix and Linux Stack Exchange site, recode works cleanly for me on Ubuntu 12.04.5 (LTS):

echo -n 0x82ae | recode SHIFT-JIS/x4..UTF-8 

Of course this converts the output of the the hexadecimal code from Shift-JIS to UTF-8, but heck… UTF-8 is what all the kids are using nowadays. But you can just lop off the ..UTF-8 stuff like this and the output should be pure Shift-JIS:

echo -n 0x82ae | recode SHIFT-JIS/x4 

To confirm it converted to UTF-8 correctly you can pipe it to xxd like this:

echo -n 0x82ae | recode SHIFT-JIS/x4..UTF-8 | xxd -p -u 

And it checks out as being E38190 which matches the exact same character in UTF-8 as shown here. A full table of Shift-JIS to UTF-8 conversion mappings can be found here.

Or you can just run xxd from the command line like this to get the exact hexadecimal code for any character—or series of characters—you wish:

echo -n "ぐ" | xxd -p -u 
`Iconv` также должен был быть преобразован в UFT-8. Я подозреваю, что его терминал использует другую кодировку. Tom Zych 8 лет назад 1
Нет, я был неправ. Записываю ответ сейчас. Tom Zych 8 лет назад 1
Джейк, спасибо за четкий ответ. Это работает сейчас! Philippe Remy 8 лет назад 1
@TomZych Нет проблем. Проблемы с кодировкой символов могут быть немного волшебными, когда вы справляетесь с этим. Магия, головные боли и удача. JakeGould 8 лет назад 0

Похожие вопросы