Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, who could correct me if im wrong at understanding these lines. 1. rem_value=$((dec_value % 16)) - divides number by 16 and finds its mod. 2. dec_value=$((dec_value / 16)) - ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    3

    Bash script help

    Hi, who could correct me if im wrong at understanding these lines.
    1. rem_value=$((dec_value % 16)) - divides number by 16 and finds its mod.
    2. dec_value=$((dec_value / 16)) - divides number by 16.
    3. hex_digit=${HEX_DIGITS:$rem_value:1} - searches the simbol from HEX_DIGITS. But i dont understand how it works. Does it seaches one by one becouse of that ":1" at the end?.
    4.hex_value="${hex_digit}${hex_value}" - Shows the hex_value. Adding hex_digit with hex_value. But i dont understand how the order in this one works? Becouse hex_digit could be after hex_value so how did it decide where to but the values? Im new at bash scripting so some of my thoughts could be very stupid. Thanks.

    P.S. I apologize for my poor English language.



    if [ $1 == '-h' ]; then
    declare -r HEX_DIGITS="0123456789ABCDEF"
    dec_value=$2
    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

  2. #2
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by TryingMyBest View Post
    Hi, who could correct me if im wrong at understanding these lines.
    1. rem_value=$((dec_value % 16)) - divides number by 16 and finds its mod.

    Correct.
    2. dec_value=$((dec_value / 16)) - divides number by 16.

    Correct.
    3. hex_digit=${HEX_DIGITS:$rem_value:1} - searches the simbol from HEX_DIGITS. But i dont understand how it works. Does it seaches one by one becouse of that ":1" at the end?.

    It expands to a substring of $HEX_DIGITS (which I assume contains 0123456789ABCDEF) beginning with character position $rem_value (counting from 0); the final 1 is the number of characters it returns.
    4.hex_value="${hex_digit}${hex_value}" - Shows the hex_value. Adding hex_digit with hex_value. But i dont understand how the order in this one works? Becouse hex_digit could be after hex_value so how did it decide where to but the values? Im new at bash scripting so some of my thoughts could be very stupid. Thanks.

    It concatenates the value of $hex_digit and $hex_value; it doesn't add them.

    What are you trying to do? It looks as if you are converting a decmal number to a hexadecimal. If so, use printf:

    Code:
    dec_value=42
    printf "%X\n" "$dec_value"

  3. #3
    Just Joined!
    Join Date
    Dec 2010
    Posts
    3
    Quote Originally Posted by cfajohnson View Post
    What are you trying to do? It looks as if you are converting a decmal number to a hexadecimal. If so, use printf:
    Yes im trying to convert decimal to hexadecimal but i need to make whole algorithm for it.

  4. #4
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52

    Code:
    n=${1:-128}
    hexdigits=0123456789ABCDEF
    x=
    while [ $n -gt 0 ]
    do
      rem=$(( $n % 16 ))
      x=${hexdigits:$rem:1}$x
      n=$(( $n / 16 ))
    done
    printf "%s\n" "$x"

Posting Permissions

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