Convert between binary and decimal

How to convert between decimal and binary? I work on the Solaris 10 platform

Decimal value in binary format 4000000002 → 1000000000000000000000000000000010

Binary to decimal - 100000000000000000000000000010 → 4000000002

I used the following command on unix, but it takes a lot of time. I have 20 million entries like this

For a decimal value in binary, set obase to 2:

echo 'obase=2;4000000002' | bc 

For binary to decimal, set ibase to 2:

echo 'ibase=2;100000000000000000000000000010' | bc 
+3
source share
1 answer

If you use bconce for each number, that will be slow. Can you organize the delivery of data to a file and data entry at a time?

, input.txt:

# To binary
$ ( echo 'obase=2;ibase=16;'; cat input.txt ) | bc | paste input.txt - > output.txt

# To hex
$ ( echo 'obase=16;ibase=2;'; cat input.txt ) | bc | paste input.txt - > output.txt

output.txt.

paste ,

07      111
1A      11010
20      100000
2B      101011
35      110101
80      10000000
FF      11111111

. , , :

$ ( echo 'obase=2;ibase=16;'; cat input.txt ) | bc > output.txt

, , , ibase, obase, . gclswceap1d-mc48191-CRENG_DEV [/home/mc48191/scratch]

+1

Source: https://habr.com/ru/post/1764635/


All Articles