Convert hexadecimal value to decimal value


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Convert hexadecimal value to decimal value
# 1  
Old 10-22-2015
Convert hexadecimal value to decimal value

Hi All,

Code:
cat filename | awk '{print $1, $2, $4, $5, $6, $7, $8, $9, $10;}' |  awk 'NF > 0'

Code:
OUTPUT:
2015-01-19 00:12:32 00000000fbfa0000 000000009ae5cf80 014d 015d 0017 003c 0362de20
2015-01-19 00:13:52 00000000fc820000 00000000994c6758 014c 015d 000b 003c 08670250
2015-01-19 00:14:25 00000000fc550000 000000009247d3c0 014c 015d 000b 003c 00000c04
2015-01-19 00:15:25 00000000fce70000 000000009a9db6f0 014c 015d 000e 003c 00000424
2015-01-19 00:16:25 00000000fcb30000 0000000099504220 014c 015d 001d 003c 00000000

the coloumns 3,4,5,6,7,8,9 are hexadecimal values, now I want to convert the hexadecimal values into decimal format in one line code or any better way

Quote:
Reference: below is the sample syntax to convert from hexadecimal format to decimal format which is supportable on my OS
Code:
hexNum=2f
echo $((0x${hexNum}))
output
47

my os details: SunOS 5.10 Generic_148888-05 sun4u spare SUNW
Moderator's Comments:
Mod Comment Please put sample input and sample output in CODE tags as well as sample code segments.

Last edited by sam@sam; 10-22-2015 at 03:57 AM.. Reason: Put sample output in CODE tags.
# 2  
Old 10-22-2015
Perhaps something like:
Code:
/usr/xpg4/bin/awk 'NF{printf("%s %s %u %u %u %u %u %u %u\n",$1,$2,"0x"$4,"0x"$5,"0x"$6,"0x"$7,"0x"$8,"0x"$9,"0x"$10)}' hexconv

Making a wild guess (since you didn't show us your actual sample input) and assuming the file hexconv contains something like:
Code:
2015-01-19 00:12:32 junk 00000000fbfa0000 000000009ae5cf80 014d 015d 0017 003c 0362de20 junk

2015-01-19 00:13:52 junk 00000000fc820000 00000000994c6758 014c 015d 000b 003c 08670250 junk

2015-01-19 00:14:25 junk 00000000fc550000 000000009247d3c0 014c 015d 000b 003c 00000c04 junk

2015-01-19 00:15:25 junk 00000000fce70000 000000009a9db6f0 014c 015d 000e 003c 00000424 junk

2015-01-19 00:16:25 junk 00000000fcb30000 0000000099504220 014c 015d 001d 003c 00000000 junk

it produces the output:
Code:
2015-01-19 00:12:32 4227465216 2598752128 333 349 23 60 56811040
2015-01-19 00:13:52 4236378112 2571921240 332 349 11 60 140968528
2015-01-19 00:14:25 4233428992 2454180800 332 349 11 60 3076
2015-01-19 00:15:25 4242997248 2594027248 332 349 14 60 1060
2015-01-19 00:16:25 4239589376 2572173856 332 349 29 60 0

which I'm guessing is the output you wanted (since you didn't show us any sample output either).

Last edited by Don Cragun; 10-22-2015 at 03:57 AM.. Reason: Change %d format specifiers to %u.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-22-2015
Yes, Don your assumption are right and the output is producing correct results...let me check this cmd on my sys

---------- Post updated at 04:31 PM ---------- Previous update was at 04:25 PM ----------

Input file:
Code:
more hexconv 
2015-01-19 00:12:32 MET 00000000fbfa0000 000000009ae5cf80 014d 015d 0017 003c 0362de20 0001 02b3 00000ecb 00000077 0000007c

2015-01-19 00:13:52 MET 00000000fc820000 00000000994c6758 014c 015d 000b 003c 08670250 0000 02b3 00000342 00000065 00000066

2015-01-19 00:14:25 MET 00000000fc550000 000000009247d3c0 014c 015d 000b 003c 00000c04 0000 02b3 00000032 0000099d 0000099d

2015-01-19 00:15:25 MET 00000000fce70000 000000009a9db6f0 014c 015d 000e 003c 00000424 0000 02b3 000002de 0000007c 0000007c

