Re: [Help-bash] Reading and handling "control" characters from a file
From:
Davide Brini
Subject:
Re: [Help-bash] Reading and handling "control" characters from a file
Date:
Sun, 22 Apr 2012 00:50:25 +0200
On Sat, 21 Apr 2012 14:13:52 -0400, Chet Ramey <address@hidden> wrote:
> On 4/19/12 6:11 PM, Conrad J. Sabatier wrote:
> > I've just started doing a little prototyping in bash for a program I'll
> > eventually code most likely in C, and have hit a serious stumbling
> > block re: the handling of characters (bytes) in the very low range of
> > the ASCII table.
>
> Bash variables are strings of characters. There is a difference between
> a character with ASCII value 4 and a character with the ASCII value 52
> ("4"). Shell arithmetic convers the latter into numbers using the
> equivalent of strtol() before use.
>
> To make what you want work, you'll have to figure out some way to offset
> the value you read from the file (i.e., c+'0') before attempting to use it
> in an arithmetic context. Maybe perl or something like that could help.
> It's quite difficult to do using just what the shell provides.
Shouldn't the single quote trick work here? eg
$ a=$(printf '\x4\n')
$ printf "%d\n" "'$a"
4
--
D.