I have a byte array named 'hexa', which goes through some processes and array 'hexa1' is produced. packPDU returns a string array which has hexadecimal in the form of characters. I want the format of 'hexa1' to be following so that they can be interpreted as numbers:
byte[] hexa1 = { (byte) 0x0F, (byte) 0xAA, (byte) 0x5C, (byte) 0x4E, (byte) 0x45 ,(byte) 0xA3, (byte) 0XA9, (byte) 0x68};
Following is the code I am using:
package com.bittest;
public class aditya1 {
public static void main(String[] args) {
byte[] hexa = {'A','9', 'G', 'F', 'B', 'C', 'D', 'E', '5', '1'};
byte[] gsm= convertUnicode2GSM(hexa);
byte [] dst=new byte[gsm.length];
byte[] packed_array = packPDU(gsm, (short) 0, dst, (short) 0, (short) dst.length) ;
byte[] hexa1 = new byte [(short)(packed_array.length/2)];
for(short i =0; i<(short)((packed_array.length/2)-1); i++){
hexa1[(short)(i+1)] = (byte)((short)(16*hexa[(short)(2*i+1)]) + (short)(hexa[(short)(2*i+2)]));
}
}
public static byte[] convertUnicode2GSM(byte[] msg) {
byte[] data = new byte[160];
short j=0;
for (short i = 0; i <(short) msg.length; i++) {
switch (msg[i]) {
case (byte)'@': data[j] = 0x00; j++; break;
case (byte)'$': data[j] = 0x02; j++;break;
case (byte)'\n': data[j] = 0x0A; j++;break;
case (byte)'\r': data[j] = 0x0D; j++;break;
case (byte)'_': data[j] = 0x11; j++;break;
//case (byte) 'ß': data[j] = 0x1E; j++;break;
case (byte)' ': data[j] = 0x20; j++;break;
case (byte)'!': data[j] = 0x21; j++;break;
case (byte) '\"': data[j] = 0x22; j++;break;
case (byte)'#': data[j] = 0x23; j++;break;
case (byte)'%': data[j] = 0x25; j++;break;
case (byte)'&': data[j] = 0x26; j++;break;
case (byte)'\'': data[j] = 0x27; j++;break;
case (byte)'(': data[j] = 0x28; j++;break;
case (byte)')': data[j] = 0x29; j++;break;
case (byte) '*': data[j] = 0x2A; j++;break;
case (byte)'+': data[j] = 0x2B; j++;break;
case (byte)',': data[j] = 0x2C; j++;break;
case (byte)'-': data[j] = 0x2D; j++;break;
case (byte) '.': data[j] = 0x2E; j++;break;
case (byte)'/': data[j] = 0x2F; j++;break;
case (byte)'0': data[j] = 0x30; j++;break;
case (byte)'1': data[j] = 0x31; j++;break;
case (byte)'2': data[j] = 0x32; j++;break;
case (byte)'3': data[j] = 0x33; j++;break;
case (byte)'4': data[j] = 0x34; j++;break;
case (byte)'5': data[j] = 0x35; j++;break;
case (byte)'6': data[j] = 0x36; j++;break;
case (byte)'7': data[j] = 0x37; j++;break;
case (byte)'8': data[j] = 0x38; j++;break;
case(byte) '9': data[j] = 0x39; j++;break;
case (byte)':': data[j] = 0x3A; j++;break;
case (byte)';': data[j] = 0x3B; j++;break;
case (byte)'<': data[j] = 0x3C; j++;break;
case (byte)'=': data[j] = 0x3D; j++;break;
case (byte)'>': data[j] = 0x3E; j++;break;
case (byte)'?': data[j] = 0x3F; j++;break;
case (byte)'A': data[j] = 0x41; j++;break;
case (byte)'B': data[j] = 0x42; j++;break;
case (byte)'C': data[j] = 0x43; j++;break;
case (byte)'D': data[j] = 0x44; j++;break;
case (byte)'E': data[j] = 0x45; j++;break;
case (byte)'F': data[j] = 0x46; j++;break;
case (byte)'G': data[j] = 0x47; j++;break;
case (byte)'H': data[j] = 0x48; j++;break;
case (byte)'I': data[j] = 0x49; j++;break;
case (byte)'J': data[j] = 0x4A; j++;break;
case (byte)'K': data[j] = 0x4B; j++;break;
case (byte)'L': data[j] = 0x4C; j++;break;
case (byte)'M': data[j] = 0x4D; j++;break;
case (byte)'N': data[j] = 0x4E; j++;break;
case (byte)'O': data[j] = 0x4F; j++;break;
case (byte)'P': data[j] = 0x50; j++;break;
case (byte)'Q': data[j] = 0x51; j++;break;
case (byte)'R': data[j] = 0x52; j++;break;
case (byte)'S': data[j] = 0x53; j++;break;
case (byte)'T': data[j] = 0x54; j++;break;
case (byte)'U': data[j] = 0x55; j++;break;
case (byte)'V': data[j] = 0x56; j++;break;
case (byte)'W': data[j] = 0x57; j++;break;
case (byte)'X': data[j] = 0x58; j++;break;
case (byte)'Y': data[j] = 0x59; j++;break;
case (byte)'Z': data[j] = 0x5A; j++;break;
// case (byte) 'Ü': data[j] = 0x5E; j++;break;
// case (byte) '§': data[j] = 0x5F; j++;break;
case (byte)'a': data[j] = 0x61; j++;break;
case (byte)'b': data[j] = 0x62; j++;break;
case (byte) 'c': data[j] = 0x63; j++;break;
case (byte)'d': data[j] = 0x64; j++;break;
case (byte)'e': data[j] = 0x65; j++;break;
case (byte)'f': data[j] = 0x66; j++;break;
case (byte)'g': data[j] = 0x67; j++;break;
case (byte)'h': data[j] = 0x68; j++;break;
case (byte)'i': data[j] = 0x69; j++;break;
case (byte)'j': data[j] = 0x6A; j++;break;
case (byte)'k': data[j] = 0x6B; j++;break;
case (byte)'l': data[j] = 0x6C; j++;break;
case (byte)'m': data[j] = 0x6D; j++;break;
case (byte)'n': data[j] = 0x6E; j++;break;
case (byte)'o': data[j] = 0x6F; j++;break;
case (byte)'p': data[j] = 0x70; j++;break;
case (byte)'q': data[j] = 0x71; j++;break;
case (byte)'r': data[j] = 0x72; j++;break;
case (byte)'s': data[j] = 0x73; j++;break;
case (byte)'t': data[j] = 0x74; j++;break;
case (byte)'u': data[j] = 0x75; j++;break;
case (byte)'v': data[j] = 0x76; j++;break;
case (byte)'w': data[j] = 0x77; j++;break;
case (byte)'x': data[j] = 0x78; j++;break;
case (byte)'y': data[j] = 0x79; j++;break;
case (byte)'z': data[j] = 0x7A; j++;break;
case (byte) '|':
data[j] = 0x1B;
j +=1;
data[j] = 0x40;
j++;
break;
default: data[j] = 0x3F; j++;break; // '?'
} // switch
} // for
return data;
} // convertUnicode2GSM
public static byte[] packPDU(byte[] src, short offsetSrc, byte[] dst, short offsetDst, short length) {
short countSrc = (short) 0;
short countDst = (short) 0;
short countCurrent;
byte leftover = (byte) 0;
while (countSrc < length) {
countCurrent = (byte) (countSrc & 7);
if (countCurrent == 0) {
leftover = src[(short) (offsetSrc)];
} else {
dst[offsetDst] = (byte) ((src[offsetSrc] << (8 - countCurrent)) | leftover);
leftover = (byte) (src[offsetSrc] >> countCurrent);
offsetDst++;
countDst++;
}
countSrc++;
offsetSrc++;
}
if ((length % 8) != 0) {
dst[offsetDst] = leftover;
countDst++;
}
return dst;
}
}
I've put this through Java SE testing but I haven't uploaded it yet to Java Card. Paul Bastian seems to have done so though.
Comments on the hex conversion are within the code. Hopefully it also shows some best programming practices: