Command to convert Hex to decimal in Linux? [duplicate] - linux

This question already has answers here:
Hexadecimal To Decimal in Shell Script
(8 answers)
Closed 2 years ago.
I'm trying to convert the hexadecimal value to a decimal value in my map file. For example, here is a snippet of the file
.bss 0xc g2.comms
.bss 0xc g2.comms
.bss 0xa g2.comms
.bss 0x14 g2.comms
.rodata 0x64 g2.comms
.rodata 0x1ac g2.comms
.rodata 0x270 g2.comms
.rodata 0x18 g2.comms
Is there a one line command that can convert the 2nd column from hexadecimal to decimal?

If you have GNU awk (gawk):
$ awk -n '{print $2+0}' file
12
12
10
20
100
428
624
24
-n (or --non-decimal-data) causes "the automatic interpretation of octal and hexadecimal values in input data." In order to make sure that awk treats the second column as a number, not a string, we add zero to it: $2+0.

Related

Trying to get the average of 3 2-digit numbers NASM x86

I've seen other similar questions and I've tried the solutions mentioned but my code simply doesn't work. It gets three 2-digit numbers and adds it, divides by 3 to get the floored average, and prints it. Originally it works with the sum of numbers < 255, now I'm trying to make it work for cases > 255. With this code I'm getting Floating point exception (core dumped) in the terminal, and I'm at a loss why.
; i have successfully scanned, converted and joined the 3 numbers as n1o, n2o, n3o
; add to al, then ax
mov ax, 0
add al, [n1o]
add al, [n2o]
add ax, [n3o] ; to prevent loss of sum > 255
; will this work?
; average
mov bl, 3
div bl ; divide ax by 3
; split
mov ah, 0 ; clear remainder of average
mov bl, 10
div bl
; assign to a1
mov [a1t], al
mov [a1o], ah
; convert a1
; print a1
; terminate
EDIT: my declarations
; n1
n1t resb 1
n1o resb 1
; n2
n2t resb 1
n2o resb 1
; n3
n3t resb 1
n3o resb 1
; a1
a1t resb 1
a1o resb 1
; reflects the average
; s1
s1w resw 1
; sum of n1 n2 n3

Shell Script: Hexadecimal Loop

I am trying to learn shell script and writing a simple script to increment Hex values in the loop.
Here is my script:
increment=0x0001
handle=0x0001
for((i=1;i<=20;i++))
do
echo $handle
handle=$(($handle + $increment))
handle=$(printf '%x' $handle)
done
Here is my output:
0x0001
2
3
4
5
6
7
8
9
a
1
2
3
4
5
6
7
8
9
a
It is working fine till 10th iteration but after that it is starting from 1 again.
Can any one let me know my mistake?
EDIT: After removing handle=$(printf '%x' $handle) line output is:
0x0001
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Actually I want output in HEX only.
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

How to use hexdump to show charaters in different presentations at the same time

