-
|
Is there any way to get the number of bytes in a binary variable without using typeset -b bin
read -n 10 bin < /dev/zero
print ${#bin}
16
print $bin
AAAAAAAAAAAAAA==
print $(printf %B bin | wc -c)
10 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Sure. Here is a solution using ksh only and thus avoiding external programs like wc. ksh93 displayes the results of a binary varible in base64. Base64 encoding of a binary varabile will consume four base64 encoding characters for every 3 bytes of the binary variable. Padding of the unused bits is represented by an equal sign. All that is needed to determine the count of bytes that were encoded is subtract the number of padding chars from the base64 encoded string length, divide by 4, multiple by 3, then floor the result. [ 1/4 * 3 = 3/4 = .75 ] Continuing with your example setup: Version 1: Base64 binary variable byte count $ unset bin; typeset -b bin
$ read -n 10 bin < /dev/zero
$ typeset -p bin
typeset -b bin=AAAAAAAAAAAAAA==''
$
$ true ${bin%%=*}; echo $(( floor( (${#bin} - ${#.sh.match}) * .75) ))
10Version 2: Base64 binary variable byte count storing result in short integer x avoiding the floor as conversion to integer truncrates the fraction. $ unset b x; typeset -si x; typeset -b b; read -n 22 b <<< 'Kornshell is the Best!'
$ echo $b | base64 --decode; echo
Kornshell is the Best!
$ true ${b%%=*}; (( x = ( ${#b} - ${#.sh.match} ) * .75 )); typeset -p b x
typeset -b b=S29ybnNoZWxsIGlzIHRoZSBCZXN0IQ==''
typeset -s -i x=22
$ What stuff does:
|
Beta Was this translation helpful? Give feedback.
Sure. Here is a solution using ksh only and thus avoiding external programs like wc.
ksh93 displayes the results of a binary varible in base64. Base64 encoding of a binary varabile will consume four base64 encoding characters for every 3 bytes of the binary variable. Padding of the unused bits is represented by an equal sign.
All that is needed to determine the count of bytes that were encoded is subtract the number of padding chars from the base64 encoded string length, divide by 4, multiple by 3, then floor the result. [ 1/4 * 3 = 3/4 = .75 ]
Continuing with your example setup:
Version 1: Base64 binary variable byte count