在波斯数字中,۰۱۲۳۴۵۶۷۸۹
等同0123456789
于欧洲数字。
如何将波斯数字(in UTF-8
)转换为ASCII?
例如,我想۲۱
成为21
。
我们可以利用波斯数字的UNICODE代码点是连续的并且从0到9进行排序的事实:
$ printf '%b' '\U06F'{0..9}
۰۱۲۳۴۵۶۷۸۹
这意味着最后一个十六进制数字是十进制值:
$ echo $(( $(printf '%d' "'۲") & 0xF ))
2
这使这个简单的循环成为转换工具:
#!/bin/bash
( ### Use a locale that use UTF-8 to make the script more reliable.
### Maybe something like LC_ALL=fa_IR.UTF-8 for you?.
LC_ALL=en_US.UTF-8
a="$1"
while (( ${#a} > 0 )); do
# extract the last hex digit from the UNICODE code point
# of the first character in the string "$a":
printf '%d' $(( $(printf '%d' "'$a") & 15 ))
a=${a#?} ## Remove one character from $a
done
)
echo
用作:
$ sefr.sh ۰۱۲۳۴۵۶۷۸۹
0123456789
$ sefr.sh ۲۰۱
201
$ sefr.sh ۲۱
21
请注意,此代码还可以转换阿拉伯数字和拉丁数字(即使混合使用):
$ sefr.sh ۴4٤۵5٥۶6٦۷7٧۸8٨۹9٩
444555666777888999
$ sefr.sh ٤٧0٠٦7١٣3٥۶٦۷
4700671335667
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句