Link to home
Start Free TrialLog in
Avatar of jhughes4
jhughes4

asked on

Convert HEX to a String

If I have this date in HEX 00 00 01 01 00 00 00 00, what's the best way to convert it to a String?

thanks
Avatar of aozarov
aozarov

How this "HEX 00 00 01 01 00 00 00 00" represent a date?
Avatar of jhughes4

ASKER

It represents January 1, year 0000, 00:00:00.0 is that what you're asking?
Almost, which part of the HEX string represent any part of the date (year, month, day, hour, time, ..)?
is it: HEX 00 00            01        01            00      00           00           00
              year             month       day      hour    minute     second    ms ?
And is this value "HEX 00 00 01 01 00 00 00 00" stored as a string?
with or without the HEX?
with or without spaces?
Also, just to make sure.
Is it homework excerise?
Actually a more real one looks like this.  07 D5 02 1C 0E 01 00 00.  It comes across as a string with the spaces.  It's from an SNMP walk, not a homework exercise.
Yes, that's the representation.

HEX 00 00            01        01            00      00           00           00
              year             month       day      hour    minute     second    ms ?

 
>> It's from an SNMP walk
If it is an Snmp DateAndTime type  (which is actually an Octet String then the format is actually different.
It should be: OCTET STRING;(SIZE (8 | 11));2d-1d-1d,1d:1d:1d.1d,1a1d:1d
You can try this method:
      public static String stringToDateAndTime(String str) throws UnsupportedEncodingException
      {
            byte[] bytes = str.getBytes("ISO-8859-1");
            StringBuffer stBuff = new StringBuffer();
            stBuff.append(((bytes[0] & 0xff) << 8) + (bytes[1] & 0xff)).append('-').append(bytes[2] & 0xff).append('-');
            stBuff.append(bytes[3] & 0xff).append(',').append(bytes[4] & 0xff).append(':').append(bytes[5] & 0xff).append(':');
            stBuff.append(bytes[6] & 0xff).append('.').append(bytes[7] & 0xff);
            if (bytes.length == 11 && (bytes[8]==0x2b || bytes[8]==0x2d))
                  stBuff.append(',').append((char)bytes[8]).append(bytes[9] & 0xff).append(':').append(bytes[10] & 0xff);

            return stBuff.toString();
      }
Did I do something wrong?  Here's the output of your solution.

Before going in: 07 D5 02 1C 0B 10 06 00
The result: 12343-32-68,53:32:48.50 //from stringToDateAndTime
Do you manipulate in any way the string you are getting from the snmp device (you or your snmp library)?
The code I gave you takes the Octet string from the SNMP Agent (as is) and based on the DateAndTime SMI definition translate it to a visual date.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)
So, I take it that your snmp value was already formed as a string using such format: "07 D5 02 1C 0E 01 00 00"
and not the raw Octet string given by the device.
May I ask which snmp library you are using?
Yes by the time it gets to the piece I was working on it was converted to a String.  I started off using joesnmp, but I may have to do something different for Bridge-MIBs and MIB III.  Don't know yet.
Ok, I am part of the joesnmp group and will be happy to assist you regarding this library.
That would be great.  Thanks!
:-)
Two small changes you may want to consider is:
cal.set(Calendar.MONTH, Integer.parseInt(month, 16) - 1);
as the Calendar stores months from 0 to 11.
Also you might want to call cal.setLenient(false) if you want to have
validation of the input (and to get an exception when its wrong).
When lenient is set to true (which is the default) setting the MONTH to 12 will cause
the calendar to add one to your year value and to become 0 (January) as the month. (the same rollover logic applies to all)
Thanks.  I made that change.  I'm going to ask a separate question in MISC, hopefully you'll be able to answer.  I'm having a problem with the OID hrSWInstalledDate, it indicates that it should show the last modification date of the application as it would appear in a directory listing.  However, when I make a change to an application's directory, the new date doesn't show up.  Do you know why, or know of a better OID that I could use to represent more accurately when an application has changed?

thanks again..