LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices



Reply
 
Search this Thread
Old 08-27-2013, 02:16 PM   #1
nab_elf
LQ Newbie
 
Registered: Aug 2013
Posts: 3

Rep: Reputation: Disabled
How to convert a hexadecimal to timestamp via bash script ?


Hello,
please, could you help me to resolve a mesterious problem when i convert time in hexadecimal format to humain format , i mean :

convert for example Ab153d to 00:00:00 with a bash script

Thank's in advance

Last edited by nab_elf; 08-27-2013 at 02:17 PM.
 
Old 08-27-2013, 02:37 PM   #2
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Rep: Reputation: 47
A quick google search of your requirement came up with the following result. Worth checking out I guess.

http://www.epochconverter.com/epoch/...-timestamp.php
 
Old 08-27-2013, 03:14 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 1,773

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
How's this?

Code:
perl -e '$foo=hex($ARGV[0]); @t=(localtime($foo))[2,1,0]; print $t[0],":",$t[1],":",$t[2],"\n"'  Ab153d
14:28:13

Last edited by smallpond; 08-28-2013 at 01:19 PM. Reason: fix wrong hex conversion
 
Old 08-28-2013, 06:25 AM   #4
nab_elf
LQ Newbie
 
Registered: Aug 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi,
thank's for replying ,

but is there any solution to do the same with bash script instead of perl ?


Thank's
 
Old 08-28-2013, 06:40 AM   #5
nab_elf
LQ Newbie
 
Registered: Aug 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hello

Hello,
please find out in the attachment the resul when i lunch perl command. always the result = 0:0:0
Attached Images
File Type: jpg perl.jpg (15.2 KB, 7 views)
 
Old 08-28-2013, 07:45 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
I'm not entirely sure what your overall goal is, with that in mind.

Assuming that AB153D and 521DD201 are hex representations of linux epoch (seconds from January 1, 1970), have a look at this:
Code:
# hex input is 521dd201:
date --date @$( printf '%d\n' 0x521dd201) '+%Y-%m-%d %T'
2013-08-28 12:33:37

# hex input is Ab153d
date --date @$( printf '%d\n' 0xAb153d) '+%Y-%m-%d %T'
1970-05-10 19:28:13
The printf part converts from hex to decimal, which leaves you with epoch in seconds. This epoch can be given to the date command (--date @no_of_seconds), the last part ('+%Y-%m-%d %T') specifies the wanted output.

See the date manual page for details.
 
Old 08-28-2013, 07:45 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Code:
printf "%d\n" 0x521DD201

date --date="@$(printf "%d\n" 0x521DD201)"

date "+%Y-%m-%d %H:%M:%S ( %s )" --date="@$(printf "%d\n" 0x521DD201)"

too slow

Last edited by Firerat; 08-28-2013 at 07:46 AM.
 
Old 08-28-2013, 07:51 AM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
and if you wanted to go the other way..

Code:
printf '0x%x\n' $(date +%s)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find out timestamp line and delete if it is older than particular/specific days using bash script nitya Linux - Newbie 10 09-26-2014 02:30 PM
[SOLVED] log bash script output to file with timestamp daisychick Programming 11 05-08-2013 02:50 AM
convert string to hexadecimal kpachopoulos Programming 3 12-11-2007 07:23 AM
Adding timestamp to a BASH script thefox Linux - Software 1 11-10-2007 10:29 PM
function in shell to convert hexadecimal into ascii suchi_s Linux - Software 1 04-01-2005 03:07 PM


All times are GMT -5. The time now is 08:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration