converting hex to decimal with bash
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I've seen some strange things.
I tried to convert hex to dec with bash Shell
I used very very simple command.
$ g_receiverBeforeToken=1158e460913d00000
$ echo $((16#$g_receiverBeforeToken))
1553255926290448384
As you guys know, this result should be '20000000000000000000'.
When I put in any other hex number, it was correct. But only 1553255926290448384 was weird.
bash
add a comment |
I've seen some strange things.
I tried to convert hex to dec with bash Shell
I used very very simple command.
$ g_receiverBeforeToken=1158e460913d00000
$ echo $((16#$g_receiverBeforeToken))
1553255926290448384
As you guys know, this result should be '20000000000000000000'.
When I put in any other hex number, it was correct. But only 1553255926290448384 was weird.
bash
So you're saying, for example,1553255926290448383
works fine? By the way, are you aware that20000000000000000000
is greater than 2**64? The word size on your machine is probably 64 bits. 64 bits is 16 hexadecimal digits.1158e460913d00000
is 17 hexadecimal digits.
– lurker
Nov 23 '18 at 4:34
add a comment |
I've seen some strange things.
I tried to convert hex to dec with bash Shell
I used very very simple command.
$ g_receiverBeforeToken=1158e460913d00000
$ echo $((16#$g_receiverBeforeToken))
1553255926290448384
As you guys know, this result should be '20000000000000000000'.
When I put in any other hex number, it was correct. But only 1553255926290448384 was weird.
bash
I've seen some strange things.
I tried to convert hex to dec with bash Shell
I used very very simple command.
$ g_receiverBeforeToken=1158e460913d00000
$ echo $((16#$g_receiverBeforeToken))
1553255926290448384
As you guys know, this result should be '20000000000000000000'.
When I put in any other hex number, it was correct. But only 1553255926290448384 was weird.
bash
bash
edited Nov 23 '18 at 4:43
jkdev
5,28553565
5,28553565
asked Nov 23 '18 at 4:22
JuneJune
216
216
So you're saying, for example,1553255926290448383
works fine? By the way, are you aware that20000000000000000000
is greater than 2**64? The word size on your machine is probably 64 bits. 64 bits is 16 hexadecimal digits.1158e460913d00000
is 17 hexadecimal digits.
– lurker
Nov 23 '18 at 4:34
add a comment |
So you're saying, for example,1553255926290448383
works fine? By the way, are you aware that20000000000000000000
is greater than 2**64? The word size on your machine is probably 64 bits. 64 bits is 16 hexadecimal digits.1158e460913d00000
is 17 hexadecimal digits.
– lurker
Nov 23 '18 at 4:34
So you're saying, for example,
1553255926290448383
works fine? By the way, are you aware that 20000000000000000000
is greater than 2**64? The word size on your machine is probably 64 bits. 64 bits is 16 hexadecimal digits. 1158e460913d00000
is 17 hexadecimal digits.– lurker
Nov 23 '18 at 4:34
So you're saying, for example,
1553255926290448383
works fine? By the way, are you aware that 20000000000000000000
is greater than 2**64? The word size on your machine is probably 64 bits. 64 bits is 16 hexadecimal digits. 1158e460913d00000
is 17 hexadecimal digits.– lurker
Nov 23 '18 at 4:34
add a comment |
2 Answers
2
active
oldest
votes
It's not just that number, it's any number over 7fffffffffffffff, because it's using 64-bit integers and that's the largest one. 16-digit numbers over that wrap around and become negative (because of two's complement representation of signed integers):
$ echo $((16#7fffffffffffffff))
9223372036854775807
$ echo $((16#7fffffffffffffff + 1))
-9223372036854775808
$ echo $((16#8000000000000000))
-9223372036854775808
Past ffffffffffffffff (aka -1), it wraps back to zero:
$ echo $((16#ffffffffffffffff))
-1
$ echo $((16#ffffffffffffffff + 1))
0
$ echo $((16#10000000000000000))
0
Net result: only the last 16 hex digits actually matter; anything past that gets dropped off the high end of the 64-bit integer representation:
$ echo $((16#0000000000000010))
16
$ echo $((16#10000000000000010))
16
$ echo $((16#ffffffff0000000000000010))
16
Since 1553255926290448384 is 17 digits long, the first digit is being dropped off in this way:
$ echo $((16#1158e460913d00000))
1553255926290448384
$ echo $((16#158e460913d00000))
1553255926290448384
$ echo $((16#121345158e460913d00000))
1553255926290448384
add a comment |
You can do what you are looking to do using bc
as a base conversion with a short script:
#!/bin/bash
## validate sufficient input
test -n "$1" || {
printf "n error: insufficient input. usage: %s num [obase (2)] [ibase (10)]nn" "${0//*//}"
exit 1
}
## test for help
test "$1" = "-h" || test "$1" = "--help" && {
printf "n usage: %s num [obase (2)] [ibase (10)] -- to convert numbernn" "${0//*//}"
exit 0
}
## validate numeric value given for conversion (bash only test)
ival="${1^^}"
[[ $ival =~ [^0-9A-F] ]] && {
printf "n error: invalid input. Input must be within upper/lower case hex character set [0-9A-Fa-f]nn"
exit 1
}
ob=${2:-2}
ib=${3:-10}
# set obase first before ibase -- or weird things happen.
printf "obase=%d; ibase=%d; %sn" $ob $ib $ival | bc
Example Use/Output
$ bash hex2dec.sh -h
usage: hex2dec.sh num [obase (2)] [ibase (10)] -- to convert number
Using your example:
$ bash hex2dec.sh 1158e460913d00000 10 16
20000000000000000000
Also handy if you want it in binary as well:
$ bash hex2dec.sh 1158e460913d00000 2 16
10001010110001110010001100000100100010011110100000000000000000000
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440622%2fconverting-hex-to-decimal-with-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's not just that number, it's any number over 7fffffffffffffff, because it's using 64-bit integers and that's the largest one. 16-digit numbers over that wrap around and become negative (because of two's complement representation of signed integers):
$ echo $((16#7fffffffffffffff))
9223372036854775807
$ echo $((16#7fffffffffffffff + 1))
-9223372036854775808
$ echo $((16#8000000000000000))
-9223372036854775808
Past ffffffffffffffff (aka -1), it wraps back to zero:
$ echo $((16#ffffffffffffffff))
-1
$ echo $((16#ffffffffffffffff + 1))
0
$ echo $((16#10000000000000000))
0
Net result: only the last 16 hex digits actually matter; anything past that gets dropped off the high end of the 64-bit integer representation:
$ echo $((16#0000000000000010))
16
$ echo $((16#10000000000000010))
16
$ echo $((16#ffffffff0000000000000010))
16
Since 1553255926290448384 is 17 digits long, the first digit is being dropped off in this way:
$ echo $((16#1158e460913d00000))
1553255926290448384
$ echo $((16#158e460913d00000))
1553255926290448384
$ echo $((16#121345158e460913d00000))
1553255926290448384
add a comment |
It's not just that number, it's any number over 7fffffffffffffff, because it's using 64-bit integers and that's the largest one. 16-digit numbers over that wrap around and become negative (because of two's complement representation of signed integers):
$ echo $((16#7fffffffffffffff))
9223372036854775807
$ echo $((16#7fffffffffffffff + 1))
-9223372036854775808
$ echo $((16#8000000000000000))
-9223372036854775808
Past ffffffffffffffff (aka -1), it wraps back to zero:
$ echo $((16#ffffffffffffffff))
-1
$ echo $((16#ffffffffffffffff + 1))
0
$ echo $((16#10000000000000000))
0
Net result: only the last 16 hex digits actually matter; anything past that gets dropped off the high end of the 64-bit integer representation:
$ echo $((16#0000000000000010))
16
$ echo $((16#10000000000000010))
16
$ echo $((16#ffffffff0000000000000010))
16
Since 1553255926290448384 is 17 digits long, the first digit is being dropped off in this way:
$ echo $((16#1158e460913d00000))
1553255926290448384
$ echo $((16#158e460913d00000))
1553255926290448384
$ echo $((16#121345158e460913d00000))
1553255926290448384
add a comment |
It's not just that number, it's any number over 7fffffffffffffff, because it's using 64-bit integers and that's the largest one. 16-digit numbers over that wrap around and become negative (because of two's complement representation of signed integers):
$ echo $((16#7fffffffffffffff))
9223372036854775807
$ echo $((16#7fffffffffffffff + 1))
-9223372036854775808
$ echo $((16#8000000000000000))
-9223372036854775808
Past ffffffffffffffff (aka -1), it wraps back to zero:
$ echo $((16#ffffffffffffffff))
-1
$ echo $((16#ffffffffffffffff + 1))
0
$ echo $((16#10000000000000000))
0
Net result: only the last 16 hex digits actually matter; anything past that gets dropped off the high end of the 64-bit integer representation:
$ echo $((16#0000000000000010))
16
$ echo $((16#10000000000000010))
16
$ echo $((16#ffffffff0000000000000010))
16
Since 1553255926290448384 is 17 digits long, the first digit is being dropped off in this way:
$ echo $((16#1158e460913d00000))
1553255926290448384
$ echo $((16#158e460913d00000))
1553255926290448384
$ echo $((16#121345158e460913d00000))
1553255926290448384
It's not just that number, it's any number over 7fffffffffffffff, because it's using 64-bit integers and that's the largest one. 16-digit numbers over that wrap around and become negative (because of two's complement representation of signed integers):
$ echo $((16#7fffffffffffffff))
9223372036854775807
$ echo $((16#7fffffffffffffff + 1))
-9223372036854775808
$ echo $((16#8000000000000000))
-9223372036854775808
Past ffffffffffffffff (aka -1), it wraps back to zero:
$ echo $((16#ffffffffffffffff))
-1
$ echo $((16#ffffffffffffffff + 1))
0
$ echo $((16#10000000000000000))
0
Net result: only the last 16 hex digits actually matter; anything past that gets dropped off the high end of the 64-bit integer representation:
$ echo $((16#0000000000000010))
16
$ echo $((16#10000000000000010))
16
$ echo $((16#ffffffff0000000000000010))
16
Since 1553255926290448384 is 17 digits long, the first digit is being dropped off in this way:
$ echo $((16#1158e460913d00000))
1553255926290448384
$ echo $((16#158e460913d00000))
1553255926290448384
$ echo $((16#121345158e460913d00000))
1553255926290448384
answered Nov 23 '18 at 4:44
Gordon DavissonGordon Davisson
71.6k97994
71.6k97994
add a comment |
add a comment |
You can do what you are looking to do using bc
as a base conversion with a short script:
#!/bin/bash
## validate sufficient input
test -n "$1" || {
printf "n error: insufficient input. usage: %s num [obase (2)] [ibase (10)]nn" "${0//*//}"
exit 1
}
## test for help
test "$1" = "-h" || test "$1" = "--help" && {
printf "n usage: %s num [obase (2)] [ibase (10)] -- to convert numbernn" "${0//*//}"
exit 0
}
## validate numeric value given for conversion (bash only test)
ival="${1^^}"
[[ $ival =~ [^0-9A-F] ]] && {
printf "n error: invalid input. Input must be within upper/lower case hex character set [0-9A-Fa-f]nn"
exit 1
}
ob=${2:-2}
ib=${3:-10}
# set obase first before ibase -- or weird things happen.
printf "obase=%d; ibase=%d; %sn" $ob $ib $ival | bc
Example Use/Output
$ bash hex2dec.sh -h
usage: hex2dec.sh num [obase (2)] [ibase (10)] -- to convert number
Using your example:
$ bash hex2dec.sh 1158e460913d00000 10 16
20000000000000000000
Also handy if you want it in binary as well:
$ bash hex2dec.sh 1158e460913d00000 2 16
10001010110001110010001100000100100010011110100000000000000000000
add a comment |
You can do what you are looking to do using bc
as a base conversion with a short script:
#!/bin/bash
## validate sufficient input
test -n "$1" || {
printf "n error: insufficient input. usage: %s num [obase (2)] [ibase (10)]nn" "${0//*//}"
exit 1
}
## test for help
test "$1" = "-h" || test "$1" = "--help" && {
printf "n usage: %s num [obase (2)] [ibase (10)] -- to convert numbernn" "${0//*//}"
exit 0
}
## validate numeric value given for conversion (bash only test)
ival="${1^^}"
[[ $ival =~ [^0-9A-F] ]] && {
printf "n error: invalid input. Input must be within upper/lower case hex character set [0-9A-Fa-f]nn"
exit 1
}
ob=${2:-2}
ib=${3:-10}
# set obase first before ibase -- or weird things happen.
printf "obase=%d; ibase=%d; %sn" $ob $ib $ival | bc
Example Use/Output
$ bash hex2dec.sh -h
usage: hex2dec.sh num [obase (2)] [ibase (10)] -- to convert number
Using your example:
$ bash hex2dec.sh 1158e460913d00000 10 16
20000000000000000000
Also handy if you want it in binary as well:
$ bash hex2dec.sh 1158e460913d00000 2 16
10001010110001110010001100000100100010011110100000000000000000000
add a comment |
You can do what you are looking to do using bc
as a base conversion with a short script:
#!/bin/bash
## validate sufficient input
test -n "$1" || {
printf "n error: insufficient input. usage: %s num [obase (2)] [ibase (10)]nn" "${0//*//}"
exit 1
}
## test for help
test "$1" = "-h" || test "$1" = "--help" && {
printf "n usage: %s num [obase (2)] [ibase (10)] -- to convert numbernn" "${0//*//}"
exit 0
}
## validate numeric value given for conversion (bash only test)
ival="${1^^}"
[[ $ival =~ [^0-9A-F] ]] && {
printf "n error: invalid input. Input must be within upper/lower case hex character set [0-9A-Fa-f]nn"
exit 1
}
ob=${2:-2}
ib=${3:-10}
# set obase first before ibase -- or weird things happen.
printf "obase=%d; ibase=%d; %sn" $ob $ib $ival | bc
Example Use/Output
$ bash hex2dec.sh -h
usage: hex2dec.sh num [obase (2)] [ibase (10)] -- to convert number
Using your example:
$ bash hex2dec.sh 1158e460913d00000 10 16
20000000000000000000
Also handy if you want it in binary as well:
$ bash hex2dec.sh 1158e460913d00000 2 16
10001010110001110010001100000100100010011110100000000000000000000
You can do what you are looking to do using bc
as a base conversion with a short script:
#!/bin/bash
## validate sufficient input
test -n "$1" || {
printf "n error: insufficient input. usage: %s num [obase (2)] [ibase (10)]nn" "${0//*//}"
exit 1
}
## test for help
test "$1" = "-h" || test "$1" = "--help" && {
printf "n usage: %s num [obase (2)] [ibase (10)] -- to convert numbernn" "${0//*//}"
exit 0
}
## validate numeric value given for conversion (bash only test)
ival="${1^^}"
[[ $ival =~ [^0-9A-F] ]] && {
printf "n error: invalid input. Input must be within upper/lower case hex character set [0-9A-Fa-f]nn"
exit 1
}
ob=${2:-2}
ib=${3:-10}
# set obase first before ibase -- or weird things happen.
printf "obase=%d; ibase=%d; %sn" $ob $ib $ival | bc
Example Use/Output
$ bash hex2dec.sh -h
usage: hex2dec.sh num [obase (2)] [ibase (10)] -- to convert number
Using your example:
$ bash hex2dec.sh 1158e460913d00000 10 16
20000000000000000000
Also handy if you want it in binary as well:
$ bash hex2dec.sh 1158e460913d00000 2 16
10001010110001110010001100000100100010011110100000000000000000000
edited Nov 23 '18 at 4:56
answered Nov 23 '18 at 4:50
David C. RankinDavid C. Rankin
43.9k33252
43.9k33252
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440622%2fconverting-hex-to-decimal-with-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So you're saying, for example,
1553255926290448383
works fine? By the way, are you aware that20000000000000000000
is greater than 2**64? The word size on your machine is probably 64 bits. 64 bits is 16 hexadecimal digits.1158e460913d00000
is 17 hexadecimal digits.– lurker
Nov 23 '18 at 4:34