title: ISO 7816 APDU for Smart Card using javax.smartcardio
body: <p>I've already used <code>javax.smartcardio</code> to read serial number from smart card without much effort. However now I'm assigned to create MF on blank card (without which serial number can't be read).I'm creating APDU command for that as per ISO 7816 guidelines,but unable to create proper APDU command because my hex values are getting converted to wrong bytes.</p>
  
  <pre><code>import javax.smartcardio.Card;
  import javax.smartcardio.CardChannel;
  import javax.smartcardio.CardException;
  import javax.smartcardio.CardTerminal;
  import javax.smartcardio.CommandAPDU;
  import javax.smartcardio.ResponseAPDU;
  import javax.smartcardio.TerminalFactory;
  
  class SmartCardAPIs {
  
      public int Create_MF() throws CardException{
  
          //--Variable declaration
          int result=0;
          Card card=null;     
          byte[] responseData=null;
          ResponseAPDU answer=null;
          String SW1=null;
          String SW2=null;
          int cla,  ins,  p1,  p2;
          byte[] data=null;
          //---------------------------------------------
  
          //--1--Establish connection with the smart card
          TerminalFactory factory = TerminalFactory.getDefault();
          List&lt;CardTerminal&gt; terminals = factory.terminals().list();
          // Use the first terminal
          CardTerminal terminal = terminals.get(0);
          // Connect with the card
          card = terminal.connect("*");
          CardChannel channel = card.getBasicChannel();           
          //---------------------------------------------
  
          //--2--Create MF 
          cla=0x00;
          ins=0xE0;
          p1=0x00;
          p2=0x00;
          data = new byte[] {
              (byte) 0x21,
              (byte) 0x62,
              (byte) 0x1F,
              (byte) 0x82, // **** Getting converted to -126 ****
              --
              --
              --
          };          
          answer = channel.transmit(new CommandAPDU(cla,  ins,  p1,  p2, data));
          responseData= answer.getBytes();
  
          if(responseData!=null)
          {
              if(responseData.length==2)
              {
                  SW1=String.format("%02X ", (responseData[0])).trim();
                  SW2=String.format("%02X ", (responseData[1])).trim();
              }
          }       
      }
  }
  </code></pre>
  
  <p>I've 2 problems</p>
  
  <p>1: data in command APDU is taking a byte which is wrong(marked as <strong>*</strong>).</p>
  
  <p>2: SW1 and SW2 are returning  as 6A 80 which means parameter in data field are incorrect(I guess because of negative value while casting int in hex format to byte,but can not help much as I'm forced to do it).</p>
  
  <p>The partial APDU command which I've placed here is part of full command which I've been supplied ,and the command is 100% OK and tested as I've been successful in creating MF in blank card using smart card tools with the command.I want to do the same in java now.</p>
  
  <p>I assume that problem lies with the way this APDU is being getting created,may be negative value problem(I'm not very expert with java APDU stuffs  although I 've created Applet to read serial number from card).</p>