在 UNIX shell 脚本中将十进制转换为十六进制

发布于 2024-07-09 19:49:30 字数 134 浏览 10 评论 0原文

在 UNIX shell 脚本中,我可以使用什么将十进制数转换为十六进制数? 我以为 od 可以解决这个问题,但它没有意识到我正在向它提供数字的 ASCII 表示形式。

打印? 总的! 目前可以使用,但是还有什么可用的呢?

In a UNIX shell script, what can I use to convert decimal numbers into hexadecimal? I thought od would do the trick, but it's not realizing I'm feeding it ASCII representations of numbers.

printf? Gross! Using it for now, but what else is available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

迷离° 2024-07-16 19:49:31
echo "obase=16; 34" | bc

如果要过滤整个整数文件,每行一个:

( echo "obase=16" ; cat file_of_integers ) | bc
echo "obase=16; 34" | bc

If you want to filter a whole file of integers, one per line:

( echo "obase=16" ; cat file_of_integers ) | bc
独行侠 2024-07-16 19:49:31

十六进制到十进制:

$ echo $((0xfee10000))
4276158464

十进制到十六进制:

$ printf '%x\n' 26
1a

Hexidecimal to decimal:

$ echo $((0xfee10000))
4276158464

Decimal to hexadecimal:

$ printf '%x\n' 26
1a
糖粟与秋泊 2024-07-16 19:49:31
bash-4.2$ printf '%x\n' 4294967295
ffffffff

bash-4.2$ printf -v hex '%x' 4294967295
bash-4.2$ echo $hex
ffffffff
bash-4.2$ printf '%x\n' 4294967295
ffffffff

bash-4.2$ printf -v hex '%x' 4294967295
bash-4.2$ echo $hex
ffffffff
离不开的别离 2024-07-16 19:49:31

抱歉我的错,试试这个...

#!/bin/bash
:

declare -r HEX_DIGITS="0123456789ABCDEF"

dec_value=$1
hex_value=""

until [ $dec_value == 0 ]; do

    rem_value=$((dec_value % 16))
    dec_value=$((dec_value / 16))

    hex_digit=${HEX_DIGITS:$rem_value:1}

    hex_value="${hex_digit}${hex_value}"

done

echo -e "${hex_value}"

示例:

$ ./dtoh 1024
400

Sorry my fault, try this...

#!/bin/bash
:

declare -r HEX_DIGITS="0123456789ABCDEF"

dec_value=$1
hex_value=""

until [ $dec_value == 0 ]; do

    rem_value=$((dec_value % 16))
    dec_value=$((dec_value / 16))

    hex_digit=${HEX_DIGITS:$rem_value:1}

    hex_value="${hex_digit}${hex_value}"

done

echo -e "${hex_value}"

Example:

$ ./dtoh 1024
400
我恋#小黄人 2024-07-16 19:49:31

尝试:

printf "%X\n" ${MY_NUMBER}

Try:

printf "%X\n" ${MY_NUMBER}
守望孤独 2024-07-16 19:49:31

就我而言,我偶然发现了使用 printf 解决方案的一个问题:

$ printf "%x" 008
bash: printf: 008: 无效的八进制数

最简单的方法是使用 bc 的解决方案,在更高的帖子中建议:

$ bc <<<< “奥基=16;008”
8

In my case, I stumbled upon one issue with using printf solution:

$ printf "%x" 008
bash: printf: 008: invalid octal number

The easiest way was to use solution with bc, suggested in post higher:

$ bc <<< "obase=16; 008"
8

香橙ぽ 2024-07-16 19:49:31

zsh 中,您可以执行此类操作:

