中关村电器城:一个关于BCB的问题?

来源:百度文库 编辑:杭州交通信息网 时间:2024/03/29 00:26:03
我在BCB5中放了3个控件,EDIT两个,一个BUTTON,这是自己写了一个算法函数

……ulong CRCBitReflect(ulong ulData, int nBits)……
这是控件代码:
Edit2->Text=CRCBitReflect(Edit1->Text);
运行后
产生错误如下:
[C++ Error] Unit1.cpp(23): E2034 Cannot convert 'AnsiString' to 'unsigned long'
[C++ Error] Unit1.cpp(23): E2342 Type mismatch in parameter 'ulData' (wanted 'unsigned long', got 'AnsiString')
[C++ Error] Unit1.cpp(23): E2193 Too few parameters in call to 'CRCBitReflect(unsigned long,int)'
如能解决问题,再送上100分!谢谢
谁能帮我写一个程序把这个地方搞通,也就是如何解决这个问题

#define CRC16_POLYNOMIAL 0x1021 // CRC_16校验方式的多项式.

typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
// typedef enum tagBoolean { FALSE, TRUE } bool;

ulong g_ulTable[256];

// CRC_16方式校验的初始化函数, 计算CRC_16余数表.
void CRC16Init(void)
{
uint nRemainder;
int n, m;
ulong *pulTable = g_ulTable;

for(n = 0; n < 256; n ++)
{
nRemainder = (uint)n << 8;
for(m = 8; m > 0; m --)
{
if(nRemainder & 0x8000)
{
nRemainder = (nRemainder << 1) ^ CRC16_POLYNOMIAL;
}
else
{
nRemainder = (nRemainder << 1);
}
}
*(pulTable + n) = nRemainder;
}
}

// 反转数据的比特位, 反转后MSB为1.
// 反转前: 1110100011101110 0010100111100000
// 反转后: 1111001010001110 1110001011100000
ulong CRCBitReflect(ulong ulData, int nBits)
{
ulong ulResult = 0x00000000L;
int n;

for(n = 0; n < nBits; n ++)
{
if(ulData & 0x00000001L)
{
ulResult |= (ulong)(1L << ((nBits - 1) - n));
}
ulData = (ulData >> 1);
}
return(ulResult);
}
// 以CRC_16方式计算一个数据块的CRC值.
// pucData - 待校验的数据块指针.
// nBytes - 数据块大小, 单位是字节.
// 返回值是无符号的长整型, 其中低16位有效.
ulong CRC16Calc(uchar *pucData, int nBytes)
{
uint nRemainder, nRet;
int n;
uchar index;
ulong *pulTable = g_ulTable;

nRemainder = 0x0000;
for(n = 0; n < nBytes; n ++)
{
index = (uchar)CRCBitReflect(*(pucData + n), 8) ^ (nRemainder >> 8);
nRemainder = (uint)*(pulTable + index) ^ (nRemainder << 8);
}
nRet = (uint)CRCBitReflect(nRemainder, 16) ^ 0x0000;
return(nRet);
}

程序就是这样的,是一个算CRC16的函数

错误一:Edit1->Text是个字符型的,你的转换成无符号长整型。你的CRCBitReflect函数参数定义为ulong ulData(无符号长整型)及int nBits(带符号整型),而你传递一个Edit1->Text(ANSI字符串)给它,它当然不知道怎么操作了。
错误二:由第一个问题引起
错误三:你调用参数数量不一致CRCBitReflect是2个参数,而你只给了一个参数。
你还是说说CRCBitReflect函数的作用,让人帮你改写一下吧。

你的CRCBitReflect函数是unsigned long类型
Edit2->Text是字符串型
要把CRCBitReflect的还回值先变成字符串再赋值