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

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

Please use [ code ][ /code ] (sans the space) for code/logs/listings/...

$ 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

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

ok, thanks for sharing. Is there a question here?

'%s' - Output seconds since the epoch (works at least for Linux and HP-UX 11.31)
date -d @time shows the formatted time for this UNIX timestamp, dunno why it needs that @, but it does. I put in in there for demonstration purpose only, to show the calculation works.

ah, ok - thanks - I don't have that under Solaris.

unfortunately, on my Linux Tablet (embedded TI Debian)
date -ud @`echo $time`
generates
date: invalid date '@118573'

Ok.
The most important thing is to convert hex seconds to decimal seconds,
than to minutes, hours on my Linux Tablet.

thanks,

Jack

Two things that come to mind:

  1. Check your time on the tablet, maybe it isn't set correctly
  2. Check the output of date --help, maybe %s doesn't print the epoch-time in your version

Because a six digit epoch time sure as hell isn't right (except if you're posting through time. In that case I might have some messages for you to relay to Mr. Thompson, Mr. Kernighan, Mr. Ritchie, and some others...)

You are completely right.
Epoch time on my old Internet Tablet is
Fri Jan 2 11:00:50 CET 1970
I loaded acu, get it wifi connected to the Internet
and expected time to reset.
Unfortunately. Nothing of this kind.

Ok. All I need is to convert sex seconds into decimal seconds
than into readable format like

01:44:46
and convert hex bytes into decimal bytes, than into something like

4.481MB or 1.026GB

I need to timestamp converted data with a real date.

Read how to configure my tablet to get connected to ntp server on boot up but don't know if it supports the described procedure.

Set time manually.
Run example and the same error, as system time has nothing to do with the problem (I suppose so).

I need to count seconds and convert into minutes, hours, days,
so I go to test example from reply.

thanks
Jack

The bash shell has built-in conversions to handle octal and hex numbers, consider the following:

svn1:~# (( x = 0377 )); echo $x
255

This example will set X to an octal value of 0377, which is 255 in decimal.

svn1:~# (( x = 0xFFFE )); echo $x
65534

This example will set x to a hex value of 0xFFFE, which is 65534.

The use of (( and )) is a shorthand for the "let" statement.

Bash has built-in conversion tools. Consider the following:

svn1:~# (( x = 0377 )); echo $x
255

This will assign x the octal value of 0377 (the leading 0 indicates an octal value), which returns the decimal value 255.

svn1:~# (( x = 0xFFFE )); echo $x
65534

This will assign x the hex value of 0xFFFE (the leading 0x means hex), which is the decimal value of 65534.
The use of "((" and "))" is shorthand for the "let" statement.

Thanks my dear friend,

for your excellent solution.
Exactly, it works fine under bash.

My cellular modem generates time ticks every 2 s
in the following format
00001F9C

converting the above hex value into decimal value of
8092
I get a number of seconds my modem Internet connection is on.
Converting seconds into minutes, hours, days for totals,
is exactly what lets me control my airtime.

I have compiled Unix dialog utility for mipsel (router)
to let me open GUI window with session time counters via putty ssh.

Yesterday I tried to test some example shell scripts running dialog widgets
to get no flickered data refresh functionality (ncurses library).

I exprimented with running `date` as " some string" 1st parameter .
Succeeded right now and can run dialog window with data update and no flickering.
`date` is updated in gauge widget box.
wait is 1
So I get data refreshed every second.

Now I will try to preset session time and get session left time displayed
in % + gauge bar.

Jack