Programming 版 (精华区)
发信人: jiaxuan (笑十子), 信区: Programming
标 题: Re: 一个自由打印函数,欢迎测试!--给你一个例子
发信站: 哈工大紫丁香 (Tue May 27 01:46:27 2003) , 转信
【 在 fib (May Flower) 的大作中提到: 】
: 明白了,你这个主要用于print结构体的吧?
: 但是我看你把重点放在数据格式处理上了,自己处理的优点是什么?
/*--------------------------------------------------------------------------*/
/* zprintf函数使用范例 */
/*--------------------------------------------------------------------------*/
/* History : */
/* Date Version Person Activies */
/* 2003.05.26 1.0.0 RZ.Tian Create */
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* 普通数据类型定义 */
/*--------------------------------------------------------------------------*/
#define LONG long
#define BYTE unsigned char
#define DWORD unsigned long
#define ULONG unsigned long
#define USHORT unsigned short
#define WORD unsigned short
#define SHORT short
#define NULL 0
extern ULONG zprintf(BYTE *pbBuf, char *pbFmt, void *pbDataAddr);
extern ULONG vzprintf(BYTE *pbBuf, char *pbFmt, ...);
typedef struct
{
BYTE bVar1;
WORD wVar2;
DWORD dwVar3;
BYTE bVar4;
} StrucTest_T;
typedef struct
{
DWORD bt1 : 3;
DWORD bt2 : 3;
DWORD bt3 : 3;
DWORD bt4 : 3;
DWORD bt5 : 4;
} bitTakeTest_T;
typedef struct
{
BYTE btVar1 : 2;
BYTE btVar2 : 7;
BYTE bVar3;
WORD wVar4;
} MissTest_T;
typedef BYTE mIMSI_T[8];
#define PleaseWaitAndLook_M (i = 1)
void main(void)
{
BYTE baBuff[1600];
char cData[5] = "0123";
int iData[10] = {1, 2, 3, 4, 5}, i;
double daArray[10] = {1.0, 2.0, 3.0, 4.0, 5.0};
float faArray[10] = {1.0, 2.0, 3.0, 4.0, 5.0};
mIMSI_T tImsi;
DWORD dwIp = 0xa8020b11;
StrucTest_T tTest1 = {1, 2, 3, 4};
bitTakeTest_T tTest2 = {1, 2, 3, 4, 5};
MissTest_T tTest3 = {1, 2, 3, 4};
tImsi[0]= 0x12, tImsi[1]= 0x34, tImsi[2]= 0x56, tImsi[3]= 0x78,
tImsi[4]= 0x90, tImsi[5]= 0x12, tImsi[6]= 0x34, tImsi[7]= 0x5f;
/*----------------------------------------------------------------------*/
/* 不妨碍原函数使用方法,其它功能类似测试 */
/*----------------------------------------------------------------------*/
vzprintf(baBuff, "你好!这里是中兴通讯!"); /* 请查看baBuff内容 */
PleaseWaitAndLook_M;
vzprintf(baBuff, "cData: {%d %d %d %d %d}", cData[0], cData[1], cData[2], cDa
ta[3], cData[4]);
PleaseWaitAndLook_M;
vzprintf(baBuff, "cData: %s", "this is a test");
PleaseWaitAndLook_M;
vzprintf(baBuff, "iData: {%d %d %d %d %d}", iData[0], iData[1], iData[2], iDa
ta[3], iData[4]);
PleaseWaitAndLook_M;
vzprintf(baBuff, "daArray: {%f %f %f %f %f}", daArray[0], daArray[1], daArray
[2], daArray[3], daArray[4]);
PleaseWaitAndLook_M;
vzprintf(baBuff, "faArray: {%f %f %f %f %f}", faArray[0], faArray[1], faArray
[2], faArray[3], faArray[4]);
PleaseWaitAndLook_M;
vzprintf(baBuff, "tTest1: {%d %d %d %d}", tTest1.bVar1, tTest1.wVar2, tTest1.
dwVar3, tTest1.bVar4);
PleaseWaitAndLook_M;
vzprintf(baBuff, "tTest2: {%d %d %d %d %d}", tTest2.bt1, tTest2.bt2, tTest2.b
t3, tTest2.bt4, tTest2.bt5);
PleaseWaitAndLook_M;
vzprintf(baBuff, "tTest3: {%d %d %d %d}", tTest3.btVar1, tTest3.btVar2, tTest
3.bVar3, tTest3.wVar4);
PleaseWaitAndLook_M;
/*----------------------------------------------------------------------*/
/* 新增 */
/*----------------------------------------------------------------------*/
vzprintf(baBuff, "IMSI %zM %+zM", tImsi, tImsi);
PleaseWaitAndLook_M;
vzprintf(baBuff, "IP %zI %+zI", dwIp, dwIp);
PleaseWaitAndLook_M;
/*----------------------------------------------------------------------*/
/* 通过取数定义打印,对于后台业务观察,不能传参数表的情况来说很实用哦 */
/*----------------------------------------------------------------------*/
zprintf(baBuff, "cData: {%1a %1a %1a %1a %1a}", cData);
PleaseWaitAndLook_M;
zprintf(baBuff, "cData: %as", cData);
PleaseWaitAndLook_M;
zprintf(baBuff, "iData: {%4a %4a %4a %4a %4a}", iData);
PleaseWaitAndLook_M;
zprintf(baBuff, "daArray: {%8af %8af %8af %8af %8af}", daArray);
PleaseWaitAndLook_M;
zprintf(baBuff, "faArray: {%4ae %4af %4af %4af %4af}", faArray);
PleaseWaitAndLook_M;
zprintf(baBuff, "tTest1: {%1a%1ak %2a %4a %1a}", &tTest1); /* 不知道VC是循何原
则分配内存的 */
PleaseWaitAndLook_M;
zprintf(baBuff, "tTest2: {%4.3a %.3a %.3a %.3a %.4a}", &tTest2);
PleaseWaitAndLook_M;
zprintf(baBuff, "tTest3: {%1.2a %.7a %1a%1ak %2a}", &tTest3);/* 不知道VC是循何
原则分配内存的 */
PleaseWaitAndLook_M;
/*----------------------------------------------------------------------*/
/* 由上可见,尽量使用四字节对齐的结构是有好处的,少用奇形怪状的结构 */
/*----------------------------------------------------------------------*/
zprintf(baBuff, "IMSI %azM", tImsi);
PleaseWaitAndLook_M;
zprintf(baBuff, "IP %zI", &dwIp);
PleaseWaitAndLook_M;
zprintf(baBuff, "%a+zI", &dwIp);
PleaseWaitAndLook_M;
zprintf(baBuff, "IP %8.3zI", &dwIp);
PleaseWaitAndLook_M;
zprintf(baBuff, "IP %16.4zI", &dwIp);
PleaseWaitAndLook_M;
zprintf(baBuff, "IP %#16.4zI", &dwIp);
PleaseWaitAndLook_M;
zprintf(baBuff, "IP %-20.3zI", &dwIp);
PleaseWaitAndLook_M;
}
--
※ 来源:.哈工大紫丁香 http://bbs.hit.edu.cn [FROM: 210.52.238.66]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:4.519毫秒