c++到Java的按位等效

C++ to Java Bitwise equivalent

本文关键字:Java c++      更新时间:2023-10-16

我正在尝试将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年代编写的哈希总计函数,该函数至今仍被我们的遗留系统使用。用新东西替换它们将花费大量的开发/测试成本,所以我必须继续沿着这条路走下去。

我写的这段代码是用来从给定的记录(文本行)中分离出每个字符的。

如有任何帮助,不胜感激。

一些代码的要求-这是来自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. 数组索引总是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