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;
}







0















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.










share|improve this question

























  • 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




















0















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.










share|improve this question

























  • 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
















0












0








0


0






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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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



















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














2 Answers
2






active

oldest

votes


















1














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





share|improve this answer































    1














    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





    share|improve this answer


























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      1














      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





      share|improve this answer




























        1














        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





        share|improve this answer


























          1












          1








          1







          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





          share|improve this answer













          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 4:44









          Gordon DavissonGordon Davisson

          71.6k97994




          71.6k97994

























              1














              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





              share|improve this answer






























                1














                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





                share|improve this answer




























                  1












                  1








                  1







                  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





                  share|improve this answer















                  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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 4:56

























                  answered Nov 23 '18 at 4:50









                  David C. RankinDavid C. Rankin

                  43.9k33252




                  43.9k33252






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?