Shell Script: Hexadecimal Loop

Shell Script: Hexadecimal Loop

It has to do with how you print the value try printf '%#x' or printf '%#X'

Just change the line you are using to print the content with a leading 0x as:-

handle=$(printf '%#x' $handle) 

(or) to have leading hex-character as 0X

handle=$(printf '%#X' $handle) 

With the changes, you get the output as below:-

$ ./script.sh 
0x0001
0x2
0x3
0x4
0x5
0x6
0x7
0x8
0x9
0xa
0xb
0xc
0xd
0xe
0xf
0x10
0x11
0x12
0x13
0x14
0x15
0x16
0x17
0x18
0x19
0x1a
0x1b
0x1c
0x1d
0x1e
0x1f
0x20

For more formatting options check here:- http://wiki.bash-hackers.org/commands/builtin/printf (and) http://ss64.com/bash/printf.html

Shell script loop, increment by hex

(I'll assume you are using bash).

In general, bash only allows you to enter numeric literals in base 10. You can use other bases in arithmetic contexts (inside ((...)), for example), but it will be displayed in its base-10 representation. You can use the printf built-in to convert quickly to a hexadecimal representation, however.

for (( num=0xe0000000; num <= 0xe0001000; num+=4 )); do
peek -lt $( printf "%x" $num ) >> /luascript/dumpfileA
done

While loop in bash script for echoing out hexadecimal numbers

Look at printf builtin bash command, and change your echo with

  printf "mac=00:00:01:%2.2x:%2.2x:%2.2x\n" $i $j $k

Converting decimal to hexadecimal in bash script

You can use printf.

$ printf "0x%04x" 100
0x0064

In your example you'd probably use a="$(printf '0x%04x' $i)".

check if a number is hex in shell script without specifying number of characters limit [ bash ]

I won't do it with regex.

HEX="301DE8"
(( 16#$HEX ))

then you check the return code of the last statement by $?.

If it is 0, your HEX is ok, otherwise, it is not HEX number.

How to convert Hexadecimal to Decimal in bash/scripting by only using functions

I was always told you convert a number from one base to another by converting single digit from the back in that number and then multiply that number by the base and position it is in.

First I wrote a function to convert a single digit from hex to dec. Then I convert the number from the most significant digit (from the one to the left) to dec, then multiply by 16, then add the next digit, then mutliply by 16 and so on.

hextodec1() {
case "$1" in
[0-9]) echo "$1"; ;;
a|A) echo 10; ;;
b|B) echo 11; ;;
c|C) echo 12; ;;
d|D) echo 13; ;;
e|E) echo 14; ;;
f|F) echo 15; ;;
esac
}

out=0
for ((i = ${#num} - 1; i >= 0; --i)); do
((out = out * 16 + $(hextodec1 ${num:$i:1}) ))
done
echo $out

You can extract a substring of string by using ${variable_name:$start_position:$length}.

But really the simplest way is to do this:

echo $((0x$num))

You algorithm needs some fixing because bash understands decimal numbers by default, or hexadecimal numbers if pretended with 0x. So you loop for (( x=0; num>0; x++ )); do will not work for num=6C3 cause num is a string, and bash can't compare number x to string 6C3. You can do for ((x=0;(0x$num)>0;++x)); do but then you might better just echo $((0x$num)), cause the first time you will num=$(( (0x$num) % 2 )) it will be converted to decimal and the next time bash enters the loop condition the variable will be in decimal not hex.



Related Topics



Leave a reply



Submit