reading response APDU data question

Hello,

I am using the smartcardio api to read information off of a smart card. The problem I have right now is that I am having problems 'decoding' the returned data.

e.g.

To read the card number I select 3 files on the card by sending 3 commandAPDUs,

ResponseAPDU r = channel.transmit(new CommandAPDU(cardNo1APDU));

(repeated with two more commands)

I then send another command to read the 8 bytes of data from the card:

r = channel.transmit(new CommandAPDU(getCardNumberAPDU));

Then, as a test I output the returned data to see if it looks right

System.out.println(new String(r.getBytes()));

But the output is displayed as a series of characters, 'squares', question marks etc. So I am assuming that I am not decoding the data properly, but I am a bit stuck as to what to try. I have tried specifying the character set in the

String constuctor, egnew String(r.getBytes(),"UTF-8")

, but that doesn't make much difference.

I am expecting the card number to be 8 digits long.

Can anyone think what I might be doing wrong?

Thanks

[1290 byte] By [DxMa] at [2007-11-27 10:29:47]
# 1

try

byte z[] = {(byte)0x32, 0x33, 0x34, 0x35};

String s = new String(z);

String(byte[] bytes)

Constructs a new String by decoding the specified array of bytes using the platform's default charset.

I don't know if this helps, but the constructor converts the bytes to the String Representation of the bytes received

qoheletha at 2007-7-28 17:59:29 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 2

you may want to convert received bytes to hex representation.

What you received from a card is generally meaningless in utf-8/ascii representation.

ausa at 2007-7-28 17:59:29 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 3

yeah, you are right, I spent quite some time searching the internet for a toBinary mehtod, but then I just created my own. I take the byte and split it off into 2 nibbles, High Nib and Low Nib (& 0xF0, & 0x0F), and then I take each nibble and shift to get each bit to the LSB position and & by 1 to mask off extra bits. (The &1 might not be necessary, but I like to be sure) i.e.

byte nibH = (byte)(bite & 0xF0);

bit7 = (nib1 >> 7) & 1);

And viola, you have your msb, if you continue this pattern, you will get each bit from the bytes, and then you can put this into a string or whatever you like. I hope that was helpful. Also, sorry about my prev post, that one would still cause the ugly chars as before ;-)

qoheletha at 2007-7-28 17:59:29 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 4

public static byte[] hexStringToByteArray(String s) {

byte[] retval = new byte[s.length() / 2];

for (int i = 0; i < retval.length; i++)

retval[i] = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);

return retval;

}

public static String byteArrayToHexString(byte[] bytes) {

StringBuffer stringBuffer = new StringBuffer();

for (int i = 0; i < bytes.length; i++)

stringBuffer.append(String.format("%02X", bytes[i]));

return stringBuffer.toString();

}

ausa at 2007-7-28 17:59:29 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 5

wow, thats a much better method than mine. works nicely!

qoheletha at 2007-7-28 17:59:29 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 6

Does anyone know what the hex chars mean that are returned when you do a get CCC and getACR? i.e. they start out with 6F and are a total of 13 bytes?

I see 6F... in the response data from the response apdu donstructor

qoheletha at 2007-7-28 17:59:29 > top of Java-index,Java Mobility Forums,Consumer and Commerce...
# 7

and what are those?

ausa at 2007-7-28 17:59:29 > top of Java-index,Java Mobility Forums,Consumer and Commerce...