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 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
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
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.
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'?
'%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.
Check your time on the tablet, maybe it isn't set correctly
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.
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.
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.