Solved

# Convert HEX to a String

Posted on 2005-04-27
1,116 Views
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
0
Question by:jhughes4

LVL 15

Expert Comment

How this "HEX 00 00 01 01 00 00 00 00" represent a date?
0

Author Comment

It represents January 1, year 0000, 00:00:00.0 is that what you're asking?
0

LVL 15

Expert Comment

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 ?
0

LVL 15

Expert Comment

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?
0

LVL 15

Expert Comment

Also, just to make sure.
Is it homework excerise?
0

Author Comment

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 ?

0

LVL 15

Expert Comment

>> 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();
}
0

Author Comment

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
0

LVL 15

Expert Comment

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.
0

LVL 86

Accepted Solution

Not exactly sure what the format is of the time, but you could tweak this:

String s1 = "07 D5 02 1C 0E 01 00 00";
s1 = s1.replaceAll(" ", "");

String year = s1.substring(0, 4);
String month = s1.substring(4, 6);
String day = s1.substring(6, 8);
String hour = s1.substring(8, 10);
String minute = s1.substring(10, 12);

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(year, 16));
cal.set(Calendar.MONTH, Integer.parseInt(month, 16));
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day, 16));
cal.set(Calendar.HOUR, Integer.parseInt(hour, 16));
cal.set(Calendar.MINUTE, Integer.parseInt(minute, 16));
System.out.println(cal.getTime());
0

LVL 86

Assisted Solution

Or indeed:

String[] dateTokens = s1.split(" ");
String year = dateTokens[0] + dateTokens[1];
String month = dateTokens[2];

etc.
0

LVL 86

Expert Comment

:-)
0

LVL 15

Expert Comment

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?
0

Author Comment

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.
0

LVL 15

Expert Comment

Ok, I am part of the joesnmp group and will be happy to assist you regarding this library.
0

Author Comment

That would be great.  Thanks!
0

LVL 15

Expert Comment

:-)
0

LVL 15

Expert Comment

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)
0

Author Comment

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..

0

## Featured Post

### Suggested Solutions

withoutTen challenge 14 69
sumHeights  challenge 17 51
JVM encoding. How to change encoding. 27 55
array6 challenfge 6 49
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET â€¦
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different dâ€¦
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Downâ€¦
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.