Link to home
Start Free TrialLog in
Avatar of cottyengland
cottyengland

asked on

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

Avatar of Tommy Braas
Tommy Braas
Flag of Australia image

long val = Long.parseLong("001011110001001000000011001001010011001100010011100100110100001011011111110111000001110000110101", 2);
String hexString = Long.toHexString(val);
Actually, that is not going to work.
ASKER CERTIFIED SOLUTION
Avatar of Tommy Braas
Tommy Braas
Flag of Australia 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
Avatar of cottyengland
cottyengland

ASKER

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();
   }
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.
Yes I did.. sorry. still get the same erros as posted above.
The code I posted was straight out of my IDE. Post the error, I doubt it is EXACTLY the same.
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?****

   }
}
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.
Thank You orangehead911, it worked perfectly.