Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Convert HEX to a String

Posted on 2005-04-27
19
Medium Priority
?
1,135 Views
Last Modified: 2011-10-03
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
Comment
Question by:jhughes4
  • 10
  • 6
  • 3
19 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13879897
How this "HEX 00 00 01 01 00 00 00 00" represent a date?
0
 

Author Comment

by:jhughes4
ID: 13879917
It represents January 1, year 0000, 00:00:00.0 is that what you're asking?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13879945
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:aozarov
ID: 13879950
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

by:aozarov
ID: 13879956
Also, just to make sure.
Is it homework excerise?
0
 

Author Comment

by:jhughes4
ID: 13880000
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

by:aozarov
ID: 13880119
>> 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

by:jhughes4
ID: 13880172
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

by:aozarov
ID: 13880356
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

by:
CEHJ earned 600 total points
ID: 13880495
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

by:CEHJ
CEHJ earned 600 total points
ID: 13880543
Or indeed:

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


etc.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13880799
:-)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13880818
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

by:jhughes4
ID: 13880915
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

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

Author Comment

by:jhughes4
ID: 13880977
That would be great.  Thanks!
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13881030
:-)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13882010
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

by:jhughes4
ID: 13889640
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question