% typeset -i 16 y
% print $(( [#8] x = 32, y = 32 ))
8#40
% print $x $y
8#40 16#20
% setopt c_bases
% print $y
0x20

示例取自 zsh 有关算术评估的文档页面

我相信 Bash 也有类似的功能。

In zsh you can do this sort of thing:

% typeset -i 16 y
% print $(( [#8] x = 32, y = 32 ))
8#40
% print $x $y
8#40 16#20
% setopt c_bases
% print $y
0x20

Example taken from zsh docs page about Arithmetic Evaluation.

I believe Bash has similar capabilities.

九八野马 2024-07-16 19:49:31
xd() {
    printf "hex> "
    while read i
    do
        printf "dec  $(( 0x${i} ))\n\nhex> "
    done
}
dx() {
    printf "dec> "
    while read i
    do
        printf 'hex  %x\n\ndec> ' $i
    done
}
xd() {
    printf "hex> "
    while read i
    do
        printf "dec  $(( 0x${i} ))\n\nhex> "
    done
}
dx() {
    printf "dec> "
    while read i
    do
        printf 'hex  %x\n\ndec> ' $i
    done
}
染墨丶若流云 2024-07-16 19:49:31
# number conversion.

while `test $ans='y'`
do
    echo "Menu"
    echo "1.Decimal to Hexadecimal"
    echo "2.Decimal to Octal"
    echo "3.Hexadecimal to Binary"
    echo "4.Octal to Binary"
    echo "5.Hexadecimal to  Octal"
    echo "6.Octal to Hexadecimal"
    echo "7.Exit"

    read choice
    case $choice in

        1) echo "Enter the decimal no."
           read n
           hex=`echo "ibase=10;obase=16;$n"|bc`
           echo "The hexadecimal no. is $hex"
           ;;

        2) echo "Enter the decimal no."
           read n
           oct=`echo "ibase=10;obase=8;$n"|bc`
           echo "The octal no. is $oct"
           ;;

        3) echo "Enter the hexadecimal no."
           read n
           binary=`echo "ibase=16;obase=2;$n"|bc`
           echo "The binary no. is $binary"
           ;;

        4) echo "Enter the octal no."
           read n
           binary=`echo "ibase=8;obase=2;$n"|bc`
           echo "The binary no. is $binary"
           ;;

        5) echo "Enter the hexadecimal no."
           read n
           oct=`echo "ibase=16;obase=8;$n"|bc`
           echo "The octal no. is $oct"
           ;;

        6) echo "Enter the octal no."
           read n
           hex=`echo "ibase=8;obase=16;$n"|bc`
           echo "The hexadecimal no. is $hex"
           ;;

        7) exit 
        ;;
        *) echo "invalid no." 
        ;;

    esac
done
# number conversion.

while `test $ans='y'`
do
    echo "Menu"
    echo "1.Decimal to Hexadecimal"
    echo "2.Decimal to Octal"
    echo "3.Hexadecimal to Binary"
    echo "4.Octal to Binary"
    echo "5.Hexadecimal to  Octal"
    echo "6.Octal to Hexadecimal"
    echo "7.Exit"

    read choice
    case $choice in

        1) echo "Enter the decimal no."
           read n
           hex=`echo "ibase=10;obase=16;$n"|bc`
           echo "The hexadecimal no. is $hex"
           ;;

        2) echo "Enter the decimal no."
           read n
           oct=`echo "ibase=10;obase=8;$n"|bc`
           echo "The octal no. is $oct"
           ;;

        3) echo "Enter the hexadecimal no."
           read n
           binary=`echo "ibase=16;obase=2;$n"|bc`
           echo "The binary no. is $binary"
           ;;

        4) echo "Enter the octal no."
           read n
           binary=`echo "ibase=8;obase=2;$n"|bc`
           echo "The binary no. is $binary"
           ;;

        5) echo "Enter the hexadecimal no."
           read n
           oct=`echo "ibase=16;obase=8;$n"|bc`
           echo "The octal no. is $oct"
           ;;

        6) echo "Enter the octal no."
           read n
           hex=`echo "ibase=8;obase=16;$n"|bc`
           echo "The hexadecimal no. is $hex"
           ;;

        7) exit 
        ;;
        *) echo "invalid no." 
        ;;

    esac
done
阿楠 2024-07-16 19:49:31

