Convert 16 binary to decimal (turn) with Linux/unix command

Source: Internet
Author: User
Tags binary to decimal decimal to binary ibase

That day to write a script, you need to turn the hexadecimal number into a decimal, found do not know how to do, search, the original is very simple, than with C language or something easier, on some ready-made orders to solve.

Two simple methods are listed first:

1) Echo can achieve it by itself, quite simply. However, you do not know what to write if you want to convert from decimal to hexadecimal.

]$ echo $ ((0XAC))
172

2) printf can also oh, hexadecimal and decimal conversions are fine.

]$ printf%d 0xac
172
]$ printf%x 172
Ac

Then, a little bit more trouble is the BC command. Although the online introduction of a lot of people, but in the end BC of those two parameters is how to use, or do not quite understand, feel oneself is also from the results to push the reason, do not really understand the parameters of this command so the meaning of design. In any case, let's write an example here and study it in the future.

3) BC role: 16 binary and decimal reciprocal, between decimal and binary can also be converted.

Convert 16 binary to decimal
]$ Echo ' ibase=16;obase=a; AC ' |BC
172
]$ Echo ' ibase=16;obase=1010; AC ' |BC (Linux)
0172
> Echo ' ibase=16;obase=1010; AC ' |BC (Unix)
Output base is too large
172

Note here: This error will be reported when executing in Unix, but there will still be results.
]$ Echo ' ibase=16; AC ' | Bc
172

Convert decimal to hexadecimal
]$ echo ' ibase=10;obase=16;172 ' |BC
AC
]$ echo ' obase=16;172 ' |BC
AC

converting decimal to Binary
]$ echo ' obase=2;172 ' |BC
10101100
Convert binary into decimal
]$ echo ' ibase=2;10101100 ' |BC
172

Looks like IBase and obase the default value of the two parameters is 10 (decimal), so it seems that if you do not write, IBase represents the input parameter is decimal, Obase represents the value of the output is decimal. In other words, it is not a decimal time to indicate, it seems that this will not be wrong.

Well, study again.

Http://blog.sina.com.cn/s/blog_a3052b4a0100z4nk.html

Convert 16 binary to decimal (turn) with Linux/unix command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.