通过WTC与后台c的程序交换,需要Java程序传入一个字节流给后台C程序解析。
后台C的结构;
/*
*内部通讯Buff结构
*/
struct FRONT_BUFF
{
unsigned char MsgType[8];/* 消息类型 */
unsigned char Save2;
unsigned char PkgSeq[2];/* 包序号 256进制 第一个字节存放整数,第二个字节存放余数*/
unsigned char AreaNo[6+1];/* 区域号 */
}我写的Java字节流转换:
public ByteArrayOutputStream serialize() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
try{
String msgType="12345678";
int save2 = 1;
byte pkgSeg = {'0','0'};
String areaNo = "abcdef";
dout.writeBytes(msgType);
dout.writeByte(save2);
dout.write(pkgSeg);
dout.writeBytes(areaNo);
dout.writeBytes("\0");//加结束符
}catch(IOException e){
System.out.println(e.toString());
}
return bout;
}
后台得到的结果中,除了areaNo解析正确外,其他都不对,
我该如何做转换??我该如何定义我的java的数据格式,能够让后台C程序正确解析??
后台C的结构;
/*
*内部通讯Buff结构
*/
struct FRONT_BUFF
{
unsigned char MsgType[8];/* 消息类型 */
unsigned char Save2;
unsigned char PkgSeq[2];/* 包序号 256进制 第一个字节存放整数,第二个字节存放余数*/
unsigned char AreaNo[6+1];/* 区域号 */
}我写的Java字节流转换:
public ByteArrayOutputStream serialize() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
try{
String msgType="12345678";
int save2 = 1;
byte pkgSeg = {'0','0'};
String areaNo = "abcdef";
dout.writeBytes(msgType);
dout.writeByte(save2);
dout.write(pkgSeg);
dout.writeBytes(areaNo);
dout.writeBytes("\0");//加结束符
}catch(IOException e){
System.out.println(e.toString());
}
return bout;
}
后台得到的结果中,除了areaNo解析正确外,其他都不对,
我该如何做转换??我该如何定义我的java的数据格式,能够让后台C程序正确解析??
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货