24
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Bashでn進数を扱うには

Last updated at Posted at 2013-03-29

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")
24
26
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?