Using “printf” for hex values in android shell

人走茶凉 提交于 2019-12-02 07:53:38

Fixing printf in toybox is great.

But in case anyone would like to print out a number converted to hex (or pretty much any other reasonable base from 2 to 36 if they would be so inclined) on an unrooted device with the old toybox (or no toybox at all) - here is a way how to do it using typeset built-in of mksh:

baseconv(){ typeset -Ui${3:-16} -Z35 x=$1; echo ${x: -${2:-8}};}

func1()
{
    A=100
    HEXA=$(baseconv $A 4 16)
    echo "A - ${A} HEXA - ${HEXA}"
}

or just make a specific function for the printf "%04x" case:

printf04x(){ typeset -Ui16 -Z7 x=$1; echo ${x: -4};}

func1()
{
    A=100
    echo "A - ${A} HEXA - $(printf04x $A)"
}

printf in Android is linking to toybox

root:/ # which printf
/system/bin/printf
root:/ # ls -l /system/bin/printf
lrwxr-xr-x root     shell             2016-11-14 21:02 printf -> toybox

So the issue turned out to be in toybox printf.

The issue is now fixed - https://github.com/landley/toybox/issues/54

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!