Help us understand the problem. What is going on with this article?

Bashでn進数を扱うには

More than 5 years have passed since last update.

Bashの算術式では0755で8進数や0x1aで16進数といった表記をを扱うことが出来ますが、基数#数字… で2~64進数までの全てが使えることは余り知られてないと思うのでメモ。

# 2進数
echo $(( 2#101 ))         # 5
echo $(( 2#101 & 2#100 )) # 4 (=2#100)

# 8進数
echo $(( 0744 ))   # 484
echo $(( 8#744 ))  # 484
echo $(( 8#0744 )) # 484

# 16進数
echo $(( 0xff ))  # 255
echo $(( 16#ff )) # 255

# 62進数 fの続きはzまで行って更にA~Zへと続く!
echo $(( 62#f + 62#z + 62#A + 62#Z )) # 147 (=15+35+36+61)
echo $(( 62#10 )) # 62

# 64進数 Zの続きは @ と _ だ!もはやイミフw
echo $(( 64#Z ))  # 61
echo $(( 64#@ ))  # 62
echo $(( 64#_ ))  # 63
echo $(( 64#10 )) # 64

# 65進数~は扱えないようだ
echo $(( 65#10 )) # invalid arithmetic base (error token is "65#10")
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away