• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2104
  • Last Modified:

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

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!)


James Atkin
James Atkin
1 Solution
Dave GouldOnsite SupportCommented:
> hexnum=`printf "%04x\n" 255`
> echo $hexnum

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.

>  i=254
> hexnum=`printf "%04x\n" $i`
> echo $hexnum
James AtkinSenior Principle Software EngineerAuthor Commented:
Perfect, thanks a lot :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now