C代码在查找上与C ++表示不同

时间:2014-07-10 15:31:59

标签: c++ c

我有以下代码块(不是由我编写的),它执行映射并将ASCII字符重新编码为EBCDIC。

// Variables.
CodeHeader* tchpLoc = {};
...
memset(tchpLoc->m_ucpEBCDCMap, 0xff, 256);
for (i = 0; i < 256; i++) {
    if (tchpLoc->m_ucpASCIIMap[i] != 0xff) {
        ucTmp2 = i;
        asc2ebn(&ucTmp1, &ucTmp2, 1);
        tchpLoc->m_ucpEBCDCMap[ucTmp1] = tchpLoc->m_ucpASCIIMap[i];
    }
}

CodeHeader定义是

typedef struct {
    ...
    UCHAR* m_ucpASCIIMap; 
    UCHAR* m_ucpEBCDCMap; 
} CodeHeader;

和似乎给我带来问题的方法是

void asc2ebn(char* szTo, char* szFrom, int nChrs)
{
    while (nChrs--)
        *szTo++ = ucpAtoe[(*szFrom++) & 0xff];
}

[注意,unsigned char数组ucpAtoe[256]会在问题末尾复制以供参考]。

现在,我有一个旧的C应用程序和我的C ++ 11转换并行运行,这两个代码写了一个庞大的.bin文件,并且我有一个小的差异,我已经追溯到上面的代码。两个代码发生的是块

...
    if (tchpLoc->m_ucpASCIIMap[i] != 0xff) {
        ucTmp2 = i;
        asc2ebn(&ucTmp1, &ucTmp2, 1);
        tchpLoc->m_ucpEBCDCMap[ucTmp1] = tchpLoc->m_ucpASCIIMap[i];
    }

输入i = 32asc2ebn方法返回ucTmp164'@' ,用于C和C ++变体很棒。下一个条目是i = 48,对于此值,asc2ebn方法返回ucTmp1240'ð',C ++代码返回ucTmp1-16'ð'。我的问题是为什么这个查找/转换会为完全相同的输入和查找数组(下面复制)产生不同的结果?

在这种情况下,旧的C代码被认为是正确的,所以我希望C ++为这个查找/转换产生相同的结果。谢谢你的时间。


static UCHAR ucpAtoe[256] = {
    '\x00','\x01','\x02','\x03','\x37','\x2d','\x2e','\x2f',/*00-07*/
    '\x16','\x05','\x25','\x0b','\x0c','\x0d','\x0e','\x0f',/*08-0f*/
    '\x10','\x11','\x12','\xff','\x3c','\x3d','\x32','\xff',/*10-17*/
    '\x18','\x19','\x3f','\x27','\x22','\x1d','\x35','\x1f',/*18-1f*/
    '\x40','\x5a','\x7f','\x7b','\x5b','\x6c','\x50','\xca',/*20-27*/
    '\x4d','\x5d','\x5c','\x4e','\x6b','\x60','\x4b','\x61',/*28-2f*/
    '\xf0','\xf1','\xf2','\xf3','\xf4','\xf5','\xf6','\xf7',/*30-37*/
    '\xf8','\xf9','\x7a','\x5e','\x4c','\x7e','\x6e','\x6f',/*38-3f*/
    '\x7c','\xc1','\xc2','\xc3','\xc4','\xc5','\xc6','\xc7',/*40-47*/
    '\xc8','\xc9','\xd1','\xd2','\xd3','\xd4','\xd5','\xd6',/*48-4f*/
    '\xd7','\xd8','\xd9','\xe2','\xe3','\xe4','\xe5','\xe6',/*50-57*/
    '\xe7','\xe8','\xe9','\xad','\xe0','\xbd','\xff','\x6d',/*58-5f*/
    '\x79','\x81','\x82','\x83','\x84','\x85','\x86','\x87',/*60-67*/
    '\x88','\x89','\x91','\x92','\x93','\x94','\x95','\x96',/*68-6f*/
    '\x97','\x98','\x99','\xa2','\xa3','\xa4','\xa5','\xa6',/*70-77*/
    '\xa7','\xa8','\xa9','\xc0','\x6a','\xd0','\xa1','\xff',/*78-7f*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff',/*80-87*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff',/*88-8f*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff',/*90-97*/
    '\xff','\xff','\xff','\x4a','\xff','\xff','\xff','\xff',/*98-9f*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff',/*a0-a7*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff',/*a8-af*/
    '\xff','\xff','\xff','\x4f','\xff','\xff','\xff','\xff',/*b0-b7*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff',/*b8-bf*/
    '\xff','\xff','\xff','\xff','\xff','\x8f','\xff','\xff',/*c0-c7*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff',/*c8-cf*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff',/*d0-d7*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff',/*d8-df*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff',/*e0-e7*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff',/*e8-ef*/
    '\xff','\xff','\xff','\x8c','\xff','\xff','\xff','\xff',/*f0-f7*/
    '\xff','\xff','\xff','\xff','\xff','\xff','\xff','\xff' };

1 个答案:

答案 0 :(得分:2)

在C和C ++中,标准不要求charsignedunsigned类型。它的实现已定义,显然,您的C编译器将char确定为unsigned char,而您的C ++编译器将其确定为signed char

对于GCC,charunsigned char的标记为-funsigned-char。对于MSVC,它是/J