쉘 스크립트에서 16 진수를 16 진수로
쉘 스크립트에서 16 진수를 10 진수로 변환하는 것을 도와 줄 수 있습니까?
예를 들어, bfca3000
쉘 스크립트를 사용하여 16 진수 를 10 진수로 변환하고 싶습니다 . 나는 기본적으로 두 개의 16 진수의 차이를 원합니다.
내 코드는 다음과 같습니다
var3=`echo "ibase=16; $var1" | bc`
var4=`echo "ibase=16; $var2" | bc`
var5=$(($var4-$var3)) # [Line 48]
실행할 때이 오류가 발생합니다.
Line 48: -: syntax error: operand expected (error token is "-")
16 진수에서 10 진수로 변환하려면 쉘이나 외부 프로그램을 사용하여 여러 가지 방법으로 수행 할 수 있습니다.
와 bash는 :
$ echo $((16#FF))
255
와 BC :
$ echo "ibase=16; FF" | bc
255
와 펄 :
$ perl -le 'print hex("FF");'
255
과 의 printf :
$ printf "%d\n" 0xFF
255
와 파이썬 :
$ python -c 'print(int("FF", 16))'
255
루비 와 함께 :
$ ruby<<EOF
p "FF".to_i(16).to_s(10)
EOF
"255"
와 Node.js를 :
$ nodejs <<< "console.log(parseInt('FF', 16))"
255
와 코뿔소 :
$ rhino<<EOF
print(parseInt('FF', 16))
EOF
...
255
와 그루비 :
$ groovy -e 'println Integer.parseInt("FF",16)'
255
Linux에서 매우 가벼운 임베디드 버전의 busybox를 처리한다는 것은 기존의 많은 명령을 사용할 수 없음을 의미합니다 (bc, printf, dc, perl, python)
echo $((0x2f))
47
hexNum=2f
echo $((0x${hexNum}))
47
신용 피터 렁 이 솔루션.
쉘을 사용하여 한 가지 더 방법 (bash 또는 ksh는 대시와 작동하지 않음) :
echo $((16#FF))
255
쉘 내에서 다양한 도구를 사용할 수 있습니다. Sputnick은 귀하의 초기 질문에 기초하여 귀하의 옵션에 대한 훌륭한 개요를 제공했습니다. 그는 당신에게 여러 가지 정답을 제공하는 시간 동안 투표 할 가치가 있습니다.
그의 목록에없는 것 하나 더 :
[ghoti@pc ~]$ dc -e '16i BFCA3000 p'
3217698816
그러나 당신이하고 싶은 것이 빼기라면, 왜 입력을 밑 10으로 바꾸는 것이 귀찮습니까?
[ghoti@pc ~]$ dc -e '16i BFCA3000 17FF - p 10o p'
3217692673
BFCA1801
[ghoti@pc ~]$
The dc
command is "desk calc". It will also take input from stdin, like bc
, but instead of using "order of operations", it uses stacking ("reverse Polish") notation. You give it inputs which it adds to a stack, then give it operators that pop items off the stack, and push back on the results.
In the commands above we've got the following:
16i
-- tells dc to accept input in base 16 (hexadecimal). Doesn't change output base.BFCA3000
-- your initial number17FF
-- a random hex number I picked to subtract from your initial number-
-- take the two numbers we've pushed, and subtract the later one from the earlier one, then push the result back onto the stackp
-- print the last item on the stack. This doesn't change the stack, so...10o
-- tells dc to print its output in base "10", but remember that our input numbering scheme is currently hexadecimal, so "10" means "16".p
-- print the last item on the stack again ... this time in hex.
You can construct fabulously complex math solutions with dc. It's a good thing to have in your toolbox for shell scripts.
The error as reported appears when the variables are null (or empty):
$ unset var3 var4; var5=$(($var4-$var3))
bash: -: syntax error: operand expected (error token is "-")
That could happen because the value given to bc was incorrect. That might well be that bc needs UPPERcase values. It needs BFCA3000
, not bfca3000
. That is easily fixed in bash, just use the ^^
expansion:
var3=bfca3000; var3=`echo "ibase=16; ${var1^^}" | bc`
That will change the script to this:
#!/bin/bash
var1="bfca3000"
var2="efca3250"
var3="$(echo "ibase=16; ${var1^^}" | bc)"
var4="$(echo "ibase=16; ${var2^^}" | bc)"
var5="$(($var4-$var3))"
echo "Diference $var5"
But there is no need to use bc [1], as bash could perform the translation and substraction directly:
#!/bin/bash
var1="bfca3000"
var2="efca3250"
var5="$(( 16#$var2 - 16#$var1 ))"
echo "Diference $var5"
[1] 참고 : 원래 스크립트에서 bash로 차이를 계산 했으므로 값을 64 비트 수학으로 표현할 수 있다고 가정합니다. Bash는 64 비트로 컴파일 된 경우 ((2 ** 63) -1)보다 작은 정수로 제한됩니다. 그것은 그러한 제한이없는 bc와의 유일한 차이점이 될 것입니다.
대시 및 기타 셸에서 사용할 수 있습니다
printf "%d\n" (your hexadecimal number)
16 진수를 10 진수로 변환합니다. 이것은 bash 또는 ksh가 아닙니다.
참고 URL : https://stackoverflow.com/questions/13280131/hexadecimal-to-decimal-in-shell-script
'Programming' 카테고리의 다른 글
Visual Studio Code에 상자 선택 / 여러 줄 편집이 있습니까? (0) | 2020.07.16 |
---|---|
간단한 Getter / Setter 설명 (0) | 2020.07.16 |
클래스 이름 또는 ID로 요소를 얻는 방법 (0) | 2020.07.15 |
PHP curl에서 변수로 쿠키를 얻는 방법 (0) | 2020.07.15 |
파일로서의 Java 자원 (0) | 2020.07.15 |