Solved

Char to hex and hex to char in shell

Posted on 2006-07-21
11
6,104 Views
Last Modified: 2013-12-26
Hi experts,

The following printf command converts a char to a hex value:

$ printf "%x" a
61

How can I conver this 61 back to "a" by using printf or any shell command?

Thanks

0
Comment
Question by:myrizvi
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17156998
which shell are you using?
0
 

Author Comment

by:myrizvi
ID: 17159247
Hi ahoffman,

I am using ksh.
0
 
LVL 20

Expert Comment

by:tfewster
ID: 17162071
printf '\x61'

Though I don't understand how `printf "%x" a` produces the hex value of "a"  :-(
0
 

Author Comment

by:myrizvi
ID: 17162842
$ printf '\x61'
x61

The above command is not producing desired result.

printf just converted the "a" to its hex value when used with "%x" option.

 
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17163184
please post version of OS, version of ksh, and result of
  which printf
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Accepted Solution

by:
tfewster earned 125 total points
ID: 17164633
As myrizvi has asked several questions about HP-UX, I assume it's HP-UX 11.x, which comes with ksh Version 11/16/88 and /usr/bin/printf ...

On HP-UX 11.x, `printf "%x" a`  does indeed return "61", though the man page states that it should be an integer argument. Go figure...
http://docs.hp.com/en/B2355-90680/printf.1.html

And the HP version of printf doesn't seem to allow you to specify a character to be specified in hex, only in octal - So something tortuous like this would be needed:
$ HEX=61
$ printf "\\"$(printf "%o\n" 0x$HEX)
(Use printf to convert $HEX to octal and then print the character corresponding to the octal number)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 17164915
Now there's obviously some interesting differences in printf versions and whether it's using a builtin like ksh/bash or the printf binary.

On Solaris 9 at least the bash and ksh builtin versions of printf give the same results as /usr/bin/printf

tfewster, what OS/shell are you using that produces 'a' with

printf "%x" a

On Solaris 9, I get

printf: a expected numeric value

myrizvi.

I've tried

printf '\x61'

on Solaris under ksh/bash and the printf binary.  I've tried under FreeBSD and Redhat Linux, and the printf builtins work and on Redhat the /usr/bin/printf works, but on FreeBSD /usr/bin/printf produces

x61

Are you using FreeBSD or similar?

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17165624
>  Now there's obviously some interesting differences in ..
.. HP-UX
I guess it's worth looking in /usr/dt/bin where HP-UX stores the well known working standard executables ;-)
0
 
LVL 20

Expert Comment

by:tfewster
ID: 17166227
Curioser and curioser...It's not working for me on Solaris and printf doesn't appear to be built in to ksh on either HP-UX or Solaris...
(See my previous post for HP version details)

# uname -a
SunOS SUNMCC 5.9 Generic_112233-11 sun4u sparc SUNW,Sun-Blade-1000
# printf '\x61'
\x61
# which printf
/usr/bin/printf
export PATH=""
# printf '\x61'
ksh: printf:  not found
# echo $SHELL
/bin/ksh





0
 
LVL 48

Expert Comment

by:Tintin
ID: 17166503
I was wrong in my initial statement.  On Solaris only bash printf works as it is a shell builtin.  ksh doesn't have a printf builtin and the printf binary gives the undesired result.

# uname -sr
SunOS 5.9
# echo $0
bash
# type printf
printf is a shell builtin
# printf '\x61'
a# /usr/bin/printf '\x61'
\x61# exec ksh
# type printf
printf is /usr/bin/printf
0
 

Author Comment

by:myrizvi
ID: 17166840
Thanks to all of you.

Right tfewster's solution is working for me, so I will accept this answer.

The unix is HP-UX 11.11 and ksh is /bin/ksh.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
string initialization in java 11 110
matchUp  challenge 9 94
Trying to run powershell  in a batch file. How do I do this? 8 68
x-bar in Google Sheets 2 27
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now