Tuesday, April 10, 2012

increment in hex

increment in hex:


 
LinkBack Search this Thread 

Old 11-01-2005, 04:33 PM  #1
snutz411
Member

Registered: Mar 2004
Distribution: Slackware 10.0
Posts: 55

Rep: Reputation: 15
increment in hex


[Log in to get rid of this advertisement]
is there a way i can write a shell script that will be able to increment in hex?

i.e. i was to get all the values from 000 to FFF
 
Old 11-01-2005, 04:47 PM  #2
Dark_Helmet
Senior Member

Registered: Jan 2003
Distribution: Debian 6.0.3, Ubuntu Server 10.04
Posts: 2,786

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Incrementing by one in hex is the same as incrementing by one in any other number base 

My suggestion: Do the increment like normal, and manually convert to hex format when you need to display or compare it to a hex value.

For instance:
Code:
#!/bin/bash

for number in $( seq 1 255 )
do
  hex_representation=$( printf "%X" ${number} )
  echo "${number}: ${hex_representation}"
done
If you're script is given the value of the number to increment in hex notation, you can also use printf to convert it to decimal for you:
Code:
number=$( printf "%d" 0x${hex_representation}" )
I'm not claiming that any of this is efficient by any means. Nor am I saying it will solve all your problems  But it should give you a start.
 
Old 11-01-2005, 05:54 PM  #3
solveit
Member

Registered: Jan 2005
Posts: 83

Rep: Reputation: 15
Try this :

Code:
i=0

while [ $i  -lt  4096 ]
do
   x=`printf "%04X\n"  $i`
   echo $x
   i=`expr $i + 1`
done
 


'via Blog this'

No comments:

Post a Comment