Issue
CANMIC_BASE64_DATA="FxEYEhkTGhQbFRwWIQcBCAIJAwoECwUMBjEHAQgCCQMKBAsFDAYAgICAgICAgICAgICAAICAgICAgICAgICAgAu003du003d"
CANMIC_HEX_DATA=$(base64 -d -i <<<$CANMIC_BASE64_DATA | hexdump -v -e '/1 "%02x," ')
I get hex data in CANMIC_BASE64_DATA*
and then store this data into array
array_len=${#array[@]}
Once I store it into array it store it as decimal. But I was it should be in hex only in array. I need to convert base64
into decimal similar to CANMIC_HEX_DATA
dumping.
Solution
Given your base64 encoded string, you can assign each byte to an array.
Either you assign the array with hexadecimal values:
arr1=($(base64 -d <<< "FxEYEhkTGhQbFRwWIQcBCAIJAwoECwUMBjEHAQgCCQMKBAsFDAYAgICAgICAgICAgICAAICAgICAgICAgICAgAu003du003d" | hexdump -v -e '/1 "0x%02x " '))
printf "%s" "${arr1[0]}"
0x17
printf "%d" "${arr1[0]}"
23
printf "%x" "${arr1[0]}"
17
Either you assign your array with decimal values:
arr2=($(base64 -d <<< "FxEYEhkTGhQbFRwWIQcBCAIJAwoECwUMBjEHAQgCCQMKBAsFDAYAgICAgICAgICAgICAAICAgICAgICAgICAgAu003du003d" | hexdump -v -e '/1 "%02d " '))
$ printf "%s" "${arr2[0]}"
23
$ printf "%d" "${arr2[0]}"
23
$ printf "%x" "${arr2[0]}"
17
The only difference is the hexdump
format.
Note that you can't deal with binary directly with variable as the shell will refuse to accept it.
For example, with bash, trying var="$(echo -e "\x00")"
will result in the error bash: warning: command substitution: ignored null byte in input
Answered By - oliv