Howto Add Hex Numbers via Command Line

Recently I needed to calculate the starting and ending memory addresses of an IOS application (in order to dump it). As a result i needed to figure out how to add two hex addresses. Here’s what I discovered.

Basic Example

In this case we are adding 0x4f000 and 0×2000

$ printf "0x%X\n" $(( 0x4f000+0x2000 ))
0x51000

Scripted Example (all hex numbers as input)

Source

#!/bin/bash
#
# Name; hexample.sh
# Author: Steve Stonebraker
# Date: 8-1-2013
# Description: Example of how to add hex values in bash
#

base_address=0x4f000
cryptoff=0x2000
cryptsize=0x2e8000

starting_address=$(( base_address+cryptoff ))
ending_address=$(( base_address+cryptoff+cryptsize ))

printf "\n%s: 0x%X\n%s: 0x%X\n" "starting address" ${starting_address} "ending address" ${ending_address} &&
printf "\n%s:\n%s 0x%X 0x%X\n\n" "Run the following instruction on gdb" "dump memory decrypted2.bin" ${starting_address} ${ending_address}

Output

$ ./hexample.sh

starting address: 0x51000
ending address: 0x339000

Run the following instruction on gdb:
dump memory decrypted2.bin 0x51000 0x339000

Scripted Example (Mixed decimal and hex numbers as input)

Source

#!/bin/bash
#
# Name; hexample.sh
# Date: 8-1-2013
# Description: Example of how to add hex values in bash
#
#

base_address=0x4f000
cryptoff=8192
cryptsize=3047424

starting_address=$(( base_address+cryptoff ))
ending_address=$(( base_address+cryptoff+cryptsize ))

printf "\n%s: 0x%X\n%s: 0x%X\n" "starting address" ${starting_address} "ending address" ${ending_address} &&
printf "\n%s:\n%s 0x%X 0x%X\n\n" "Run the following instruction on gdb" "dump memory decrypted2.bin" ${starting_address} ${ending_address}

Output

$ ./hexample.sh

starting address: 0x51000
ending address: 0x339000

Run the following instruction on gdb:
dump memory decrypted2.bin 0x51000 0x339000

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>