\u30a2' , but are there alternatives to thi...">

Printing Unicode Hex Codes Using Shell Utilities such as Hexdump or Xxd

Bash 4.2 and zsh support echo $'\u30a2' , but are there alternatives to this using common shell utilities?

 $ ruby -e 'puts ARGV[0].chars.map{|c|c.ord.to_s(16)}.join(" ")' aรคใ‚ข๐€ 61 e4 30a2 1d400 

This does not work with U + 10000 or on characters:

 $ printf %s aรคใ‚ข๐€ | iconv -f $(locale charmap) -t UTF-16BE | xxd -p 006100e430a2d835dc00 
+4
source share
2 answers

I found this function at https://raw.github.com/lhunath/scripts/master/bash/bashlib/bashlib :

 hex() { printf '%x' "'$1" } 

Other examples:

 $ printf %x\\n \'ใ‚ข7fc2 $ LC_CTYPE=C printf %x\\n \'ใ‚ขe3 $ printf %s $' \n\n\\'|while IFS= read -r -d '' -n1 c;do printf %x\\n "'$c";done 20 20 a a 5c $ printf %s aรคใ‚ข๐€|while IFS= read -r -d '' -n1 c;do printf '%s %x\n' "$c" "'$c";done a 61 รค e4ใ‚ข 30a2 ๐€ 1d400 

This worked with the built-in printf in bash 4.2 and zsh 4.3.11, but not with the built-in printf in bash 3.2 or with OS X /usr/bin/printf .

-n1 reads one character at a time, and -d '' changes the delimiter from \n to \0 , so read also includes strings (but not NUL characters). The only read option given by POSIX is -r .

+3
source

A simple Python 2.7 script can do the job (name it a.py say). [the script below assumes your default UTF8 shell encoding - you may need to change it if necessary]:

 import sys for i in unicode(sys.argv[1], 'utf-8'): print i.encode("utf_16_be").encode("hex") 

Testing:

 $ python a.py aรคใ‚ข๐€ 0061 00e4 30a2 d835dc00 
+3
source

Source: https://habr.com/ru/post/1469233/


All Articles