Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

printf Hexadecimal output

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-10-2010
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 22 June 2015, 6:47 PM EDT
Posts: 6,399
Thanks: 288
Thanked 675 Times in 644 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 Unix and Linux 12-10-2010
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 September 2015, 1:43 AM EDT
Location: Amsterdam
Posts: 10,431
Thanks: 358
Thanked 2,769 Times in 2,475 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 Unix and Linux 12-10-2010
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 31 August 2015, 6:17 AM EDT
Location: The Netherlands
Posts: 7,732
Thanks: 129
Thanked 549 Times in 516 Posts
@Scrutinizer,

Methyl means the ascii code of the character "2" in hexadecimal, which should be 32.
    #4  
Old Unix and Linux 12-10-2010
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 31 August 2015, 4:41 AM EDT
Location: /dev/ph
Posts: 4,952
Thanks: 70
Thanked 466 Times in 431 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 Unix and Linux 12-10-2010
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 September 2015, 1:43 AM EDT
Location: Amsterdam
Posts: 10,431
Thanks: 358
Thanked 2,769 Times in 2,475 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 Unix and Linux 12-10-2010
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 22 June 2015, 6:47 PM EDT
Posts: 6,399
Thanks: 288
Thanked 675 Times in 644 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 Unix and Linux 12-10-2010
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 31 August 2015, 9:41 PM EDT
Location: NM
Posts: 10,530
Thanks: 353
Thanked 880 Times in 818 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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:03 AM.