Convert only characters to decimal in hexa string
By Sarah Rodriguez •
I have the following hexa string
17a62I want to convert it to decimal. I am using the following approach:
echo $((16#17a62))It results in
96866but i dont want this, i just want to replace the "alphabet character" in the string with its decimal value. Like decimal value of a hex is 10 so i want the output to be like:
171062How can i achieve this ? any help would be much appreciated.
32 Answers
Could do it with sed:
$ printf %d $(sed 's/./0x& /g' <<< 17a26b12) If Perl is an option, you could do something like this:
$ echo 17a62 | perl -pe 's/[a-f]/hex $&/e'
171062If you really need to do this natively in the shell, then with bash:
$ hexa=17a62
$ [[ $hexa =~ ([0-9]*)([a-f])([0-9]*) ]] && printf '%d%d%d\n' "${BASH_REMATCH[1]}" $((16#"${BASH_REMATCH[2]}")) "${BASH_REMATCH[3]}"
171062 More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"