How to convert hex numbers to decimal ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert hex numbers to decimal ?
# 1  
Old 03-12-2009
How to convert hex numbers to decimal ?

Hi,

please tell me how to convert hex number to decimal

000000E7
000000000002640D
0000000000025B16

and seconds to minutes, hours, days, months, years
bytes to kbytes, mbytes , gbytes

read the following examples
while read a b
do
printf "%5d %5d\n" "0x$a" "0x$b"
done < "$FILE"
#!/bin/ksh
# this does the math
echo "c1:41 c2:0x0000.00046b3e" | awk -F[:\.] '{ print $3, $4}' | read one two
result=$( printf "%d + %d" $one 0x"$two")
echo $result
result=$( printf "%d + %d" 0x0x000 0x00046b3e ) # check
echo $result
printf "%d\n" "0x00046b3"
but I would like to learn syntax either
as in my case, the last line generated -1

thanks

Jack
# 2  
Old 03-12-2009
To convert hex to decimal try using bc
Eg:- bc
ibase=16
3039
12345

and the second part of conversion of seconds to min 1 min = 60 sec so if $x sec is given $x/60 will give u min and so on

similarly for bytes use 1000 or 1024 as a standard
# 3  
Old 03-13-2009
Quote:
Originally Posted by daptal
To convert hex to decimal try using bc
Eg:- bc
ibase=16
3039
12345

and the second part of conversion of seconds to min 1 min = 60 sec so if $x sec is given $x/60 will give u min and so on

similarly for bytes use 1000 or 1024 as a standard
Thanks for your excellent solution.
Unfortunately neither my Linux embedded machine not Nokia Tablet come
with bc installable package.
So I have to look for another shell based only solution.

Great idea is to use in bc - input and out base (there is a number of good bc calculator examples I learned from Google).

For seconds conversion it would be nice to use timestamp, datetime
to read seconds as input and output result in one of available formats,
the same with bytes to Mbytes, Gbytes conversion,
as I need to write another conversion script for use in main script.

minutes = seconds (mod 60)

expr 5 % 3
2
works fine for constants

didn't work for seconds as variable
/="slash-equal" (divide variable by a constant)
%="mod-equal" (remainder of dividing variable by a constant)
It started to work for me as in the following example
# If you need a random int within a certain range, use the 'modulo' operator.
# This returns the remainder of a division operation.

RANGE=500

echo

number=$RANDOM
let "number %= $RANGE"
# ^^
echo "Random number less than $RANGE --- $number"
-
for remainder
seconds %= seconds
for minutes
minutes /= seconds
..

a=9
let "a %= 4"
echo $a
1

so a mod(4) = 1, exactly as 9 = 2*4 + 1 -

for conversion of seconds I would like to use epoch time
and date +%s
I get 95958

should be

The current Unix epoch time is 1236954365


-
-
What is epoch time?

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds. The epoch timestamp 0 can be written in ISO 8601 format as: 1970-01-01T00:00:00Z. One epoch hour is 3600 seconds, one epoch day is 86400 seconds long, leap seconds are not calculated. Many Unix systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).

Human readable time Seconds 1 minute60 seconds 1 hour3600 seconds 1 day86400 seconds 1 week604800 seconds 1 month (30.44 days) 2629743 seconds 1 year (365.24 days) 31556926 seconds

Jack
# 4  
Old 03-13-2009
Please use [ code ][ /code ] (sans the space) for code/logs/listings/...
Code:
$ TIME=$( date +%s )
$ LANG=C date -ud @`echo $TIME`
Fri Mar 13 14:37:15 UTC 2009
$ SEC=$(( ${TIME} % 60 ))
$ MIN=$(( ${TIME} % 3600 / 60 ))
$ HR=$(( ${TIME} % (3600*24) / 3600 ))
$ echo $HR $MIN $SEC
14 37 15
$
$
$ SIZE=$(( 200*1024*1024 ))
$ KB=$(( ${SIZE} / 1024 ))
$ MB=$(( ${SIZE} / 1024 / 1024 ))
$ GB=$(( ${SIZE} / 1024 / 1024 / 1024 ))
$ BITS=$(( ${SIZE} * 8 ))
$ echo $GB $MB $KB $SIZE $BITS
0 200 204800 209715200 1677721600
$

This code worked both on Linux/HP-UX using bash/ksh on both (except for the second date call, HP-UX' date doesn't like the -d switch). As for the Base-16 to Base-10 conversion, it's probably easier to do that in Perl than to code in in the shell.
# 5  
Old 03-13-2009
Quote:
Originally Posted by pludi
Please use [ code ][ /code ] (sans the space) for code/logs/listings/...
Code:
$ TIME=$( date +%s )
$ LANG=C date -ud @`echo $TIME`
Fri Mar 13 14:37:15 UTC 2009
$ SEC=$(( ${TIME} % 60 ))
$ MIN=$(( ${TIME} % 3600 / 60 ))
$ HR=$(( ${TIME} % (3600*24) / 3600 ))
$ echo $HR $MIN $SEC
14 37 15
$
$
$ SIZE=$(( 200*1024*1024 ))
$ KB=$(( ${SIZE} / 1024 ))
$ MB=$(( ${SIZE} / 1024 / 1024 ))
$ GB=$(( ${SIZE} / 1024 / 1024 / 1024 ))
$ BITS=$(( ${SIZE} * 8 ))
$ echo $GB $MB $KB $SIZE $BITS
0 200 204800 209715200 1677721600
$

This code worked both on Linux/HP-UX using bash/ksh on both (except for the second date call, HP-UX' date doesn't like the -d switch). As for the Base-16 to Base-10 conversion, it's probably easier to do that in Perl than to code in in the shell.
Hi,

in my case
running
$ time=$( date +%s )
$ lang=c dat -ud @`echo $time`
generated
date: invalid date `@103798`

Jack
# 6  
Old 03-13-2009
Quote:
Originally Posted by jack2
Hi,

in my case
running
$ time=$( date +%s )
$ lang=c dat -ud @`echo $time`
generated
date: invalid date `@103798`

Jack
what's "%s" - it's valid options under Solaris? What OS are you under? What's it supposed to return?
what's 'dat'?
Why are you doing this: '@`echo $time`'? What '@' for? Why do you need 'echo'?

What are you trying to achieve?

Last edited by vgersh99; 03-13-2009 at 02:05 PM..
# 7  
Old 03-13-2009
Quote:
Originally Posted by vgersh99
what's "%s" - it's valid options under Solaris? What OS are you under? What's it supposed to return?
what's 'dat'?
Why are you doing this: '@`echo $time`'? What '@' for? Why do you need 'echo'?

What are you trying to achieve?
Hi,

just tested code from reply in this thread.
-
-

$ TIME=$( date +%s )
$ LANG=C date -ud @`echo $TIME`
Fri Mar 13 14:37:15 UTC 2009

-
Jack
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. Shell Programming and Scripting

Converting decimal to hex

How to convert decimal value to hex and than take 1st digits as variable sample data 84844294,5,6 51291736,2,3 84844294,5,6 51291736,2,3 i can use {printf "%x,%d\n",$1,$2} but than i want to filter base on 1st hex digit 1st recrd (1 Reply)
Discussion started by: before4
1 Replies

3. Shell Programming and Scripting

How to convert a file containing hex code to decimal using script?

The file contains code like the below and need to convert each one into a decimal 00 00 00 04 17 03 06 01 So the output should come as 0 0 0 4 23 3 6 1 (24 Replies)
Discussion started by: necro98
24 Replies

4. Shell Programming and Scripting

Convert hex to decimal

can someone help me in converting hex streams to decimal values using perl script Hex value: $my_hex_stream="0c07ac14001676"; Every hex value in the above stream should be converted in to decimal and separated by comma. The output should be: 12,07,172,20,00,22,118 (2 Replies)
Discussion started by: Arun_Linux
2 Replies

5. UNIX for Advanced & Expert Users

Convert 32 bit hex value into fields in decimal

I have 32 bit value in hex that I want to separate into fields and then convert the fields into decimal values. Input file has 2 words of 32 bit hex values: 000001ac ca85210e Output both words separated into individual bit fields: ca85210e: f1(31:9), f2(8:0) f7c392ac: f1(31:14),... (2 Replies)
Discussion started by: morrbie
2 Replies

6. Shell Programming and Scripting

Decimal to hex conversion

Dear All PROs Thanks in advance need a shell for Decimal to hex conversion input file (decimal values) 65,5,48,66,133,131,118,47 65,5,48,66,133,131,83,63 . . desire output should be (Hex value)... (11 Replies)
Discussion started by: The_Archer
11 Replies

7. Shell Programming and Scripting

Convert hex to decimal or reverse is better?

Please Help Me! about the problem down under. I have 2 files with nearly the same characteristics, I have to convert one to the other format or the other format to one's format. I want to write it with awk. The first file contain lines like this: 300000001#A#Y#Y#Y#Y The other file contain... (4 Replies)
Discussion started by: Axel82
4 Replies

8. Shell Programming and Scripting

hex to decimal

hi all, echo "ibase=16;obase=10;11" | bc shouldn't i get 17? i am getting 11 i am trying to convert 11 (hex) to decimal stuck! JAK (4 Replies)
Discussion started by: jakSun8
4 Replies

9. Shell Programming and Scripting

Hex to Decimal Convertion

Dear all, I have a file like this. EE48 4473 7FC9 EE48 102C D23 EE48 4DD 27D EE48 0 0 EE48 3FFE 854 F230 DC6 ... (1 Reply)
Discussion started by: Nayanajith
1 Replies

10. UNIX for Advanced & Expert Users

Req on how to convert hex numbers to decimals

Hi, If i have an input as c1:41 c2:0x0000.00046b3e I want to make output display as c1:41 c2:224062 . Basically convert first part 0x0000 (as hex) to decimal which is 0 and convert second part 0x00046b3e (as hex) to decimal which is 289598 and as such add both parts namely... (3 Replies)
Discussion started by: hare
3 Replies
Login or Register to Ask a Question