Using "Printf" for Hex Values in Android Shell

Using printf for hex values in android shell

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)"
}

bash convert hex string to integer code using printf failing

You don't need printf in this case, you can use bash's $((base#number)) construct.

#!/bin/bash -

mystring="5e51584a4c"

for ((i=0; i<${#mystring}; i+=2)); do
snumber="${mystring:i:2}"
echo "number as string=${snumber}"
echo "number=$((16#${snumber}))"
done

ShellScript hex to decimal

If you are using gawk, you could do as following. input.txt is the file you capture the events.

$ awk '{printf "%s ", $1;for(i=2;i<=NF;i++){v="0x"$i;printf "%d ",strtonum(v)};printf "\n";}' input.txt 
/dev/input/event2: 3 57 29
/dev/input/event2: 3 53 361

Read binary stdout data like screencap data from adb shell?

Sorry to be posting an answer to an old question, but I just came across this problem myself and wanted to do it only through the shell. This worked well for me:

adb shell screencap -p | sed 's/^M$//' > screenshot.png

That ^M is a char I got by pressing ctrl+v -> ctrl+m, just noticed it doesn't work when copy-pasting.

adb shell screencap -p | sed 's/\r$//' > screenshot.png

did the trick for me as well.

How do you format an unsigned long long int using printf?

Use the ll (el-el) long-long modifier with the u (unsigned) conversion. (Works in windows, GNU).

printf("%llu", 285212672);

Shell script works in bash but not on 'adb shell'

When the value of $x is just =, it looks like:

if [ = = " " -o -z = ]; then

and it's apparently getting confused by the = that should be a value rather than an operator.

Change it to:

if [ "x${x}" = "x " -o "x${x}" = "x" ]; then


Related Topics



Leave a reply



Submit