Hi Paul,
find attached a class that implement several conversion.
yes! Yet Another Conversion Class .. ,-)
hope this help,
Sylvain.
attached: "ByteString.java"
At 00:04 14/03/00 +0000, Paul Sheridan wrote:
>Hello,
>Further to an early post - I was recommended to look at the HexString
>class to help me with my problem, but pardon my ignorance but i cant get
>it working for the life of me !
>
>I assume that the method of getting a string into a byte array of its hex
>representation is as follows:
>
>1-call getBytes() on String giving byte array w/decimal ascii codes
>2-hexify(byte[]) it giving a String in hex format
>3-assign the byte array the return value of parseHexString() meaning that
>each byte has 2 hex chars
>
>e.g.
>String = "AAA" getBytes() yields [65][65][65]
>hexify yields "414141"
>and then parseHexString("414141") gives [41][41][41]
>
>this doesn't work, I constantly get number format exceptions (could this
>be because hexify puts spaces between hex pairs and thus parsing it fails
>?!)- i dont know which of the functions to use at this stage - or in which
>order ?! so if anybody could please tell me the step by step procedure
>for getting a String's hex representation into a byte array with 2 hex
>chars per byte i would very much appreciate. and conversly the inverse of
>it also i.e. byte array of pairs of hex chars into an "ascii" String !
>
>Thank you in advance,
>Paul
//----------------------------------------------
package sf.core.util;
//----------------------------------------------
public class ByteString
{
//------------------------------------------
public static byte[] StrToByte(String str)
// "ABC..." -> [41,42,43,..]
{
if (str == null || str.length() == 0)
return new byte[0];
int len = str.length();
byte[] data = new byte[len];
for (int i = 0; i < len; i++)
data[i] = (byte) str.charAt(i);
return data;
}
//------------------------------------------
public static byte[] HexStrToByte(String str)
// "10213F..." -> [10,21,3F,..]
{
if (str == null || str.length() == 0)
return new byte[0];
int len = (str.length() >> 1);
byte[] data = new byte[len];
for (int i = 0; i < len; i++)
data[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 +
2), 16);
return data;
}
//------------------------------------------
public static String HexStrToString(String str)
// "414243..." -> "ABC..."
{
if (str == null || str.length() == 0)
return new String("");
int len = (str.length() >> 1);
StringBuffer sb = new StringBuffer(len);
for (int i = 0; i < len; i++)
sb.append((char) Integer.parseInt(str.substring(i * 2, i * 2 +
2), 16));
return sb.toString();
}
//------------------------------------------
private static void appendBytes(StringBuffer stringBuffer, byte int8)
{
stringBuffer.append(Character.forDigit(((int8 >> 4) & 0x0F), 16));
stringBuffer.append(Character.forDigit((int8 & 0x0F), 16));
}
//------------------------------------------
public static String ByteToHexStr(byte[] data)
// [10,21,3F,..] -> "10213F..."
{
if (data == null || data.length == 0)
return new String("");
StringBuffer sb = new StringBuffer(2 * data.length);
for (short i = 0; i < data.length; i++)
appendBytes(sb, data[i]);
return sb.toString();
}
//------------------------------------------
public static String ByteToDumpStr(byte[] data)
// [10,21,3F,..] -> "10 21 3F XX XX XX XX XX ..."
{
if (data == null || data.length == 0)
return new String("");
StringBuffer sb = new StringBuffer(4 * data.length);
short kk = 0;
for (short i = 0; i < data.length; i++){
appendBytes(sb, data[i]);
sb.append(" ");
kk++;
if (kk == 24){
sb.append("\n");
kk = 0;
}
}
if (kk != 0)
sb.append("\n");
return sb.toString();
}
//------------------------------------------
public static String ByteToString(byte[] data)
// [41,42,43,..] -> "ABC..."
{
if (data == null || data.length == 0)
return new String("");
StringBuffer sb = new StringBuffer(data.length);
for (short i = 0; i < data.length; i++)
sb.append((char) data[i]);
return sb.toString();
}
//------------------------------------------
public static String removeSpace(String str)
{
if (str == null || str.length() == 0)
return new String("");
int len = str.length();
StringBuffer sb = new StringBuffer(len);
for (int i = 0; i < len; i++){
char car = str.charAt(i);
if (car != (char) 32 && car != '\t' && car != '\n' && car !=
'\r')
sb.append(car);
}
return sb.toString();
}
//------------------------------------------
}
//----------------------------------------------