这不是 shell 脚本,但它是我用来在 bin/oct/dec/hex 之间转换数字的 cli 工具:

    #!/usr/bin/perl

    if (@ARGV < 2) {
      printf("Convert numbers among bin/oct/dec/hex\n");
      printf("\nUsage: base b/o/d/x num num2 ... \n");
      exit;
    }

    for ($i=1; $i<@ARGV; $i++) {
      if ($ARGV[0] eq "b") {
                    $num = oct("0b$ARGV[$i]");
      } elsif ($ARGV[0] eq "o") {
                    $num = oct($ARGV[$i]);
      } elsif ($ARGV[0] eq "d") {
                    $num = $ARGV[$i];
      } elsif ($ARGV[0] eq "h") {
                    $num = hex($ARGV[$i]);
      } else {
                    printf("Usage: base b/o/d/x num num2 ... \n");
                    exit;
      }
      printf("0x%x = 0d%d = 0%o = 0b%b\n", $num, $num, $num, $num);
    }

This is not a shell script, but it is the cli tool I'm using to convert numbers among bin/oct/dec/hex:

    #!/usr/bin/perl

    if (@ARGV < 2) {
      printf("Convert numbers among bin/oct/dec/hex\n");
      printf("\nUsage: base b/o/d/x num num2 ... \n");
      exit;
    }

    for ($i=1; $i<@ARGV; $i++) {
      if ($ARGV[0] eq "b") {
                    $num = oct("0b$ARGV[$i]");
      } elsif ($ARGV[0] eq "o") {
                    $num = oct($ARGV[$i]);
      } elsif ($ARGV[0] eq "d") {
                    $num = $ARGV[$i];
      } elsif ($ARGV[0] eq "h") {
                    $num = hex($ARGV[$i]);
      } else {
                    printf("Usage: base b/o/d/x num num2 ... \n");
                    exit;
      }
      printf("0x%x = 0d%d = 0%o = 0b%b\n", $num, $num, $num, $num);
    }
酒废 2024-07-16 19:49:31

对于那些想要使用变量的人,首先通过运行将其导出:

export NUM=100

然后运行:

printf "%x\n" $NUM

否则,您可以忽略变量的用例并运行直接如下所示:

printf "%x\n" 100

注意:用您选择的变量名称替换 NUM。

导出使其成为环境变量(全局)。

For those who would like to use variables, first export it by running:

export NUM=100

Then run:

printf "%x\n" $NUM

Else, you can you can ignore the use case of the variables and run it directly as shown below:

printf "%x\n" 100

NB:Substitute NUM with the variable name of your choice.

Exporting makes it an environmental variable(global).

荒人说梦 2024-07-16 19:49:31

也许有人可以帮我将其移植到 bash,但目前,我只能让这个函数在 zsh 中工作,

  • 接受以 10 为基数的整数(在 zsh precision range)
  • 按顺序输出每个 base-36 到 base-2 中的值:

zsh --xtrace --verbose --no-rcs  -c  '

