问题描述
一个有一些数据的类的对象,我已经把那个对象写到java卡中了.我有一个功能,可以将十六进制数据转换为字节数组,然后使用Java卡将该数据写入智能卡.当我将数据转换为十六进制格式时,我会对该数据进行加密.所以我需要将类的对象转换为十六进制.请告诉我如何在Java中将对象转换为十六进制格式.
An object of class having some data and I am gone write that object into java card.I am having a function that convert hexadecimal data into byte array and then write that data to smart card using java card.While i convert data into hex format i encrypt that data.So i need to convert object of class into hexadecimal.Please tell me how to convert object into Hex format in java.
我正在使用智能卡类型=使用Java卡2.2.2的联系卡以及使用apdu的jcop.
I am using smart card type = contact card using java card 2.2.2 with jcop using apdu.
推荐答案
在这里,我向您发送了将对象转换为字节数组的程序,反之亦然.
Here i am sending you program which converts objects to byte array and vice versa.
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
public class Sandbox {
public static void main(String[] args) {
try {
// convert object to bytes
Date d1 = new Date();
System.out.println(d1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(d1);
byte[] buf = baos.toByteArray();
// convert back from bytes to object
ObjectInputStream ois =
new ObjectInputStream(new ByteArrayInputStream(buf));
Date d2 = (Date) ois.readObject();
ois.close();
System.out.println(d2);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
这篇关于如何在Java中将类的对象转换为十六进制数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!