title: Convert BCD encoded hexadecimal value into timestamp / data and time ( ASN.1 syntax)
body: <p>Hi i have hexadecimal string of time stamp generated by MSC (is a neighbouring base stations keeps a record of the location of the mobile subscribers). Now I want to convert hexadecimal string to human readable date and time.</p>
  
  <p>Hear actual hexadecimal string variable is a Octet String data type (ASN.1 syntax)</p>
  
  <p>let me give a example how the hexadecimal string look like</p>
  
  <pre><code>hexadecimal value : 1307301826042B0530
  </code></pre>
  
  <p>hear the hexadecimal value formed by converting BCD encoded timestamp as bellow </p>
  
  <pre><code>Local time of UTC compression format
  --YY    =   Year 00 to 99       BCD encoded
  --MM    =   Month 01 to 12      BCD encoded
  --DD    =   Day 01 to 31        BCD encoded
  -- hh   =   hour 00 to 23       BCD encoded
  -- mm   =   minute 00 to 59     BCD encoded
  -- ss   =   second 00 to 59     BCD encoded
  -- S    =   Sign 0 = “+”, “-“       ASCII encoded
  -- hh   =   hour 00 to 23       BCD encoded
  -- mm   =   minute 00 to 59     BCD encoded
  </code></pre>
  
  <p>So bellow table shows time stamp of Octet String </p>
  
  <pre><code>in ASN.1 syntax it look like  TimeStamp ::= OCTET STRING (SIZE(9))
  
  Octet 1 YY
  Octet 2 MM
  Octet 3 DD
  Octet 4 hh
  Octet 5 mm
  Octet 6 ss
  Octet 7 S
  Octet 8 hh
  Octet 9 mm
  </code></pre>
  
  <p>So according to above table the hex value can be divided as fallow </p>
  
  <pre><code> Hex value : 1307301826042B0530
  
      Octet 1 YY  : 13  
      Octet 2 MM  : 07
      Octet 3 DD  : 30
      Octet 4 hh  : 18            Hear every octet is of one byte each
      Octet 5 mm  : 26
      Octet 6 ss  : 04 
      Octet 7 S  :  2B
      Octet 8 hh  : 05
      Octet 9 mm  : 30
  </code></pre>
  
  <p>So to convert hex to time stamp / date and time i did as bellow </p>
  
  <p>First i converted each hex byte into a BCD and BCD to binary and binary to decimal then i got </p>
  
  <pre><code>Decimal : 685571919861616
  </code></pre>
  
  <p>then i tried to convert into date and time </p>
  
  <pre><code>        String decimal = "685571919861616";
          Calendar c=Calendar.getInstance();
          c.setTimeInMillis(Long.parseLong(decimal));
  
          DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  
          System.out.println(dateFormat.format(c.getTime()));
  </code></pre>
  
  <p>But output was like <code>23694-11-28 09:54:21</code></p>
  
  <p>even i tried to convert hex to decimal then to convert decimal into date and time using java but output was like <code>8227-12-21 02:24:03</code> </p>
  
  <p>pleas help me on this and i would like to know is there any class present in Bouncy castle API to do this ...???</p>
  
  <p>Any help will be appreciated. </p>