Reputation: 411
All,
I have a decrypt process which has a part that reverses an encoded (only as another ascii character) string.
The string is...
vtr«¥ºc€a
And the reverse command I'm using is...
enc_pass="vtr«¥ºc€a"
reverse=""
len=${#enc_pass}
for (( i=$len-1; i>=0; i-- ))
do
reverse="$reverse${enc_pass:$i:1}"
done
echo $reverse
But when I check the length, it comes out as 9 characters long rather than 10, and I've found out its the € character which is being ignored.
Is there any way to cater for this? I'm pretty lost on it, not sure where to go next.
It turns out the main issue was with my LANG on the new server. The old server had LANG="en_GB" and the new server LANG="en_GB.UTF-8"
Changing the LANG back to en_GB resolved the reversing as UTF-8 ignored the character for some reason.
Upvotes: 0
Views: 1865
Reputation: 301
rev command is of no great help, at least for binary files because rev terminates at 0x00.
$ export LC_ALL='en_US.UTF-8'
$ echo -e "\xe4" | rev
rev: stdin: Invalid or incomplete multibyte or wide character
$
$ export LC_ALL='de_DE.ISO-8859-1'
$ echo -e "\xe4" | rev | od -tx1
0000000 e4 0a
0000002
$
$ export LC_ALL='en_US.UTF-8'
$ echo -e "te\x00st" | rev | od -tx1
0000000 65 74 0a
0000003
$
Upvotes: 1
Reputation: 77147
LC_ALL='en_US.UTF-8'
(your code)
echo "${#reverse}"
10
LC_ALL='C'
(your code)
echo "${#reverse}"
16
I think you must just have a localization/encoding problem in your environment. I suspect that would even effect rev
, although it's a nonstandard utility so I can't say for sure. This works fine for me:
rev() {
local -x LC_ALL=en_US.UTF-8
if [[ ! "$1" ]]; then
echo
return
fi
printf '%s' "${1: -1:1}"
rev "${1:0: -1}"
}
$ rev 'vtr«¥ºc€a'
a€cº¥«rtv
Upvotes: 1
Reputation: 785711
You can use rev
utility:
enc_pass="vtr«¥ºc€a"
rev <<< "$enc_pass"
a€cº¥«rtv
Upvotes: 2