The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check for a particular character inside a file and substitute with a given character? karthikprasathk AIX 1 07-01-2008 04:29 AM
read a variable character by character, substitute characters with something else vipervenom25 UNIX for Dummies Questions & Answers 2 06-06-2008 04:18 PM
Processing extended ascii character file names in UNIX (BASH scipts) peli UNIX for Advanced & Expert Users 9 04-06-2008 06:17 PM
Non-ascii character detection (perl or grep) srinivasan_85 UNIX for Dummies Questions & Answers 9 02-20-2007 08:38 AM
ASCII character to accept logon password gord UNIX for Dummies Questions & Answers 8 02-02-2006 03:58 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-09-2008
sszd sszd is offline
Registered User
  
 

Join Date: Dec 2007
Location: Denver
Posts: 23
Ascii value of character?

Is there a way to determine the ascii value of a character? For example, let's say a shell variable has the value 'A'. I would like it's ascii value (e.g. 65 in this case). I would like to do this from a script (preferably ksh).
  #2 (permalink)  
Old 09-09-2008
sszd sszd is offline
Registered User
  
 

Join Date: Dec 2007
Location: Denver
Posts: 23
I just stumbled upon the following...

$ printf "%d" "'A"

This will give me 65, so likewise I can certainly do this...

$ var=A
$ printf "%d" "'${var}"

What I don't understand is the single quote immediately preceding the character. This is the only way it'll work, but I've never seen that syntax before. I quickly glanced at the man pages for printf and I still don't fully understand this. Can someone please explain? Thank you.
  #3 (permalink)  
Old 09-09-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Single quotes like 'A' around a character is the convention used in the C language to distinguish a character from a string of characters like "A" which is enclosed in double quotes. That's all there is to it.
  #4 (permalink)  
Old 09-09-2008
sszd sszd is offline
Registered User
  
 

Join Date: Dec 2007
Location: Denver
Posts: 23
Quote:
Originally Posted by shamrock View Post
Single quotes like 'A' around a character is the convention used in the C language to distinguish a character from a string of characters like "A" which is enclosed in double quotes. That's all there is to it.
This is not a "pair" of single quotes. It's simply a single quote (by itself) preceding the character (i.e. 'A) . That's what I've never seen before. In fact, if you use a pair of single quotes, the conversion to the ascii value does not work.
  #5 (permalink)  
Old 09-10-2008
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Interesting. From info printf...
Quote:
* If the leading character of a numeric argument is `"' or `'' then
its value is the numeric value of the immediately following
character. Any remaining characters are silently ignored if the
`POSIXLY_CORRECT' environment variable is set; otherwise, a
warning is printed. For example, `printf "%d" "'a"' outputs `97'
on hosts that use the ASCII character set, since `a' has the
numeric value 97 in ASCII.
  #6 (permalink)  
Old 09-10-2008
sszd sszd is offline
Registered User
  
 

Join Date: Dec 2007
Location: Denver
Posts: 23
Quote:
Originally Posted by Ygor
Interesting. From info printf...
Quote:
* If the leading character of a numeric argument is `"' or `'' then
its value is the numeric value of the immediately following
character. Any remaining characters are silently ignored if the
`POSIXLY_CORRECT' environment variable is set; otherwise, a
warning is printed. For example, `printf "%d" "'a"' outputs `97'
on hosts that use the ASCII character set, since `a' has the
numeric value 97 in ASCII.
Quote:
Originally Posted by fpmurphy
From POSIX.1-2001 ..
Quote:
The argument operands will be treated as strings if the corresponding conversion character is b, c or s; otherwise, it will be evaluated as a C constant, as described by the ISO C standard, with the following extensions:

* If the leading character is a single- or double-quote, the value will be the numeric value in the underlying codeset of the character following the single- or double-quote.
Both of these seem to answer my question. None of the man pages (on Solaris 10) explained this with this kind of detail. Thank you.
  #7 (permalink)  
Old 09-10-2008
Dave Miller's Avatar
Dave Miller Dave Miller is offline
Registered User
  
 

Join Date: Dec 2007
Location: Northern NJ, USA
Posts: 151
This is slightly off topic, but may give a clue:


I use a report generation program (CyberQuery). One option is to output the report as an Excel document.

Within Excel, if any cell of the document is all digits, but the source field was Aplha format, the data is preceeded by a single quote.

On the spreadsheet you see the data, including leading zeros (if any), but you do not see the leading quote.

Only when you select one of those cells, and look up to the cell contents field, do you see the single leading quote.


Hope that helps, or at least gives you something to research / consider.
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0