baseView()
{
        [[ -n "$#" ]] && {
                ____="${1}"
        } || {
                ____="9012612898217846967"
        }

        for __ in $( jot - 36 2 -1 )
        do
            typeset -i "$__" ___="$____"
            echo " base $__ ="":: $___..."
      
        done | cat - | column -s= -t | 
 
        awk '\'' BEGIN {  _ = substr(__=_=(_=(_=" ")_)_, +_,
                   ___ = FS = RS)
                 } ORS = $+_ ___ substr(_, $+_ = __) '\'' | 

        awk '\'' !/\43/ ? sub(":: ", "&10#")^_ : \
                 /[ \t][2-9][ \t]/ ? gsub(/[0-9][ \t]/, "0&") : _^_ '\'' | 

        rev | column -s

 base 36   :: 36                                            1A62LTNJ8FP...
 base 35   :: 35                                            1OH7IGLN5MJ...
 base 34   :: 34                                            297MFQOC973...
 base 33   :: 33                                            320V61D8HW4...
 base 32   :: 32                                            458GK2KD33L...
 base 31   :: 31                                            5MAJR5NHNQM...
 base 30   :: 30                                            7S6I6ASBLTJ...
 base 29   :: 29                                            B464H7MRHL9...
 base 28   :: 28                                            FN6R3K2LB05...
 base 27   :: 27                                            MKNQAMAP6QP...
 base 26   :: 26                                           175FBKOCBL57...
 base 25   :: 25                                           1O442DN8OJLE...
 base 24   :: 24                                           31ML285199ND...
 base 23   :: 23                                           4L465B0DB5L2...
 base 22   :: 22                                           80BJ7L8CHI9F...
 base 21   :: 21                                           D826EJ0FH5BJ...
 base 20   :: 20                                          12HHJE914JBJ9...
 base 19   :: 19                                          224EDHE448G89...
 base 18   :: 18                                          40H469ACG9FD7...
 base 17   :: 17                                          80DE6274FD2E3...
 base 16   :: 16                                         10A88505468C75...
 base 15   :: 15                                         26213B62543CE4...
 base 14   :: 14                                         5C9C360DD23205...
 base 13   :: 13                                        1263444313436A7...
 base 12   :: 12                                        379A820B95A33B1...
 base 11   :: 11                                       113902A684A6A784...
 base 10   :: 10                                       4688888899996789...
 base 09    :: 9                                      24685871741836887...
 base 08    :: 8                                     205210240521506165...
 base 07    :: 7                                    2610433021013104105...
 base 06    :: 6                                  114100233453531122341...
 base 05    :: 5                                14404040223431344344124...
 base 04    :: 4                            100222020110011101220301311...
 base 03    :: 3                      211202212222101211101221020222221...
 base 02    :: 2  10000101010001000010100000101010001101000110001110101...

baseView()
{
    [[ -n "$#" ]] && {
        ____="${1}" 
    } || {
        ____="9012612898217846967" 
    }
    for __ in $( jot - 36 2 -1 );
        do
        typeset -i "$__" ___="$____" 
        echo " base $__ ="":: $___..."
    done | cat - | column -s= -t | awk 'BEGIN { _ = substr(__=_=(_=(_=" ")_)_,+_,
                ___ = FS = RS) } ORS = $_ ___ substr(_,$_=__)' | awk '         !/\43/  ?  sub(":: ", "&10#")^_    : /[ \t][2-9][ \t]/ ? gsub(/[0-9][ \t]/, "0&") : _^_ ' | rev | column -s
\''#'\'' -t | rev
}
declare -F baseView
baseView 4688888899996789 '




#' -t | rev
}
declare -F baseView;
baseView 4688888899996789 
+zsh:17> declare -F baseView
+zsh:18> baseView 4688888899996789
+baseView:2> [[ -n 1 ]]
+baseView:3> ____=4688888899996789 
+baseView:7> jot - 36 2 -1
+baseView:11> cat -
+baseView:11> column '-s=' -t
+baseView:11> awk 
\''#'\'' -t | rev
}
declare -F baseView
baseView 4688888899996789 '


