So here are 2 POSIX-compliant and fully portable awk functions, one for hex representation, the other for combined BE32 and LE32 actual binary bytes. Specify either L or LE (in any case) in 2nd function argument to request for LE32, otherwise it defaults to BE32.
It also has auto detection for byte mode vs. UTF-8 mode, and adjusts necessary offsets accordingly. hex32() assumes unsigned input only, but binary32() properly range clamps, including negative inputs.
function hex32(__, _, ___) {
return (__ = int(__)) < (___ = (_ += _ += _ ^= _<_)^(_ * _)) \
? sprintf("0x[%.*X]", _ + _, __) \
: sprintf("0x[%.*X][%.*X]", _ += _,
(__ - (__ %= ___)) / ___, _, __)
}
function binary32(__, _, _1_, _2_, _3_, _4_) {
_1_ = int(__)
__ = _ = (_ ~ /^[Ll][Ee]?$/)
_ ^= _4_ = _ += _ += _ += !_
_1_ += ((_1_ %= _4_ = _^_4_) < !_) * _4_
_4_ = ((_3_ = ((_2_ = (_1_ - (_1_ %= _)) / _) \
- (_2_ %= _)) / _) - (_3_ %= _)) / _
return sprintf("%.*s%c%c%c%c", (__ && (_3_ += _2_ - (_2_ = _3_)) < \
(_4_ += _1_ - (_1_ = _4_))) < !_,
FLG_AWK_UTF8 ? _ *= _ * _ : _ = !_,
_4_ + _, _3_ + _, _2_ + _, _1_ + _)
}
BEGIN { FLG_AWK_UTF8 = !+sprintf("%c", 5^5) }
89 0x[00000059]
4567 0x[000011D7]
76543 0x[00012AFF]
23456789 0x[0165EC15]
61277761 0x[03A70641]
3221225473 0x[C0000001]
3745221223 0x[DF3B8A67]
FLG_AWK_UTF8 = !+sprintf("%c", 5^5)
The detection here works by leveraging 8-bit wraparound behavior in byte mode to yield the ASCII integer "5" for an input value of 5^5, but that same value in Unicode mode prints this 3-byte UTF-8 Telugu character "వ" instead.