C ++到Java Bitwise等价

时间:2016-10-06 16:07:13

标签: java c++ bit-manipulation

我正在尝试将C / C ++类转换为Java等价物。到目前为止它还可以,但我已经得到了这些代码,Java正在抱怨。

// Isolate each byte in turn
cRecByte = lpszRec[ lRecByteCount++ ] & 0x00FF;

cRecByte是char

lpszRec是LPCSTR

lRecByteCount是long

这适用于C ++代码,但不适用于Java。

在我的Java代码中,我有完全相同的行,但有以下差异

cRecByte是char

lpszRec是String

lRecByteCount是long

我得到的错误(正如我所料)是

 Type mismatch: cannot convert from int to char
 Type mismatch: cannot convert from long to int

对于某些上下文,我正在尝试重新编写/模拟20世纪80年代编写的Hash Totaling函数,该函数至今仍被我们的遗留系统使用。用新东西替换它们将花费开发/测试成本上的财富,所以我必须继续沿着这条道路前进。

我提出的这段代码旨在将每个字符与给定记录(文本行)分开。

任何帮助都会非常感激。

请求的一些代码 - 这是来自C ++文件。因为实际的课程非常庞大,所以我已经走到了最远的地方。

void CHashTotalFile::CalcRecHashTotal( /*[in]*/     LPCSTR lpszRec,
                                       /*[in]*/     DWORD dwRecLen, 
                                       /*[in]*/     long lIteration, 
                                       /*[in,out]*/ UINT64& u64HashTotal,
                                                    SYSTYPE stType ) throw()
{
    char    cRecByte;
    LPCSTR  szNullReplacer = "CALCULATE HASH TOTAL";

    const static int  nLenNullReplacer = lstrlenA( szNullReplacer );
    const static char szRandomMap[] = { 17,
                                        5,
                                        37,
                                        31,
                                        53,
                                        19,
                                        41,
                                        7,
                                        11,
                                        2,
                                        23,
                                        3,
                                        29,
                                        47,
                                        43,
                                        13 };

    // 64bit unsigned integer data types:
    UINT64  u64ByteValue;
    UINT64  u64Carry;

    // long data types:
    long    lByteWord;      // Used in u64ByteValue & offset to random map
    long    lRecByteCount;  // Byte count within (actual) record  - used as subscript to data
    long    lRecByteIndex;  // Byte count within (logical) record - used in hashing calculations

    // int data types:
    int     nAttempts;
    int     nByteDistance;  // 'random' distance which rotates (right) the hash total
    int     nHashDistance;  // 'random distance which rotates (left) the hash total
    int     nWordValue;     // From data - offset to random map for hash distance
    bool    bGenerated = false;

    // No exception calls or error logging here as efficiency is the name of the game!
    // (or not so much inefficiency!)

    // If text & this is a blank line (i.e. \n) set to record length to zero
    if( m_fText && lpszRec[0] == NEWLINE_CHARACTER  )
        dwRecLen = 0;

    // use dummy string where no data
    if( dwRecLen == 0 )
    {
        lpszRec = szNullReplacer;
        dwRecLen = nLenNullReplacer;
    }

    for( nAttempts = 0; (nAttempts <= MAX_HASH_ATTEMPTS) && !bGenerated; nAttempts++ )
    {
        // Loop around every byte in the record
        for( lRecByteCount = lRecByteIndex = 0L; lRecByteCount < dwRecLen; lRecByteIndex++ )
        {
            // Isolate each byte in turn
            cRecByte = lpszRec[ lRecByteCount++ ] & 0x00FF;

            if( m_fText )   // If text

1 个答案:

答案 0 :(得分:1)

我在这里看到了多个不同的问题:

  1. 数组索引总是int

    long l = 0;
    int[] i = new int[] { 1, 2, 3 };
    int res1 = i[l]; //Error: Type mismatch: cannot convert from long to int
    int res2 = i[(int)l]; //works
    
  2. 字符串无法直接作为数组访问 - 您需要先将其转换为数组或使用访问器:

    String testString = "TESTSTRING";
    char c1 = testString[0]; //Error: The type of the expression must be an array type but it resolved to String  
    char c2 = testString.charAt(0); //works
    
  3. charint之间的操作结果是int - 如果要将其存储为char,则需要明确强制转换}:

    char c = 'h';
    char var1 = c & 0x00FF; //Error: Type mismatch: cannot convert from int to char
    char var2 = (char) (c & 0x00FF); //works