The Artima Developer Community
Sponsored Link

Java Answers Forum
How to Convert String to Hex?

1 reply on 1 page. Most recent reply: Apr 23, 2003 3:25 AM by Adam Duffy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Cheong Weng Way

Posts: 1
Nickname: cheongww
Registered: Apr, 2003

How to Convert String to Hex? Posted: Apr 20, 2003 6:51 PM
Reply to this message Reply
Advertisement
Currently, I'm writing a encrypt/decrypt program using DES algorithm.

The copy of program which I downloaded from Internet requires me to enter the key and string to be encrypted in Hex format.

So, can anyone pls help to convert string to Hex format.

e.g. key = "sa"
string = "sa"

Another question is that, is it necessary for DES to input only Hex format string as key and password?

Thanks alot.


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: How to Convert String to Hex? Posted: Apr 23, 2003 3:25 AM
Reply to this message Reply
Taken from the jcap project which can be found at http://jpcap.sourceforge.net/

import java.io.StringWriter;
 
public class HexHelper
{
  /**
   * Convert an int to a hexadecimal string.
   */
  public static String toString(int i) {
    StringBuffer sb = new StringBuffer();
    for(int p=0; p<8; p++) {
      byte b = (byte)(i & 0xf);
      sb.append(nibbleToDigit(b));
      i >>= 4;
    }
 
    return sb.toString();
  }
 
  /** 
   * Converts the lower four bits of a byte into the ascii digit 
   * which represents its hex value. For example:
   * nibbleToDigit(10) produces 'a'.
   */
  public static char nibbleToDigit(byte x) {
    char c = (char)(x & 0xf); // mask low nibble
    return(c > 9 ? (char)(c - 10 + 'a') : (char)(c + '0')); // int to hex char
  }
 
  /**
   * Convert a single byte into a string representing its hex value.
   * i.e. -1 -> "ff"
   * @param b the byte to convert.
   * @return a string containing the hex equivalent.
   */
  public static String toString(byte b) {
    StringBuffer sb = new StringBuffer();
    sb.append(nibbleToDigit((byte)(b >> 4)));
    sb.append(nibbleToDigit(b));
    return sb.toString();
  }
 
  /**
   * Returns a text representation of a byte array.
   *
   * @param bytes a byte array
   * @return a string containing the hex equivalent of the bytes.
   */
  public static String toString(byte [] bytes) {
    StringWriter sw = new StringWriter();
 
    int length = bytes.length;
    if(length > 0) {
      for(int i = 0; i < length; i++) {
        sw.write(toString(bytes[i]));
        if(i != length - 1)
          sw.write(" ");
      }
    }
    return(sw.toString());
  }
}


To use simply use the following code
String hexString = HexHelper.toString( unicodeString.getBytes() );


Adam

Flat View: This topic has 1 reply on 1 page
Topic: Nim\Games of Piles\Grundy's Game in Java Previous Topic   Next Topic Topic: Avoiding this error

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use