BEGIN { _ = substr(__=_=(_=(_=" ")_)_,+_,\n ___ = FS = RS) } ORS = $+_ ___ substr(_, $+_=__)' +baseView:12> awk '!/\43/ ? sub(":: ", "&10#")^_ : /[ \t][2-9][ \t]/ ? gsub(/[0-9][ \t]/, "0&") : _^_ ' +baseView:7> __=36 +baseView:9> typeset -i 36 ___=4688888899996789 +baseView:10> echo ' base 36 =:: 36#1A62LTNJ8FP...' +baseView:7> __=35 +baseView:12> rev +baseView:9> typeset -i 35 ___=4688888899996789 +baseView:10> echo ' base 35 =:: 35#1OH7IGLN5MJ...' +baseView:7> __=34 +baseView:9> typeset -i 34 ___=4688888899996789 +baseView:10> echo ' base 34 =:: 34#297MFQOC973...' +baseView:7> __=33 +baseView:9> typeset -i 33 ___=4688888899996789 +baseView:10> echo ' base 33 =:: 33#320V61D8HW4...' +baseView:7> __=32 +baseView:9> typeset -i 32 ___=4688888899996789 +baseView:10> echo ' base 32 =:: 32#458GK2KD33L...' +baseView:7> __=31 +baseView:9> typeset -i 31 ___=4688888899996789 +baseView:10> echo ' base 31 =:: 31#5MAJR5NHNQM...' +baseView:7> __=30 +baseView:9> typeset -i 30 ___=4688888899996789 +baseView:10> echo ' base 30 =:: 30#7S6I6ASBLTJ...' +baseView:7> __=29 +baseView:9> typeset -i 29 ___=4688888899996789 +baseView:10> echo ' base 29 =:: 29#B464H7MRHL9...' +baseView:7> __=28 +baseView:9> typeset -i 28 ___=4688888899996789 +baseView:10> echo ' base 28 =:: 28#FN6R3K2LB05...' +baseView:7> __=27 +baseView:9> typeset -i 27 ___=4688888899996789 +baseView:10> echo ' base 27 =:: 27#MKNQAMAP6QP...' +baseView:7> __=26 +baseView:9> typeset -i 26 ___=4688888899996789 +baseView:10> echo ' base 26 =:: 26#175FBKOCBL57...' +baseView:7> __=25 +baseView:9> typeset -i 25 ___=4688888899996789 +baseView:10> echo ' base 25 =:: 25#1O442DN8OJLE...' +baseView:7> __=24 +baseView:9> typeset -i 24 ___=4688888899996789 +baseView:10> echo ' base 24 =:: 24#31ML285199ND...' +baseView:7> __=23 +baseView:9> typeset -i 23 ___=4688888899996789 +baseView:10> echo ' base 23 =:: 23#4L465B0DB5L2...' +baseView:7> __=22 +baseView:9> typeset -i 22 ___=4688888899996789 +baseView:10> echo ' base 22 =:: 22#80BJ7L8CHI9F...' +baseView:7> __=21 +baseView:9> typeset -i 21 ___=4688888899996789 +baseView:10> echo ' base 21 =:: 21#D826EJ0FH5BJ...' +baseView:7> __=20 +baseView:9> typeset -i 20 ___=4688888899996789 +baseView:10> echo ' base 20 =:: 20#12HHJE914JBJ9...' +baseView:7> __=19 +baseView:9> typeset -i 19 ___=4688888899996789 +baseView:10> echo ' base 19 =:: 19#224EDHE448G89...' +baseView:7> __=18 +baseView:9> typeset -i 18 ___=4688888899996789 +baseView:10> echo ' base 18 =:: 18#40H469ACG9FD7...' +baseView:7> __=17 +baseView:9> typeset -i 17 ___=4688888899996789 +baseView:10> echo ' base 17 =:: 17#80DE6274FD2E3...' +baseView:7> __=16 +baseView:9> typeset -i 16 ___=4688888899996789 +baseView:10> echo ' base 16 =:: 16#10A88505468C75...' +baseView:7> __=15 +baseView:9> typeset -i 15 ___=4688888899996789 +baseView:10> echo ' base 15 =:: 15#26213B62543CE4...' +baseView:7> __=14 +baseView:9> typeset -i 14 ___=4688888899996789 +baseView:10> echo ' base 14 =:: 14#5C9C360DD23205...' +baseView:7> __=13 +baseView:9> typeset -i 13 ___=4688888899996789 +baseView:10> echo ' base 13 =:: 13#1263444313436A7...' +baseView:7> __=12 +baseView:9> typeset -i 12 ___=4688888899996789 +baseView:10> echo ' base 12 =:: 12#379A820B95A33B1...' +baseView:7> __=11 +baseView:9> typeset -i 11 ___=4688888899996789 +baseView:10> echo ' base 11 =:: 11#113902A684A6A784...' +baseView:7> __=10 +baseView:9> typeset -i 10 ___=4688888899996789 +baseView:10> echo ' base 10 =:: 4688888899996789...' +baseView:7> __=9 +baseView:9> typeset -i 9 ___=4688888899996789 +baseView:10> echo ' base 9 =:: 9#24685871741836887...' +baseView:7> __=8 +baseView:9> typeset -i 8 ___=4688888899996789 +baseView:10> echo ' base 8 =:: 8#205210240521506165...' +baseView:7> __=7 +baseView:9> typeset -i 7 ___=4688888899996789 +baseView:10> echo ' base 7 =:: 7#2610433021013104105...' +baseView:7> __=6 +baseView:9> typeset -i 6 ___=4688888899996789 +baseView:10> echo ' base 6 =:: 6#114100233453531122341...' +baseView:7> __=5 +baseView:9> typeset -i 5 ___=4688888899996789 +baseView:12> column '-s#' -t +baseView:10> echo ' base 5 =:: 5#14404040223431344344124...' +baseView:7> __=4 +baseView:9> typeset -i 4 ___=4688888899996789 +baseView:10> echo ' base 4 =:: 4#100222020110011101220301311...' +baseView:7> __=3 +baseView:9> typeset -i 3 ___=4688888899996789 +baseView:10> echo ' base 3 =:: 3#211202212222101211101221020222221...' +baseView:7> __=2 +baseView:9> typeset -i 2 ___=4688888899996789 +baseView:10> echo ' base 2 =:: 2#10000101010001000010100000101010001101000110001110101...' +baseView:12> rev \''#'\'' -t | rev } declare -F baseView baseView 4688888899996789 '

