/*
使用CISO8583Writer类构造报文
*/
bool
Get8583Package(BYTE
**
pBuf,
int
&
nBufLen)
{
char
szDate[
5
]
=
{
'
\0
'
}, szTime[
7
]
=
{
'
\0
'
}, szBatchTransNo[
13
]
=
{
'
\0
'
};
char
szUsers[
128
]
=
{
'
\0
'
}, szDESKey[
33
]
=
{
'
\0
'
}, szBuf[MAX_PATH]
=
{
'
\0
'
}, szDevNo[
9
]
=
{
'
\0
'
};
CISO8583Writer mWriter;
SYSTEMTIME t;
GetLocalTime(
&
t);
_snprintf(szDate,
5
,
"
%02d%02d
"
, t.wMonth, t.wDay);
_snprintf(szTime,
7
,
"
%02d%02d%02d
"
, t.wHour, t.wMinute, t.wSecond);
/*
Generator radom des key [16bytes]
*/
/*
省略部分准备数据代码
*/
mWriter.WriteField(
12
,
6
, (BYTE
*
)szTime);
mWriter.WriteField(
13
,
4
, (BYTE
*
)szDate);
mWriter.WriteField(
37
,
12
, (BYTE
*
)szBatchTransNo);
mWriter.WriteField(
41
,
8
, (BYTE
*
)szDevNo);
mWriter.WriteField(
48
,
128
, (BYTE
*
)szUsers);
mWriter.WriteField(
55
,
132
, (BYTE
*
)szEncryptedDESKey);
mWriter.WriteField(
70
,
3
, (BYTE
*
)
"
929
"
);
nBufLen
=
mWriter.GetLength();
*
pBuf
=
new
BYTE[nBufLen];
assert(
*
pBuf
!=
NULL);
mWriter.CopyOut(
*
pBuf, nBufLen);
/*
Write Debug info
*/
#ifdef _DEBUG
mWriter.WriteToFile(XML_FILE_PATH, CISO8583::XML);
mWriter.WriteToFile(BIN_FILE_PATH, CISO8583::BIN);
mWriter.WriteToFile(HEX_FILE_PATH, CISO8583::HEX);
#endif
}
/*
读取报文
*/
void
Read8583Package(BYTE
*
pBuf)
{
CISO8583Reader mReader;
int
fields[]
=
{
12
,
13
,
37
,
41
,
48
,
55
,
70
};
int
nFields
=
sizeof
(fields)
/
sizeof
(
int
);
if
(
!
mReader.ReadMessage(pBuf))
throw
"
Read package failed
"
;
/*
检查域存在,如果所有域都存在,返回0。 该方法发现第一个不存在的域则返回该域索引
*/
assert(mReader.FieldsExist(fields, nFields)
==
0
);
/*
读取第六域
*/
BYTE sBuf[
256
];
memset(sBuf,
0
,
sizeof
(sBuf));
mReader.ReadField(
12
, sBuf,
6
);
/*
其他域读取相似
*/
/*
将报文写入文件
*/
mReader.WriteToFile(XML_FILE_PATH, CISO8583::XML);
mReader.WriteToFile(BIN_FILE_PATH, CISO8583::BIN);
mReader.WriteToFile(HEX_FILE_PATH, CISO8583::HEX);
}
void
Test()
{
BYTE
*
pBuf
=
NULL;
int
nLen;
Get8583Package(pBuf, nLen);
Read8583Package(pBuf);
}