Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print hex value of a random number 1

Status
Not open for further replies.

userMikeD

Programmer
Nov 5, 2008
28
US
I can print the hex value of a given number using printf
Code:
printf "%02x\n" 2 -> 02
printf "%02x\n" 20 -> 14
printf "%02x\n" 200 -> c8

I need to print the hex value of a random number, 0-127, but can't seem to get that working. I tried this:

printf "%02x\n" ${RANDOM}%128

What do I need to do differently?

Thanks,
Mike
 
Hi

Arithmetic evaluation is not one of the main tasks of a shell. If you need such thing, you have to ask for it explicitly :
Code:
[COLOR=chocolate]printf[/color] [green][i]"%02x[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green] [highlight][teal]$(([/teal][/highlight] [blue]RANDOM[/blue][teal]%[/teal][purple]128[/purple] [highlight][teal]))[/teal][/highlight]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top