Re: ASCII Hex string to character conversion
- From: "E.Otter" <eotter@xxxxxxxxxxxx>
- Date: Tue, 26 Jul 2005 03:36:10 GMT
Do a search on google for "Base64.java". You should find plenty of examples
including at least one public domain utility at sourceforge.net. The Base64
scheme can encode/decode anything to ascii characters. Its been around for
years. Its pretty much the standard.
If for some reason that doesn't float your boat, use this as a starting
point. It essentially produces 2 "hex" characters for every byte so its
extremely inefficient. Base64 is much more efficient. I had to remove some
company specific stuff so no gaurantees that this compiles/works.
public static String encodeHexString(String sourceText) {
Byte[] rawData = sourceText.getBytes();
StringBuffer hexText= new StringBuffer();
String initialHex = null;
int initHexLength=0;
for(int i=0; i<rawData.length; i++) {
int positiveValue = rawData[i] & 0x000000FF;
initialHex = Integer.toHexString(positiveValue);
initHexLength=initialHex.length();
while(initHexLength++ < 2) {
hexText.append("0");
}
hexText.append(initialHex);
}
return hexText.toString();
}
public static String decodeHexString(String hexText) {
String decodedText=null;
String chunk=null;
if(hexText!=null && hexText.length()>0) {
int numBytes = hexTextlength()/2;
byte[] rawToByte = new byte[numBytes];
int offset=0;
int bCounter=0;
for(int i =0; i <numBytes; i++) {
chunk = hexText.substring(offset,offset+2);
offset+=2;
rawToByte[i] = (byte) (Integer.parseInt(chunk,16) & 0x000000FF);
}
decodedText= new String(rawToByte);
}
return decodedText;
}
.
- References:
- ASCII Hex string to character conversion
- From: David Anderson
- ASCII Hex string to character conversion
- Prev by Date: Re: IS CEclipse well suited for GUI development in Java too?
- Next by Date: Re: Eclipse editor - slowwwwwwww?
- Previous by thread: Re: ASCII Hex string to character conversion
- Next by thread: Re: ASCII Hex string to character conversion
- Index(es):
Relevant Pages
|