Solved

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

Posted on 2014-04-01
2
1,197 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

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

910 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

23 Experts available now in Live!

Get 1:1 Help Now