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)) - ...
- 12-08-2010 #1Just 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
- 12-11-2010 #2
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"
- 12-11-2010 #3Just Joined!
- Join Date
- Dec 2010
- Posts
- 3
- 12-12-2010 #4
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"


Reply With Quote