2015-01-19 00:16:25 MET 00000000fcb30000 0000000099504220 014c 015d 001d 003c 00000000 0000 02b3 0000028d 0000006a 0000006a

Code:
which awk
Missing ]
/usr/bin/awk

below is output of commands you given to me but its not generating expected results:
Code:
/usr/bin/awk 'NF{printf("%s %s %u %u %u %u %u %u %u\n",$1,$2,"0x"$4,"0x"$5,"0x"$6,"0x"$7,"0x"$8,"0x"$9,"0x"$10)}' hexconv

Code:
OUTPUT
awk: syntax error near line 1
awk: bailing out near line 1

Code:
/usr/xpg4/bin/awk 'NF{printf("%s %s %u %u %u %u %u %u %u\n",$1,$2,"0x"$4,"0x"$5,"0x"$6,"0x"$7,"0x"$8,"0x"$9,"0x"$10)}' hexconv

Code:
OUTPUT
2015-01-19 00:12:32 0 0 0 0 0 0 0
2015-01-19 00:13:52 0 0 0 0 0 0 0
2015-01-19 00:14:25 0 0 0 0 0 0 0
2015-01-19 00:15:25 0 0 0 0 0 0 0
2015-01-19 00:16:25 0 0 0 0 0 0 0

The sample output should convert hexavalues to decimals

---------- Post updated at 04:35 PM ---------- Previous update was at 04:31 PM ----------

I have tried using below cmd but not getting full results:

Code:
/usr/bin/awk '{printf("%s %s %u %u %u %u %u %u %u\n",$1,$2,"0x"$4,"0x"$5,"0x"$6,"0x"$7,"0x"$8,"0x"$9,"0x"$10)}' hexconv

Code:
OUTPUT
2015-01-19 00:12:32 %uu %uu %uu %uu %uu %uu %uu
  %uu %uu %uu %uu %uu %uu %uu
2015-01-19 00:13:52 %uu %uu %uu %uu %uu %uu %uu
  %uu %uu %uu %uu %uu %uu %uu
2015-01-19 00:14:25 %uu %uu %uu %uu %uu %uu %uu
  %uu %uu %uu %uu %uu %uu %uu
2015-01-19 00:15:25 %uu %uu %uu %uu %uu %uu %uu
  %uu %uu %uu %uu %uu %uu %uu
2015-01-19 00:16:25 %uu %uu %uu %uu %uu %uu %uu

# 4  
Old 10-22-2015
Would using %.f as a format string help?
# 5  
Old 10-22-2015
I'm sorry you're having problems. With that input, I get the output:
Code:
2015-01-19 00:12:32 4227465216 2598752128 333 349 23 60 56811040
2015-01-19 00:13:52 4236378112 2571921240 332 349 11 60 140968528
2015-01-19 00:14:25 4233428992 2454180800 332 349 11 60 3076
2015-01-19 00:15:25 4242997248 2594027248 332 349 14 60 1060
2015-01-19 00:16:25 4239589376 2572173856 332 349 29 60 0

with awk on OS X. It looks like /usr/xpg4/bin/awk is stopping at the x instead of treating 0x as an introducer to a hex number string. It should work on Solaris 10 with either /usr/xpg4/bin/awk or /usr/bin/nawk. Try it with nawk and see if that works better.

If nawk doesn't work with the above code either, try both nawk and /usr/xpg4/bin/awk with the code:
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}'

hoping that at least one of those forms will print 15 instead of 0 with one or both of those versions of awk.
# 6  
Old 10-22-2015
Quote:
Originally Posted by RudiC
Would using %.f as a format string help?
I'm assuming that the problem is the conversion of the string 0x concatenated with a hex-digit string into a number stopping at the x rather than there being a problem with a %d, %u, or %f format specifier, but this is certainly worth a try. Just for the fun of it, we should also try %d as well as %.f instead of %u.
# 7  
Old 10-22-2015
I did on linux and FreeBSD. On both, %u failed, outputting 2147483648 which is INT_MAX + 1 or the negative INT_MIN. While %d and %.f worked on FreeBSD, only the latter worked on linux.

Last edited by RudiC; 10-22-2015 at 10:51 AM..
 
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