Solved

How do I convert an integer to hex and assign to a string in bash

Posted on 2014-04-01
2
1,144 Views
Last Modified: 2014-04-01
Hi all,

I am developing a bash script and I need to convert an integer number into a fixed format hex string.

for example, 255 would need to be stored as "00FF" and assigned to a variable

How do I do this (I am new to bash!)

Thanks,

James
0
Comment
Question by:jatkin
2 Comments
 
LVL 5

Accepted Solution

by:
Dave Gould earned 500 total points
ID: 39968900
> hexnum=`printf "%04x\n" 255`
> echo $hexnum
   00ff

The above example assigns the hex value for 255 to the variable "hexnum" with a fixed integer length of 4.
255 can be replaces with a variable that would make it more interesting for a script.

ie
>  i=254
> hexnum=`printf "%04x\n" $i`
> echo $hexnum
00fe
0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 39968934
Perfect, thanks a lot :-)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

21 Experts available now in Live!

Get 1:1 Help Now