实现功能:(1)把个人信息写到智能卡中。(2)从智能卡读出个人信息。
实现方法:将界面上的个人信息数据提取出来,然后转换为一个由16进制的byte合成的字符串。用UCS-2即两个字节表示任何1个字符。
个人信息数据包括CID、类型、版本、手机号码、姓名等。
private void btnSave_Click(object sender, EventArgs e)
{
CommonUtil.beginWaitCursor(0);
// 从界面中获得数据,将Gbk编码的数据转换为Ucs2编码
string cid = CommonUtil.strGbkToStrUcs2(txtBoxCID.Text.Trim());
string type = CommonUtil.strGbkToStrUcs2(txtBoxType.Text.Trim());
string version = CommonUtil.strGbkToStrUcs2(txtBoxVersion.Text.Trim());
string phoneNumber = CommonUtil.strGbkToStrUcs2(txtBoxPhoneNumber.Text.Trim());
string name = CommonUtil.strGbkToStrUcs2(txtBoxName.Text.Trim());
// 每个数据项的长度
int lenCid = cid.Length / 2;
int lenType = type.Length / 2;
int lenVersion = version.Length / 2;
int lenPhoneNumber = phoneNumber.Length / 2;
int lenName = name.Length / 2;
// 将作为APDU的数据段
string data = CommonUtil.bytToStrHex((byte)lenCid) + cid +
CommonUtil.bytToStrHex((byte)lenType) + type +
CommonUtil.bytToStrHex((byte)lenVersion) + version +
CommonUtil.bytToStrHex((byte)lenPhoneNumber) + phoneNumber +
CommonUtil.bytToStrHex((byte)lenName) + name;
//CommonUtil.info("data的长度:" + data.Length / 2 + ",/ndata:" + data);
writeData(data);
clearForm();
CommonUtil.endWaitCursor();
CommonUtil.info("保存成功!");
}
private void writeData(string data)
{
byte[] bytAry;
bytAry = Utilities.SMARTMxRW(EEWayAPDU.apduChooseEEWayInfo);
//CommonUtil.info("选择applet:" + CommonUtil.bytAryToStrHex(bytAry));
bytAry = Utilities.SMARTMxRW(EEWayAPDU.buildApduWriteEEWayInfo(data));
//CommonUtil.info("writeData:" + CommonUtil.bytAryToStrHex(bytAry));
formMain.resetSDCard();
}
private byte[] readData()
{
byte[] bytAry;
bytAry = Utilities.SMARTMxRW(EEWayAPDU.apduChooseEEWayInfo);
//CommonUtil.info("选择applet:" + CommonUtil.bytAryToStrHex(bytAry));
bytAry = Utilities.SMARTMxRW(EEWayAPDU.apduReadEEWayInfo);
//CommonUtil.info("readData:" + CommonUtil.bytAryToStrHex(bytAry));
return bytAry;
}
private void clickBtnRead()
{
CommonUtil.beginWaitCursor(0);
byte[] bytAry = readData();
int lenCid, lenType, lenVersion, lenPhoneNumber, lenName;
string cid, type, version, phoneNumber, name;
int offset = 1; // 从第2个字节开始的数据有效
lenCid = bytAry[offset];
cid = getStrFromBytAry(bytAry, offset + 1, lenCid);
txtBoxCID.Text = cid;
offset = offset + lenCid + 1;
lenType = bytAry[offset];
type = getStrFromBytAry(bytAry, offset + 1, lenType);
txtBoxType.Text = type;
offset = offset + lenType + 1;
lenVersion = bytAry[offset];
version = getStrFromBytAry(bytAry, offset + 1, lenVersion);
txtBoxVersion.Text = version;
offset = offset + lenVersion + 1;
lenPhoneNumber = bytAry[offset];
phoneNumber = getStrFromBytAry(bytAry, offset + 1, lenPhoneNumber);
txtBoxPhoneNumber.Text = phoneNumber;
offset = offset + lenPhoneNumber + 1;
lenName = bytAry[offset];
name = getStrFromBytAry(bytAry, offset + 1, lenName);
txtBoxName.Text = name;
CommonUtil.endWaitCursor();
}
private void btnRead_Click(object sender, EventArgs e)
{
this.clickBtnRead();
}
private string getStrFromBytAry(byte[] bytAry, int offset, int len)
{
return Encoding.BigEndianUnicode.GetString(bytAry, offset, len);
}
// 将一个byte转换为16进制的String
public static string bytToStrHex(byte byt)
{
string strHex = byt.ToString("x");
return strHex.Length == 1 ? "0" + strHex : strHex;
}
// 将一个byte数组转换为16进制的String
public static string bytAryToStrHex(byte[] bytAry)
{
string strHex = "";
for (int i = 0; i < bytAry.Length; i++)
strHex += bytToStrHex(bytAry[i]);
return strHex;
}
// 将一个gbk编码的String转换为Ucs2编码的String
public static string strGbkToStrUcs2(string strGbk)
{
byte[] bytAry = Encoding.BigEndianUnicode.GetBytes(strGbk);
return bytAryToStrHex(bytAry);
}
// 将一个Ucs2编码的String转换为byte数组
public static byte[] strUcs2ToBytAry(string strUcs2)
{
int len = strUcs2.Length / 2;
byte[] bytAry = new byte[len];
for (int i = 0, j = 0; i < strUcs2.Length; i += 2, j++)
{
string strByt = strUcs2.Substring(i, 2);
bytAry[j] = (byte)strXToDec(strByt, 16);
}
return bytAry;
}
// 将一个Ucs2编码的String转换为Gbk编码的String
public static string strUcs2ToStrGbk(string strUcs2)
{
int len = strUcs2.Length / 2;
byte[] bytAry = strUcs2ToBytAry(strUcs2);
return Encoding.BigEndianUnicode.GetString(bytAry, 0, len);
}
// 将其他进制的String转换到十进制数字
public static int strXToDec(string Num, int n)
{
char[] nums = Num.ToCharArray();
int d = 0;
for (int i = 0; i < nums.Length; i++)
{
string number = nums[i].ToString();
if (n == 16)
{
switch (number.ToUpper())
{
case "A":
number = "10";
break;
case "B":
number = "11";
break;
case "C":
number = "12";
break;
case "D":
number = "13";
break;
case "E":
number = "14";
break;
case "F":
number = "15";
break;
}
}
Double power = Math.Pow(Convert.ToDouble(n),
Convert.ToDouble(nums.Length - (i + 1)));
d = d + Convert.ToInt32(number) * Convert.ToInt32(power);
}
return d;
}
相关资料:
资料1:短信中的UCS2的编码与解码方法(java)
链接:http://rept.javaeye.com/blog/683870
资料2:Unicode详解
链接:http://tech.idv2.com/2008/02/21/unicode-intro/