Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, i need help with creating script that converts hex to dec. But without using bc calculator or other methods that could convert it in one line. I need to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    3

    Bash script HELP - Hex to Dec


    Hi, i need help with creating script that converts hex to dec. But without using bc calculator or other methods that could convert it in one line. I need to make something like this script that converts dec to hex.

    Thanks!

    1. #!/bin/bash
    2.
    3. declare -r HEX_DIGITS="0123456789ABCDEF"
    4.
    5. dec_value=$1
    6. hex_value=""
    7.
    8. until [ $dec_value == 0 ]; do
    9.
    10. rem_value=$((dec_value % 16))
    11. dec_value=$((dec_value / 16))
    12.
    13. hex_digit=${HEX_DIGITS:$rem_value:1}
    14.
    15. hex_value="${hex_digit}${hex_value}"
    16.
    17. done
    18.
    19. echo -e "${hex_value}"

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Homework questions are not allowed:
    http://www.linuxforums.org/forum/lin...ums-rules.html

    If you have a specific conceptual question, we will be happy to help, but we won't do your homework for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •