Paul,
Try the following functions:
Darren K. Smith
private String ByteArrayToHexString(byte byteArray[]) {
String strArray = new String();
strArray = "";
for (int x=0; x < byteArray.length; x++) {
int b = ((int)byteArray[x] & 0x000000ff);
if (b < 16) {
strArray = strArray + "0" + Integer.toHexString(b).toUpperCase();
}
else {
strArray = strArray + Integer.toHexString(b).toUpperCase();
}
}
return strArray;
}
private byte[] HexStringToByteArray(String strHex) {
byte bytKey[] = new byte[(strHex.length() / 2)];
int y= 0;
String strbyte;
for (int x=0; x < bytKey.length; x++) {
strbyte = strHex.substring(y, (y + 2));
if (strbyte.equals("FF")) {
bytKey[x] = (byte) 0xFF;
}
else {
try {
bytKey[x] = (byte) Integer.parseInt(strbyte, 16);
}
catch (NumberFormatException e){
System.out.println("HexStringToByteArray Failed ");
}
}
y = y + 2;
}
return bytKey;
}
-----Original Message-----
From: Paul Sheridan [mailto:[EMAIL PROTECTED]]
Sent: Monday, March 13, 2000 3:48 PM
To: [EMAIL PROTECTED]
Subject: [OCF] java.lang.String <=> byte[ ]
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
_____________________________________
Get your free E-mail at http://www.ireland.com
---
> Visit the OpenCard web site at http://www.opencard.org/ for more
> information on OpenCard---binaries, source code, documents.
> This list is being archived at http://www.opencard.org/archive/opencard/
! To unsubscribe from the [EMAIL PROTECTED] mailing list send an email
! to
! [EMAIL PROTECTED]
! containing the word
! unsubscribe
! in the body.
---
> Visit the OpenCard web site at http://www.opencard.org/ for more
> information on OpenCard---binaries, source code, documents.
> This list is being archived at http://www.opencard.org/archive/opencard/
! To unsubscribe from the [EMAIL PROTECTED] mailing list send an email
! to
! [EMAIL PROTECTED]
! containing the word
! unsubscribe
! in the body.