Convert hexadecimal value to decimal value


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Convert hexadecimal value to decimal value
# 8  
Old 10-22-2015
On Solaris 10:
Code:
 nawk -v f=f 'BEGIN{printf("%u %u %u %u %u\n", "0x"f, 0+("0x" f),"0x" f) + 0, "0x"f+0, 0+"0x"f);exit}'
0 0 0 0 0

So nawk does do what is wanted.
# 9  
Old 10-22-2015
Quote:
Originally Posted by jim mcnamara
On Solaris 10:
Code:
 nawk -v f=f 'BEGIN{printf("%u %u %u %u %u\n", "0x"f, 0+("0x" f),"0x" f) + 0, "0x"f+0, 0+"0x"f);exit}'
0 0 0 0 0

So nawk does do what is wanted.
No, what is wanted is 15. We want 0+"0xf" to be treated as a number with hexadecimal value 0xf that is to be printed as the unsigned decimal number 15.
# 10  
Old 10-23-2015
Thanks - my bad.

This, however does produce correct output.
Code:
$ printf "%d\n" 0x0362de20  "0x"0362de20
56811040
56811040

1. this emulates your approach - prepending "0x" to a hex number
2. it does produce correct output, I believe.
3. So, is it feasible to consider printf
sed or awk to pipe (or process substitution) prepended data to printf ?
data to printf?
Is there some reason we must only use awk to output
# 11  
Old 10-23-2015
The submitter's original script used cat once and awk twice before doing the conversion from hex to decimal. I cut that back to just one awk, but clearly there is no need for awk here either. We can do it all with shell built-ins...
Code:
/usr/xpg4/bin/sh
while read f1 f2 junk f4 f5 f6 f7 f8 f9 f10 junk
do	[ "$f1" = "" ] && continue
	printf '%s %s %u %u %u %u %u %u %u\n' \
	    "$f1" "$f2" "0x$f4" "0x$f5" "0x$f6" "0x$f7" "0x$f8" "0x$f9" "0x$f10"
done < hexconv

In theory, this script should work with /bin/sh, /bin/ksh, /bin/bash, and /usr/xpg4/bin/sh, but I don't know if the printf built-in in all of those shells will recognize hexadecimal string values (and Jim didn't say which shell he was using for the results he showed us in post #10 in this thread).
# 12  
Old 10-25-2015
Hello Don, Finally I could get the results now

Last edited by sam@sam; 10-25-2015 at 06:48 PM..
# 13  
Old 10-25-2015
Let's compare your code (reformatted to be more easily seen on a normal screen):
Code:
cat hexconv |
awk 'NF > 0' |
while read a b c d e f g h i j k l m n o
do     printf "%s %s %d %d %d %d %d %d %d\n" \
	    "$a" "$b" "0x$d" "0x$e" "0x$f" "0x$g" "0x$h" "0x$i" "0x$j"
done

and the code I suggested:
Code:
while read f1 f2 junk f4 f5 f6 f7 f8 f9 f10 junk
do	[ "$f1" = "" ] && continue
	printf '%s %s %u %u %u %u %u %u %u\n' \
	    "$f1" "$f2" "0x$f4" "0x$f5" "0x$f6" "0x$f7" "0x$f8" "0x$f9" "0x$f10"
done < hexconv

Your code requires that a shell be exec'ed to run your script, that cat be exec'ed to read your input file, and that awk be exec'ed to discard empty lines found in your input file.

My code requires that a shell be exec'ed to run my script. Note that the exec operation is a relatively EXTREMELY slow operation on UNIX and Linux systems.

Your script has cat read the entire contents of your input file, write the entire contents of your input file, has awk read the entire contents of your input file and write all non-blank lines in your input file, and has the shell read all non-blank line in your input file and write parts of those non-empty lines in a different format.

My script reads your input file once and writes parts of non-empty lines in a different format.

Your script reads 15 fields (named a through o) and never uses $c, $k, $l, $m, $n, or $o.

My script reads the same data, but uses the name junk for data that will not be used by the script. It uses ffield# as the names of the fields it does use. (If headings had been supplied on the sample input, I would have used variables names related to the field names.)

Your script uses %d as the format specifier when printing the converted hexadecimal values. My uses %u. By definition, hexadecimal values are unsigned. But for the sample data provided, the output will be identical. If some of your input data contains a sixteen hexadecimal digit value with the highest order hexadecimal digit being 8 or greater, your script will print it as a negative decimal value while my script will print it as a positive decimal value.

