• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 775
  • Last Modified:

How to convert the following string to hex string in Java

How can I convert the following string ....

96 bit string.. stored as 96 bit string
001011110001001000000011001001010011001100010011100100110100001011011111110111000001110000110101

to hex STRING

24 characters..

2F02032533139342DFDC1C35

thank you

0
cottyengland
Asked:
cottyengland
  • 7
  • 4
1 Solution
 
Tommy BraasCommented:
long val = Long.parseLong("001011110001001000000011001001010011001100010011100100110100001011011111110111000001110000110101", 2);
String hexString = Long.toHexString(val);
0
 
Tommy BraasCommented:
Actually, that is not going to work.
0
 
Tommy BraasCommented:
Assuming MSB to the far left.

  public static String binaryToHexString(String source) {
      StringBuffer retval = new StringBuffer();
      for (int i = source.length(); i > 0; i-=4) {
       i = i < 0 ? 0 : i;
       String temp = Integer.toHexString(Integer.parseInt(source.substring(i - 4, i), 2));
       retval.insert(0, temp);
      }
      return retval.toString();
   }
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
cottyenglandAuthor Commented:
The name of my "source string" is allstring

I cannot get this to compile....


C:\java1_4\bin>javac -classpath cfx.jar;alienrfid.jar HEX_EPC.java
HEX_EPC.java:95: illegal start of expression
public static String binaryToHexString( allstring) {
^
HEX_EPC.java:105: ';' expected
^
2 errors


my code.

public static String binaryToHexString( String allstring) {
      StringBuffer retval = new StringBuffer();
      for (int i = source.length(); i > 0; i-=4) {
      i = i < 0 ? 0 : i;
      String temp = Integer.toHexString(Integer.parseInt(source.substring(i - 4, i), 2));
      retval.insert(0, temp);
      }
      
      return retval.toString();
   }
0
 
Tommy BraasCommented:
why did you change the name of the parameter? if you change the name of a variable you have to change all occurrences of that variable.
0
 
cottyenglandAuthor Commented:
Yes I did.. sorry. still get the same erros as posted above.
0
 
Tommy BraasCommented:
The code I posted was straight out of my IDE. Post the error, I doubt it is EXACTLY the same.
0
 
cottyenglandAuthor Commented:
public class HEX_EPC implements CustomTag {

 

  public void processRequest( Request request, Response response)
  throws Exception
  {

****Is it throwing an error because it exists within these brackets?****

   }
}
0
 
Tommy BraasCommented:
I posted a METHOD. A method lives at the top level in a class. That means you CAN NOT put a method INSIDE another method. Move it outside of processRequest.

I would suggest that you download http://www.mindview.net/Books/TIJ/ . It's free.
0
 
cottyenglandAuthor Commented:
Thank You orangehead911, it worked perfectly.

0
 
Tommy BraasCommented:
;-D
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now