Maybe someone could help me port this to bash, but for now, i could only get this function to work in zsh

  • taking in an integer in base 10 (within zsh precision range)
  • sequentially outputs what that value would be in every base-36 to base-2 :

zsh --xtrace --verbose --no-rcs  -c  '

baseView()
{
        [[ -n "$#" ]] && {
                ____="${1}"
        } || {
                ____="9012612898217846967"
        }

        for __ in $( jot - 36 2 -1 )
        do
            typeset -i "$__" ___="$____"
            echo " base $__ ="":: $___..."
      
        done | cat - | column -s= -t | 
 
        awk '\'' BEGIN {  _ = substr(__=_=(_=(_=" ")_)_, +_,
                   ___ = FS = RS)
                 } ORS = $+_ ___ substr(_, $+_ = __) '\'' | 

        awk '\'' !/\43/ ? sub(":: ", "&10#")^_ : \
                 /[ \t][2-9][ \t]/ ? gsub(/[0-9][ \t]/, "0&") : _^_ '\'' | 

        rev | column -s

 base 36   :: 36                                            1A62LTNJ8FP...
 base 35   :: 35                                            1OH7IGLN5MJ...
 base 34   :: 34                                            297MFQOC973...
 base 33   :: 33                                            320V61D8HW4...
 base 32   :: 32                                            458GK2KD33L...
 base 31   :: 31                                            5MAJR5NHNQM...
 base 30   :: 30                                            7S6I6ASBLTJ...
 base 29   :: 29                                            B464H7MRHL9...
 base 28   :: 28                                            FN6R3K2LB05...
 base 27   :: 27                                            MKNQAMAP6QP...
 base 26   :: 26                                           175FBKOCBL57...
 base 25   :: 25                                           1O442DN8OJLE...
 base 24   :: 24                                           31ML285199ND...
 base 23   :: 23                                           4L465B0DB5L2...
 base 22   :: 22                                           80BJ7L8CHI9F...
 base 21   :: 21                                           D826EJ0FH5BJ...
 base 20   :: 20                                          12HHJE914JBJ9...
 base 19   :: 19                                          224EDHE448G89...
 base 18   :: 18                                          40H469ACG9FD7...
 base 17   :: 17                                          80DE6274FD2E3...
 base 16   :: 16                                         10A88505468C75...
 base 15   :: 15                                         26213B62543CE4...
 base 14   :: 14                                         5C9C360DD23205...
 base 13   :: 13                                        1263444313436A7...
 base 12   :: 12                                        379A820B95A33B1...
 base 11   :: 11                                       113902A684A6A784...
 base 10   :: 10                                       4688888899996789...
 base 09    :: 9                                      24685871741836887...
 base 08    :: 8                                     205210240521506165...
 base 07    :: 7                                    2610433021013104105...
 base 06    :: 6                                  114100233453531122341...
 base 05    :: 5                                14404040223431344344124...
 base 04    :: 4                            100222020110011101220301311...
 base 03    :: 3                      211202212222101211101221020222221...
 base 02    :: 2  10000101010001000010100000101010001101000110001110101...

baseView()
{
    [[ -n "$#" ]] && {
        ____="${1}" 
    } || {
        ____="9012612898217846967" 
    }
    for __ in $( jot - 36 2 -1 );
        do
        typeset -i "$__" ___="$____" 
        echo " base $__ ="":: $___..."
    done | cat - | column -s= -t | awk 'BEGIN { _ = substr(__=_=(_=(_=" ")_)_,+_,
                ___ = FS = RS) } ORS = $_ ___ substr(_,$_=__)' | awk '         !/\43/  ?  sub(":: ", "&10#")^_    : /[ \t][2-9][ \t]/ ? gsub(/[0-9][ \t]/, "0&") : _^_ ' | rev | column -s
