Hi forum,
how can I use unpack a hex system date like this 2007-5-24,10:24:33.7 ?
I’m trying with systemdate.unpack(“H2H2-H2-H2,H2:H2:H2.H2”) but it
doesn’t works
Thank you
Hi forum,
how can I use unpack a hex system date like this 2007-5-24,10:24:33.7 ?
I’m trying with systemdate.unpack(“H2H2-H2-H2,H2:H2:H2.H2”) but it
doesn’t works
Thank you
Alfonso C. wrote:
how can I use unpack a hex system date like this 2007-5-24,10:24:33.7 ?
I’m trying with systemdate.unpack(“H2H2-H2-H2,H2:H2:H2.H2”) but it
doesn’t works
Please write your question more clearly, showing few lines of
standalone ruby that demonstrate what you are trying to do. We don’t
have your “systemdate” method so we don’t know what it returns.
Are you saying that the source string looks like this?
systemdate = “2007-5-24,10:24:33.7”
If so, this is clearly not hex, this is a series of decimal values. So
if that’s not what you’re trying to convert, please give an example of
something that you are.
Then, how do you want to “unpack” it? For example, do you want to split
it into an array of numberics, e.g.
[2007,5,24,10,24,33.7]
If I have guessed what you want, here is a starting point:
systemdate.split(/[-,:]/)
=> [“2007”, “5”, “24”, “10”, “24”, “33.7”]
Brian C. wrote:
Alfonso C. wrote:
how can I use unpack a hex system date like this 2007-5-24,10:24:33.7 ?
I’m trying with systemdate.unpack(“H2H2-H2-H2,H2:H2:H2.H2”) but it
doesn’t worksPlease write your question more clearly, showing few lines of
standalone ruby that demonstrate what you are trying to do. We don’t
have your “systemdate” method so we don’t know what it returns.Are you saying that the source string looks like this?
systemdate = “2007-5-24,10:24:33.7”
If so, this is clearly not hex, this is a series of decimal values. So
if that’s not what you’re trying to convert, please give an example of
something that you are.Then, how do you want to “unpack” it? For example, do you want to split
it into an array of numberics, e.g.[2007,5,24,10,24,33.7]
- or something else?
If I have guessed what you want, here is a starting point:
systemdate.split(/[-,:]/)
=> [“2007”, “5”, “24”, “10”, “24”, “33.7”]
Yes, sorry me.
I would convert the result of a SNMP query (1.3.6.1.2.1.25.1.2.0).
It’s a hex string and It could be in these formats (for linux and
windows):
2009-12-28,11:37:25.0,+1:0
2009-12-28,11:53:34.0
With systemDate = systemDate.unpack(“H*”) I’ve 07d90c1c0b2a28002b0100,
where 07d9 is 2009 in decimal, 0c is 12, 1c is 28 and so on…
Thank you very much
Alfonso C. wrote:
With systemDate = systemDate.unpack(“H*”) I’ve 07d90c1c0b2a28002b0100,
where 07d9 is 2009 in decimal, 0c is 12, 1c is 28 and so on…
It would still be easier if you showed the original string, but I’m
guessing it’s this:
systemdate = [“07d90c1c0b2a28002b0100”].pack(“H*”)
=> “\a\331\f\034\v*(\000+\001\000”systemdate.bytes.to_a
=> [7, 217, 12, 28, 11, 42, 40, 0, 43, 1, 0]
In that case:
systemdate.unpack(“nccccc”)
=> [2009, 12, 28, 11, 42, 40]
I’m not sure how you want the last 4 bytes interpreted, but a bit of
googling for SNMP systemDate suggests the following:
systemdate.unpack(“nccccccAcc”)
=> [2009, 12, 28, 11, 42, 40, 0, “+”, 1, 0]
i.e. 11:42:40.0 (tenths of a second) with a timezone of +01:00
2009-12-28,11:37:25.0,+1:0
2009-12-28,11:53:34.0With systemDate = systemDate.unpack(“H*”) I’ve 07d90c1c0b2a28002b0100,
where 07d9 is 2009 in decimal, 0c is 12, 1c is 28 and so on…Thank you very much
mmh how can I use unpack(“H*”) and .to_i(base=10) together?
Brian C. wrote:
systemdate.unpack(“nccccccAcc”)
=> [2009, 12, 28, 11, 42, 40, 0, “+”, 1, 0]i.e. 11:42:40.0 (tenths of a second) with a timezone of +01:00
Just to make this clear: you should not be using the “H” format
converter. “H” gives you ASCII hex representation, for example the byte
0x07 is converted into the two ASCII characters “0” and “7”, or vice
versa.
What you appear to have is a binary string, and you want the byte 0x07
to be converted to the number 7, or the pair of bytes 0x07 0xd9 to be
converted to the number 2009. These are the “c” and “n” formats
respectively.
Type “ri String#unpack” at the command line for more info, or look at
the rdoc on the web.
systemdate.unpack(“nccccc”)
=> [2009, 12, 28, 11, 42, 40]I’m not sure how you want the last 4 bytes interpreted, but a bit of
googling for SNMP systemDate suggests the following:systemdate.unpack(“nccccccAcc”)
=> [2009, 12, 28, 11, 42, 40, 0, “+”, 1, 0]i.e. 11:42:40.0 (tenths of a second) with a timezone of +01:00
Great! It’s very good for my script! Thank you very much!
2009/12/28 Brian C. [email protected]:
What you appear to have is a binary string, and you want the byte 0x07
to be converted to the number 7, or the pair of bytes 0x07 0xd9 to be
converted to the number 2009. These are the “c” and “n” formats
respectively.
I am not so sure. He said
It’s a hex string and It could be in these formats (for linux and
windows):2009-12-28,11:37:25.0,+1:0
2009-12-28,11:53:34.0
If it is actually a string, a completely different tool might better
suit the job:
require ‘date’
[
‘2009-12-28,11:37:25.0,+1:0’,
‘2009-12-28,11:53:34.0’,
].each do |str|
dt = DateTime.strptime str, ‘%Y-%m-%d,%H:%M:%S’
printf “%s → %p (%s)\n”, str, dt, dt
end
That version omits time zone information though.
Cheers
robert
Alfonso C. wrote:
systemdate.unpack(“nccccc”)
=> [2009, 12, 28, 11, 42, 40]I’m not sure how you want the last 4 bytes interpreted, but a bit of
googling for SNMP systemDate suggests the following:systemdate.unpack(“nccccccAcc”)
=> [2009, 12, 28, 11, 42, 40, 0, “+”, 1, 0]i.e. 11:42:40.0 (tenths of a second) with a timezone of +01:00
Great! It’s very good for my script! Thank you very much!
dtm = systemDate.unpack(“ncccccc”)
dtm =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/
systemDate = “#{dtm[0]}-#{dtm[1]}-#{dtm[2]}
#{dtm[3]}:#{dtm[4]}:#{dtm[5]}”
Is it obscene?
2009/12/28 Alfonso C. [email protected]:
i.e. 11:42:40.0 (tenths of a second) with a timezone of +01:00
Great! It’s very good for my script! Thank you very much!
dtm = systemDate.unpack(“ncccccc”)
dtm =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/
systemDate = “#{dtm[0]}-#{dtm[1]}-#{dtm[2]}
#{dtm[3]}:#{dtm[4]}:#{dtm[5]}”Is it obscene?
Your code is a bit weird. First you unpack which returns an Array.
Then you try to match that Array against a regular expression without
doing anything with the result (as far as I see it will always return
nil anyway). Finally you do array accesses on the Array returned by
#unpack. Why do you have the regexp match attempt in there?
Note that there are tools in Ruby to actually deal with date and time.
So in your case you could do something like this (including giving
values proper names):
require ‘date’
year, month, day, hour, minute, second, fract, = *system_date.unpack
“ncccccc”
date = DateTime.new year, month, day, hour, minute, second + fract /
10.0
Now you can do all the date math with “date” that you need or just print
it.
Kind regards
robert
Your code is a bit weird. First you unpack which returns an Array.
Then you try to match that Array against a regular expression without
doing anything with the result (as far as I see it will always return
nil anyway). Finally you do array accesses on the Array returned by
#unpack. Why do you have the regexp match attempt in there?Note that there are tools in Ruby to actually deal with date and time.
So in your case you could do something like this (including giving
values proper names):
mmmh… Sorry but my aim is to get the date of a remote system via an
SNMP query
2009/12/28 Alfonso C. [email protected]:
mmmh… Sorry but my aim is to get the date of a remote system via an
SNMP query
And?
robert
Robert K. wrote:
2009/12/28 Alfonso C. [email protected]:
mmmh… Sorry but my aim is to get the date of a remote system via an
SNMP queryAnd?
dtm = systemDate.unpack(“ncccccc”)
systemDate = “#{dtm[0]}-#{dtm[1]}-#{dtm[2]}
#{dtm[3]}:#{dtm[4]}:#{dtm[5]}”
it seems sufficient
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs