Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
23
Help us understand the problem. What is going on with this article?
@tachesimazzoca

コマンドライン上で手軽に16進計算

More than 5 years have passed since last update.

10進/16進の変換なら printf コマンドでフォーマット変換する方法があります。

$ alias hex2dec="printf '%d\n'"
$ alias dec2hex="printf '%x\n'"

$ hex2dec 0x64 0xC9 0x14
100
201
20

$ dec2hex 100 201 20
64
c9
14

bc コマンドをつかうと、16進(任意の基数)での計算ができます。

$ echo "obase=10;ibase=16;FF00+FF" | bc
65535
$ echo "obase=16;ibase=16;100*100*100-1" | bc
FFFFFF
$ echo "obase=2;ibase=16;F0-1" | bc
11101111

ibase が入力値の基数で、obase が出力値の基数です。obase の値は直前の ibase で指定した基数で評価されますので、ibase は最後に指定するとよいです。

# 出力値は10進のつもりだが、16進(ibase=16)の 0x10 で評価されている
$ echo "ibase=16;obase=10;FF" | bc
FF
# 16進なら 0x0A で指定
$ echo "ibase=16;obase=0A;FF" | bc
255
# ややこしくなるので ibase を最後に指定するとよい
$ echo "obase=10;ibase=16;FF" | bc
255
23
Help us understand the problem. What is going on with this article?
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

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
23
Help us understand the problem. What is going on with this article?