printf Hexadecimal output | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

printf Hexadecimal output

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-10-2010
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 18 April 2014, 5:13 AM EDT
Posts: 6,396
Thanks: 287
Thanked 672 Times in 642 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
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 October 2014, 1:11 AM EDT
Location: Amsterdam
Posts: 9,487
Thanks: 280
Thanked 2,398 Times in 2,150 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
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 1 October 2014, 8:45 AM EDT
Location: The Netherlands
Posts: 7,717
Thanks: 123
Thanked 537 Times in 507 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
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 30 September 2014, 10:26 AM EDT
Location: /dev/ph
Posts: 4,811
Thanks: 63
Thanked 425 Times in 394 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
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 October 2014, 1:11 AM EDT
Location: Amsterdam
Posts: 9,487
Thanks: 280
Thanked 2,398 Times in 2,150 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 Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 18 April 2014, 5:13 AM EDT
Posts: 6,396
Thanks: 287
Thanked 672 Times in 642 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.

Convert clear text into binary

Last edited by methyl; 12-10-2010 at 10:35 AM..
Sponsored Links
    #7  
Old 12-10-2010
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 1 October 2014, 6:42 AM EDT
Location: NM
Posts: 10,221
Thanks: 278
Thanked 800 Times in 744 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 10:13 AM.