Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In the ASCII table the 'J' character exists which has code points in different numeral systems:

Oct   Dec   Hex   Char
112   74    4A    J

It's possible to print this char by an octal code point by printing printf '\112' or echo $'\112'. How do I print the same character by decimal and hexadecimal code point presentations?

share|improve this question
up vote 6 down vote accepted

Hex:

printf '\x4a'

Dec:

printf "\\$(printf %o 74)"

Alternative for hex :-)

xxd -r <<<'0 4a'
share|improve this answer

Decimal:

chr() {
    local c
    for c
    do
        printf "\\$((c/64*100+c%64/8*10+c%8))"
    done
}

chr 74

Hex:

chr $((16#4a))

The function can do sequences:

$ chr 74 75 76; echo
JKL
$
share|improve this answer

With zsh:

$ printf '\x4a\n' # Hex
J
$ printf "\\$(([##8]74))\n" # Dec
J

To get a character (in the current charset) from the Unicode code point:

$ printf '\U1F42E\n' # Hex
🐮
$ printf "\\U$(([##16]128046))\n" # Dec
🐮
share|improve this answer
    
answer also please how to print this face by 'f0 9f 90 ae' hex code – viavad yesterday
1  
2  
You are a brilliant and valued member of this community, but what the hell? He clearly asked for a Bash solution, both in the title and tags. I am flagging this – Steven Penny yesterday
1  
@StevenPenny The question has shell tag too, so it is perfectly OK IMO to offer solution in another shell. Also refrain from using any curse language, i am flagging your comment as offensive. – heemayl yesterday

In general, the shell could understand hex, oct and decimal numbers in variables, provided they have been defined as integers:

$ declare -i var1 var2 var3 var4 var5 var6 var7
$ var1=0112
$ var2=74
$ var3=0x4a
$ var4=8#112
$ var5=10#74
$ var6=16#4a
$ var7=18#h8
echo "$var1 $var2 $var3 $var4 $var5 $var6 $var7"
74 74 74 74 74 74 314

So, you just need one way to print the character that belongs to a variable value.
But here are two possible ways:

$ declare -i var
$ var=0x65; printf '%b\n' "\\$(printf '0%o' "$var")"
e

$ var=0x65; printf '%b\n' "\U$(printf '%08x' "$var")"
e

The two printf are needed, one to tranform the value into an hexadecimal string and the second to actually print the character.

The second one will print any UNICODE point (if your console is correctly set).
For example:

$ var=0x2603; printf '%b\n' "\U$(printf '%06x' "$var")"
☃

An snow man.

The character that has an utf-8 representation as f0 9f 90 ae is 0x1F42E. Search for cow face site:fileformat.info to get it:

$ var=0x1F42F; printf '%b\n' "\U$(printf '%06x' "$var")"
🐮

Note: There is a problem with the UNICODE way in that for bash 4.3 and before, the characters between UNICODE points 128 and 255 (in decimal) are incorrectly printed.


References

Fourth paragraph inside PARAMETERS in man bash:

If the variable has its integer attribute set, then value is evaluated as an arithmetic expression even if the $((...)) expansion is not used (see Arithmetic Expansion below).

Inside "ARITHMETIC EVALUATION" in man bash:

Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal. Otherwise, numbers take the form [base#]n, where the optional base is a decimal number between 2 and 64 representing the arithmetic base, and n is a number in that base. If base# is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, @, and _, in that order. If base is less than or equal to 36, lowercase and uppercase letters may be used interchangeably to represent numbers between 10 and 35.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.