\''#'\'' -t | rev
}
declare -F baseView
baseView 4688888899996789 '




#' -t | rev
}
declare -F baseView;
baseView 4688888899996789 
+zsh:17> declare -F baseView
+zsh:18> baseView 4688888899996789
+baseView:2> [[ -n 1 ]]
+baseView:3> ____=4688888899996789 
+baseView:7> jot - 36 2 -1
+baseView:11> cat -
+baseView:11> column '-s=' -t
+baseView:11> awk 
\''#'\'' -t | rev
}
declare -F baseView
baseView 4688888899996789 '


BEGIN { _ = substr(__=_=(_=(_=" ")_)_,+_,\n ___ = FS = RS) } ORS = $+_ ___ substr(_, $+_=__)' +baseView:12> awk '!/\43/ ? sub(":: ", "&10#")^_ : /[ \t][2-9][ \t]/ ? gsub(/[0-9][ \t]/, "0&") : _^_ ' +baseView:7> __=36 +baseView:9> typeset -i 36 ___=4688888899996789 +baseView:10> echo ' base 36 =:: 36#1A62LTNJ8FP...' +baseView:7> __=35 +baseView:12> rev +baseView:9> typeset -i 35 ___=4688888899996789 +baseView:10> echo ' base 35 =:: 35#1OH7IGLN5MJ...' +baseView:7> __=34 +baseView:9> typeset -i 34 ___=4688888899996789 +baseView:10> echo ' base 34 =:: 34#297MFQOC973...' +baseView:7> __=33 +baseView:9> typeset -i 33 ___=4688888899996789 +baseView:10> echo ' base 33 =:: 33#320V61D8HW4...' +baseView:7> __=32 +baseView:9> typeset -i 32 ___=4688888899996789 +baseView:10> echo ' base 32 =:: 32#458GK2KD33L...' +baseView:7> __=31 +baseView:9> typeset -i 31 ___=4688888899996789 +baseView:10> echo ' base 31 =:: 31#5MAJR5NHNQM...' +baseView:7> __=30 +baseView:9> typeset -i 30 ___=4688888899996789 +baseView:10> echo ' base 30 =:: 30#7S6I6ASBLTJ...' +baseView:7> __=29 +baseView:9> typeset -i 29 ___=4688888899996789 +baseView:10> echo ' base 29 =:: 29#B464H7MRHL9...' +baseView:7> __=28 +baseView:9> typeset -i 28 ___=4688888899996789 +baseView:10> echo ' base 28 =:: 28#FN6R3K2LB05...' +baseView:7> __=27 +baseView:9> typeset -i 27 ___=4688888899996789 +baseView:10> echo ' base 27 =:: 27#MKNQAMAP6QP...' +baseView:7> __=26 +baseView:9> typeset -i 26 ___=4688888899996789 +baseView:10> echo ' base 26 =:: 26#175FBKOCBL57...' +baseView:7> __=25 +baseView:9> typeset -i 25 ___=4688888899996789 +baseView:10> echo ' base 25 =:: 25#1O442DN8OJLE...' +baseView:7> __=24 +baseView:9> typeset -i 24 ___=4688888899996789 +baseView:10> echo ' base 24 =:: 24#31ML285199ND...' +baseView:7> __=23 +baseView:9> typeset -i 23 ___=4688888899996789 +baseView:10> echo ' base 23 =:: 23#4L465B0DB5L2...' +baseView:7> __=22 +baseView:9> typeset -i 22 ___=4688888899996789 +baseView:10> echo ' base 22 =:: 22#80BJ7L8CHI9F...' +baseView:7> __=21 +baseView:9> typeset -i 21 ___=4688888899996789 +baseView:10> echo ' base 21 =:: 21#D826EJ0FH5BJ...' +baseView:7> __=20 +baseView:9> typeset -i 20 ___=4688888899996789 +baseView:10> echo ' base 20 =:: 20#12HHJE914JBJ9...' +baseView:7> __=19 +baseView:9> typeset -i 19 ___=4688888899996789 +baseView:10> echo ' base 19 =:: 19#224EDHE448G89...' +baseView:7> __=18 +baseView:9> typeset -i 18 ___=4688888899996789 +baseView:10> echo ' base 18 =:: 18#40H469ACG9FD7...' +baseView:7> __=17 +baseView:9> typeset -i 17 ___=4688888899996789 +baseView:10> echo ' base 17 =:: 17#80DE6274FD2E3...' +baseView:7> __=16 +baseView:9> typeset -i 16 ___=4688888899996789 +baseView:10> echo ' base 16 =:: 16#10A88505468C75...' +baseView:7> __=15 +baseView:9> typeset -i 15 ___=4688888899996789 +baseView:10> echo ' base 15 =:: 15#26213B62543CE4...' +baseView:7> __=14 +baseView:9> typeset -i 14 ___=4688888899996789 +baseView:10> echo ' base 14 =:: 14#5C9C360DD23205...' +baseView:7> __=13 +baseView:9> typeset -i 13 ___=4688888899996789 +baseView:10> echo ' base 13 =:: 13#1263444313436A7...' +baseView:7> __=12 +baseView:9> typeset -i 12 ___=4688888899996789 +baseView:10> echo ' base 12 =:: 12#379A820B95A33B1...' +baseView:7> __=11 +baseView:9> typeset -i 11 ___=4688888899996789 +baseView:10> echo ' base 11 =:: 11#113902A684A6A784...' +baseView:7> __=10 +baseView:9> typeset -i 10 ___=4688888899996789 +baseView:10> echo ' base 10 =:: 4688888899996789...' +baseView:7> __=9 +baseView:9> typeset -i 9 ___=4688888899996789 +baseView:10> echo ' base 9 =:: 9#24685871741836887...' +baseView:7> __=8 +baseView:9> typeset -i 8 ___=4688888899996789 +baseView:10> echo ' base 8 =:: 8#205210240521506165...' +baseView:7> __=7 +baseView:9> typeset -i 7 ___=4688888899996789 +baseView:10> echo ' base 7 =:: 7#2610433021013104105...' +baseView:7> __=6 +baseView:9> typeset -i 6 ___=4688888899996789 +baseView:10> echo ' base 6 =:: 6#114100233453531122341...' +baseView:7> __=5 +baseView:9> typeset -i 5 ___=4688888899996789 +baseView:12> column '-s#' -t +baseView:10> echo ' base 5 =:: 5#14404040223431344344124...' +baseView:7> __=4 +baseView:9> typeset -i 4 ___=4688888899996789 +baseView:10> echo ' base 4 =:: 4#100222020110011101220301311...' +baseView:7> __=3 +baseView:9> typeset -i 3 ___=4688888899996789 +baseView:10> echo ' base 3 =:: 3#211202212222101211101221020222221...' +baseView:7> __=2 +baseView:9> typeset -i 2 ___=4688888899996789 +baseView:10> echo ' base 2 =:: 2#10000101010001000010100000101010001101000110001110101...' +baseView:12> rev \''#'\'' -t | rev } declare -F baseView baseView 4688888899996789 '

假装爱人 2024-07-16 19:49:31

哇,我没有意识到 printf 在 shell 中可用!

话虽如此,我很惊讶没有人评论将 printf 放入 shell 脚本中(如果需要,您可以将其放入您的个人 bin 目录中)。

echo "printf "0x%x\n" $1" > 十六进制
chmod +x hex

现在运行:
./hex 123

它返回:
0x7b

Wow, I didn't realize that printf was available at the shell!

With that said, I'm surprised no-one commented about putting the printf into a shell script (which then you could put in your personal bin directory if you wanted).

echo "printf "0x%x\n" $1" > hex
chmod +x hex

Now just run:
./hex 123

It returns:
0x7b

辞慾 2024-07-16 19:49:30

尝试过 printf(1) 吗?

printf "%x\n" 34
22

可能有一些方法可以使用所有 shell 中的内置函数来做到这一点,但它的可移植性较差。 我还没有检查 POSIX sh 规范来看看它是否具有这样的功能。

Tried printf(1)?

printf "%x\n" 34
22

There are probably ways of doing that with builtin functions in all shells but it would be less portable. I've not checked the POSIX sh specs to see whether it has such capabilities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文