Operating System - HP-UX
1825737 Members
2564 Online
109687 Solutions
New Discussion

Re: character to numeric representation

 
SOLVED
Go to solution
jerry1
Super Advisor

character to numeric representation

Does anyone know how to convert a character
string or letter to decimal representation.

e.g. a = ?
1a = ?
6 REPLIES 6
Hein van den Heuvel
Honored Contributor
Solution

Re: character to numeric representation

For one occruance, Or in a script?

For a quick lookup I use (a clone from) http://www.asciitable.com/

Google: asciitable.

For scripts I use whatever the language offers. For example in perl:

$ perl -e 'print chr(65)'
and
$ perl -le 'print ord("C")'

or

$ perl -le 'printf "%c", 0x43'

Enjoy!
Hein.
jerry1
Super Advisor

Re: character to numeric representation

Used in a korn shell script.
grep'ng for particular string which may
have string -11 or -1a , etc...

11 in -11 is used as index number in array
when that string is found. Can't use 1a
unless "a" is converted to it's decimal
representation.
So if "a" is 61 or 141, according to
/usr/share/lib/pub/ascii, then the value for
1a that I would need would be 161 or 1141.
James R. Ferguson
Acclaimed Contributor

Re: character to numeric representation

Hi Jerry"

Depending on your needs, you should also find 'xd' (also known as 'od') useful. You can list a file in hexadecimal, octal, or decimal:

# xd -tax /etc/hosts

# xd -tao /etc/hosts

# xd -tad /etc/hosts

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: character to numeric representation

I must be a little dense today.
That explanaiton does nto really cut it for me.

>> may have string -11 or -1a , etc...

What is the exact range?
Can it return 1A or A1 or 1$ or 1!
uppercae or lowercase?

I get the feeling that it will only return 0..9 or a..z



In that case you can use the shell base#n conversion for base 36.


$ let x=36#a
$ echo $x
10
$ let x=36#z
$ echo $x
35
$ let x=36#3
$ echo $x
3

So if "a" is 61 or 141, according to

It is not 61 or 141 is 16#61 AND 8#141
In other words, its OCTAL value is 141
It's HEX value 61 and its decimal value is 97.

/usr/share/lib/pub/ascii, then the value for
1a that I would need would be 161 or 1141.


btw..

here is an other, 'shell only' way to convert a character to its octal value string:
x=$( printf a | od -An -to1 )
$ echo $x

and back

$ echo "\0141"
a

hex: x=$(printf a | xd -An -tx1)
decimal: x=$(printf a | xd -An -td1)

What is the _real_ problem you are trying to solve. Mayb a shell script is not the optimal way to approach this. String manipulation tends to be a lot easier within AWK or PERL or such.


Good luck!

Hein.

jerry1
Super Advisor

Re: character to numeric representation

Yes, I figured out that 61 from the table
is the oct rep of the letter "a"

# perl -le 'printf "%c", 0x61'
a


And you confirmed that dec of "a" is 97.

# perl -le 'printf "%c", 97'
a

So your dec conversion line will work just
fine to convert any characters to dec.

dec output is what I was looking for. So
for the letter "a" it is 97. "b" 98, etc..

# printf a
97

I'll just run what is found as $a in

# numval=`printf $a | xd -An -td1`


Thanks as always. Ya'll are so good.
jerry1
Super Advisor

Re: character to numeric representation

Yes, I figured out that 61 from the table
is the oct rep of the letter "a"

# perl -le 'printf "%c", 0x61'
a


And you confirmed that dec of "a" is 97.

# perl -le 'printf "%c", 97'
a

So your dec conversion line will work just
fine to convert any characters to dec.

dec output is what I was looking for. So
for the letter "a" it is 97. "b" 98, etc..

# printf a
97

I'll just run what is found as $a in

# numval=`printf $char | xd -An -td1`


Thanks as always. Ya'll are so good.