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
thanks
How this "HEX 00 00 01 01 00 00 00 00" represent a date?
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 ?
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?
with or without the HEX?
with or without spaces?
Also, just to make sure.
Is it homework excerise?
Is it homework excerise?
ASKER
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 ?
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 UnsupportedEncodingExcepti on
{
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(b ytes[4] & 0xff).append(':').append(b ytes[5] & 0xff).append(':');
stBuff.append(bytes[6] & 0xff).append('.').append(b ytes[7] & 0xff);
if (bytes.length == 11 && (bytes[8]==0x2b || bytes[8]==0x2d))
stBuff.append(',').append( (char)byte s[8]).appe nd(bytes[9 ] & 0xff).append(':').append(b ytes[10] & 0xff);
return stBuff.toString();
}
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,
You can try this method:
public static String stringToDateAndTime(String
{
byte[] bytes = str.getBytes("ISO-8859-1")
StringBuffer stBuff = new StringBuffer();
stBuff.append(((bytes[0] & 0xff) << 8) + (bytes[1] & 0xff)).append('-').append(
stBuff.append(bytes[3] & 0xff).append(',').append(b
stBuff.append(bytes[6] & 0xff).append('.').append(b
if (bytes.length == 11 && (bytes[8]==0x2b || bytes[8]==0x2d))
stBuff.append(',').append(
return stBuff.toString();
}
ASKER
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
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
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?
and not the raw Octet string given by the device.
May I ask which snmp library you are using?
ASKER
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.
ASKER
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)
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)
ASKER
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..
thanks again..