I am counting the number of characters of files and directory names of an entire directory.
find . -printf "%f/n" | sed 's/./&\n/g' | sort | uniq -c
Output:
234 _
162 /
341 .
342 0
156 1
217 2
99 3
...
But I need also a list of decimal, hex and normal representation at the same time of the found characters:
Example:
066 0x42 'A'
...
090 0x5A 'Z'
I have tried hexdump with different format options but this didn't work for me.
Can it be done with hexdump ?
This is my implementation after your answer:
find . -printf "%f\n" | sed "s/./&\n/g" | sort | uniq | tr -d '\n' | hexdump -v -e '/1 "%03d "' -e '/1 "0x%02X "' -e '/1 "<%c>\n"'
Output:
060 0x3C <<>
062 0x3E <>>
058 0x3A <:>
063 0x3F <?>
046 0x2E <.>
097 0x61 <a>
098 0x62 <b>
099 0x63 <c>
100 0x64 <d>
101 0x65 <e>
069 0x45 <E>
102 0x66 <f>
104 0x68 <h>
072 0x48 <H>
105 0x69 <i>
108 0x6C <l>
109 0x6D <m>
110 0x6E <n>
078 0x4E <N>
111 0x6F <o>
195 0xC3 <�>
182 0xB6 <�>
...
Can it be done with hexdump?
Yes:
find . -printf "%f/n" |
hexdump -v -e '/1 "%03d "' -e '/1 "0x%02X "' -e '/1 "%c\n"'
See examples at the end of the hexdump man page.
An alternative to hexdump/hd is xxd. xxd can covert to hex and back. But I warn you with 32bit data and XOR I noticed an error I was getting 46 instead of 41. Also theres an option for uppercase and lowercase hex and out the default is BIG ENDIAN but theres a LITTLE ENDIAN ooption and remember the output is a char string and not the HEX equivalent.
I think the hexdump -C command is what you are looking for see man hexdump or man xxd.
PS> #Dummy00001 what does hexdumps "/1" do? I get the same result if I omit it
#ebart why is there a 'g' character in your sed script replacment operation replaces each char with \n at the end of line(&): "s/./&\n/g"
From link below:
The X/X format is called a count specifier, so 2/4 means 2 instances of a 4 byte (32 bit) value. The %08x format means output the value as an 8 digit hexadecimal number with leading zeros.
This command is not the answer to your question but shows how hexdump formatting is done:
hexdump -s 446 -n 64 -v -e '1/1 "%02x" 3/1 " %3d" 1/1 " %02x" 3/1 " %3d" 2/4 "

Trying to Understand the size of a binary file (NASM) output

I have these two files myboot.asm and theirboot.asm (listed respectively):
;----------------------------------------------------------------------
; A Simple boot program that prints the string 'Hello World'
; Author: Matthew Hoggan 2012
;----------------------------------------------------------------------
Output db 'Hello',0x00 ; Output string for bios
org 0x7c00 ; This is where BIOS loads the bootloader
entry: ; Label to Signify entry to program
jmp short begin ; Jump over the DOS boot record data
; --------------------------------------------
; Boot program code begins here
; --------------------------------------------
begin: ; Label to Signify entry to program
mov si, Output ; Get pointer to string
loop: ; Top of loop to iterate over string
mov al, [si] ; Move contents of pointer to al
or al, al ; Check if character pointed to is Zero
jz hang ; Zero signifies end of string
call print_char ; Print current char in al
jmp loop ; Repeat
; --------------------------------------------
; Function to print char
; assume caller has placed char in al
; --------------------------------------------
print_char:
mov ah, 0x0e ; Function to print a character to the screen
mov bl, 7 ; color/style to use for the character
int 0x10 ; print the character
hang:
jmp hang ; just loop forever.
;---------------------------------------------
; Write Zeros up to end of program - 2 then boot signature
;---------------------------------------------
size equ $ - entry
times (512 - size - 2) db 0
db 0x55, 0xAA ;2 byte boot signature
;----------------------------------------------------------------------
; A Simple boot program that prints the string 'Hello World'
;----------------------------------------------------------------------
org 0x7c00 ; This is where BIOS loads the bootloader
entry: ; Label to Signify entry to program
jmp short begin ; Jump over the DOS boot record data
; --------------------------------------------
; Boot program code begins here
; --------------------------------------------
begin: ; Label to Signify entry to program
xor ax, ax ; Zero out ax
mov ds, ax ; Set data segment to base of RAM
mov si, msg ; Get pointer to string
call putstr ; Print the message
jmp hang ; Go to infinite loop
msg db 'Hello, World',0x00
putstr: ; Function to print the string
lodsb ; al = [DS:SI]
or al, al ; Set zero flag if al = 0
jz ret ; Jump to end of function if al = 0
mov ah, 0x0e ; Video function 0Eh (print char)
mov bx, 0x0007 ; Color
int 0x10
jmp putstr
ret:
retn
hang:
jmp hang ; just loop forever.
;---------------------------------------------
; Write Zeros up to end of program - 2 then boot signature
;---------------------------------------------
size equ $ - entry
times (512 - size - 2) db 0
db 0x55, 0xAA ;2 byte boot signature
Building both of these files running hexdump on them and listing the files in the directory to see their size reveals:
mehoggan#mehoggan-laptop:~/Code/play/asm$ nasm myboot.asm -f bin -o boot.bin && hexdump boot.bin && ls -l && echo "------" && nasm bootloader1.asm -f bin -o boot.bin && hexdump boot.bin && ls -l
0000000 6548 6c6c 006f 00eb 00be 8a7c 0804 74c0
0000010 e80c 0003 f4e9 b4ff b30e cd07 e910 fffd
0000020 0000 0000 0000 0000 0000 0000 0000 0000
*
0000200 0000 0000 aa55
0000206
total 20
-rw-r--r-- 1 mehoggan mehoggan 518 2012-02-29 21:57 boot.bin
-rw-r--r-- 1 mehoggan mehoggan 2290 2012-02-29 20:23 bootloader0.asm
-rw-r--r-- 1 mehoggan mehoggan 1661 2012-02-29 21:55 bootloader1.asm
-rw-r--r-- 1 mehoggan mehoggan 1786 2012-02-29 21:49 myboot.asm
-rw-r--r-- 1 mehoggan mehoggan 1065 2012-02-29 20:14 ourbootloader.asm
------
0000000 00eb c031 d88e 0fbe e87c 0010 1de9 4800
0000010 6c65 6f6c 202c 6f57 6c72 0064 08ac 74c0
0000020 b40a bb0e 0007 10cd f1e9 c3ff fde9 00ff
0000030 0000 0000 0000 0000 0000 0000 0000 0000
*
00001f0 0000 0000 0000 0000 0000 0000 0000 aa55
0000200
total 20
-rw-r--r-- 1 mehoggan mehoggan 512 2012-02-29 21:57 boot.bin
-rw-r--r-- 1 mehoggan mehoggan 2290 2012-02-29 20:23 bootloader0.asm
-rw-r--r-- 1 mehoggan mehoggan 1661 2012-02-29 21:55 bootloader1.asm
-rw-r--r-- 1 mehoggan mehoggan 1786 2012-02-29 21:49 myboot.asm
-rw-r--r-- 1 mehoggan mehoggan 1065 2012-02-29 20:14 ourbootloader.asm
Why are the files sizes off by 6 bytes?
Check out the last little block of assembly code there:
size equ $ - entry
times (512 - size - 2) db 0
db 0x55, 0xAA ;2 byte boot signature
This block of code calculates how big the code is (from entry to the current location), then pads it out to a total of 512 bytes with zeroes and a signature 0x55 0xAA in the last two positions. That is:
entry: Some code
.
.
.
Some zeroes
.
.
.
0x55 0xAA
That little assembly block means the output size from the entry label to 0x55 0xAA is always 512 bytes. In your first example, there is a six byte string Hello\0 before entry. In your second example there's not. Therefore, the first program is six bytes longer than the second. You probably want to move that string to someplace after entry and before the padding block.
If you use hexump -C on your binaries, you'll see the string right up at the top of the first binary.

What's a Hex Dump - What does it mean?

thegladiator:~/cp$ cat new.txt
Hello World This is a Trest Progyy
thegladiator:~/cp$ hexdump new.txt
0000000 6548 6c6c 206f 6f57 6c72 2064 6854 7369
0000010 6920 2073 2061 7254 7365 2074 7250 676f
0000020 7979 000a
0000023
How is that text data represented in hex like that? What is the meaning of this?
it's just what it says, a dump of the data in hexidecimal format:
H 48
e 65
l 6c
l 6c
o 6f
It is odd though that all of the bytes are swapped (65 48 : e H)
If you're on a *nix system, you can use 'od -x', or 'man od' will tell you all the ways to get data from od :)
The text in the file new.txt is stored using ASCII encoding. Each letter is represented by a number, decimal: 32-127 hexidecimal: 20-7F. So the first three letters (H,e,l), are represented by the decimal numbers: 72,101,108 and the hexidecimal numbers: 48,65,6C
Hexdump by default takes each 16 bit word of the input file new.txt and outputs this word as a Hexidecimal number. Because it is operating on 16 bits, not 8 bits, you see the output in an unexpected order.
If you instead use xxd new.txt, you will see the output in the expected order.

Resources