You seem to believe that:
Code:
cat hexconv |  awk 'NF > 0' |

is needed to get rid of the empty lines in your input file. I believe that:
Code:
[ "$f1" = "" ] && continue

is a much simpler and much more efficient way to ignore empty lines in your input file and it still would be if you used the much more efficient:
Code:
awk 'NF > 0' hexconv |

without the cat.

Unfortunately, now that I've tried to answer your original question:
Quote:
Hello Don, Finally I could get the results but not satisfied, looking to reduce the complexity in code:
about the complexity of your code, you have changed the question to:
Quote:
Hello Don, Finally I could get the results now looking to subtract coloumn3 with coloumn 4 in code:
and I don't know what that means. How do you subtract MET with a hexadecimal value like 00000000fbfa0000? And, if you did subtract field 3 from field 4 or subtract field 4 from field 3, what different output are you hoping to produce as a result? Does the difference replace field 3 or field 4, or is another output field supposed to be created?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

2. UNIX for Dummies Questions & Answers

Convert hexa decimal to decimal

Hi, I want to convert two hexadecimal numbers to decimal using unix command line. 1cce446295197a9d6352f9f223a9b698 fc8f99ac06e88c4faf669cf366f60d I tried using `echo "ibase=16; $no |bc` printf '%x\n' "1cce446295197a9d6352f9f223a9b698" but it doesn't work for such big number it... (4 Replies)
Discussion started by: sudhakar T
4 Replies

3. Shell Programming and Scripting

Convert hexadecimal value in decimal value

hi all, this is my script: #! /bin/sh minutes=$( { i2cget -f -y 0 0x51 3; } 2>&1 ) minutes=${minutes:2} hour=$( { i2cget -f -y 0 0x51 4; } 2>&1 ) hour=${hour:2} day=$( { i2cget -f -y 0 0x51 5; } 2>&1 ) day=${day:2} month=$( { i2cget -f -y 0 0x51 7; } 2>&1 ) month=${month:2} ... (6 Replies)
Discussion started by: enaud
6 Replies

4. Shell Programming and Scripting

NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it

I have searched and the answers I have found thus far have led me to this point, so I feel I am just about there. I am trying to convert a column of hexadecimal to decimal values so that I can filter out via grep just the data I want. I was able to pull my original 3 character hex value and... (10 Replies)
Discussion started by: PCGameGuy
10 Replies

5. Shell Programming and Scripting

To convert file with decimal to another file with Hexadecimal

I have a text file of alphanumeric values listed one by one. I have to convert them to hexadecimal equivalents for each character seperated by ":" in Unix bash shell script. For example, 12345678 has to be converted to 31:32:33:34:35:36:37:38 (10 Replies)
Discussion started by: mathie
10 Replies

6. Shell Programming and Scripting

Convert exponential value to decimal

Hi, Here is my script to read a file into array: awk -F '+' ' # load first file into array indexed by fields 1 and 2 NR == FNR { file1nr = FNR for (i=3; i<NF; i++) { file1 = $i } I have this... (5 Replies)
Discussion started by: Sangtha
5 Replies

7. UNIX for Dummies Questions & Answers

Hexadecimal to Decimal

Hi all, I have a small script to convert my HexaDecimal Input to Decimal as output. #!/bin/ksh hd=00208060 dec=`printf %d $hd` echo $dec Output of the above program: printf: 00208060 not completely converted 16 But my expected output is "2130016". How can i acheive this. I... (2 Replies)
Discussion started by: Arunprasad
2 Replies

8. Shell Programming and Scripting

need script to convert number in hexadecimal

hi , i need a script to convert number into hexadecimal base for example: 237=>ED it s very important for me thank you in advance for you help (5 Replies)
Discussion started by: mips
5 Replies

9. Shell Programming and Scripting

Decimal to Hexadecimal conversion

Hi frnds :) I need a small help... I have a very long file containing 20 digits decimal number which i want to convert into the corresponding 16 digit hexadecimal values. File looks like.... 11908486672755551741 05446378739602232559 04862605079740156652 . . . I tried the script for i... (7 Replies)
Discussion started by: vanand420
7 Replies

10. UNIX for Dummies Questions & Answers

Convert hexadecimal to decimal base

Hello ! Does anyone knows how can I convert hexadecimal to decimal base in the ksh or csh script ?? Thanks ! Witt (1 Reply)
Discussion started by: witt
1 Replies
Login or Register to Ask a Question