Integer to hexadecimal in bash

Create a function hex which works as

$ hex 1
0x1

and

$ echo 1 | hex
0x1
function hex {
if [ -t 0 ]; then
		if [ $# -eq 1 ]; then
	    	printf '0x%x\n' $1
	    	return
		fi
	else
		while read -r line ; do
			printf '0x%x\n' $line
		done
		return
	fi
	echo "usage: hex n"
	return 1
}

(Note that this uses the pattern from this TIL post.)