Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-10-2010
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
printf Hexadecimal output


Code:
printf "%X\n" "A"
41

printf "%X\n" "2"
2
Expected 32 (not 2).

Is there a "printf" which will output the hexadecimal value of a numeric character?
Sponsored Links
    #2  
Old 12-10-2010
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,346
Thanks: 144
Thanked 1,755 Times in 1,592 Posts

Code:
$ printf "%d\n" 0xA
10


Code:
$ printf "%d\n" 0x20
32


Code:
$ printf "%X\n" 10
A


Code:
$ printf "%X\n" 32
20

Sponsored Links
    #3  
Old 12-10-2010
Moderator
 
Join Date: Feb 2007
Location: The Netherlands
Posts: 7,505
Thanks: 73
Thanked 474 Times in 453 Posts
@Scrutinizer,

Methyl means the ascii code of the character "2" in hexadecimal, which should be 32.
    #4  
Old 12-10-2010
fpmurphy's Avatar
who?
 
Join Date: Dec 2003
Location: /dev/ph
Posts: 4,427
Thanks: 47
Thanked 356 Times in 330 Posts
Quote:
Is there a "printf" which will output the hexadecimal value of a numeric character?
I think the question you meant to ask is
Quote:
Is there a "printf" which will output the hexadecimal value of a numeric character within a string?
If this is the correct question, then the answer is no.
Sponsored Links
    #5  
Old 12-10-2010
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,346
Thanks: 144
Thanked 1,755 Times in 1,592 Posts
@Franklin: Oops...

Code:
$ printf "%X\n" \'A
41
$ printf "%X\n" \'2
32

The Following 2 Users Say Thank You to Scrutinizer For This Useful Post:
Franklin52 (12-10-2010), jim mcnamara (12-10-2010)
Sponsored Links
    #6  
Old 12-10-2010
methyl methyl is offline Forum Staff  
Moderator
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
Thanks Scrutinizer. It's just unbroken my script!

Just for interest I had been having a go at this post but expanding the problem to any character.

http://www.unix.com/aix/150117-conve...to-binary.html

Last edited by methyl; 12-10-2010 at 10:35 AM..
Sponsored Links
    #7  
Old 12-10-2010
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,652
Thanks: 164
Thanked 644 Times in 621 Posts
@Scrutinizer

Where is the \'[value] syntax documented for POSIX shells? I do not think I've seen it. Or more likely I ignored it....

For example:

Code:
echo $(( \'A  + 1 ))

fails in ksh88 on Solaris 10.

This does not:

Code:
> printf "%d\n" \'A
65

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
capture the output of printf into another variable arizah Programming 2 08-10-2010 09:22 AM
How to format the output using float in awk{printf} angelarosh Shell Programming and Scripting 4 05-07-2009 08:26 AM
Help please...output problems with printf. pwanda UNIX for Advanced & Expert Users 3 10-19-2008 07:30 PM
How do I stop printf output from going into seperate txt files chrchcol Shell Programming and Scripting 12 07-26-2006 09:08 PM
fixed-width printf() output on an XmList on Solaris trido Programming 0 11-20-2002 04:21 AM



All times are GMT -4. The time now is 08:24 AM.