2016-10-06 23 views
1

我想将一个C/C++类转换成Java等价物。到目前为止,它还行,但我需要这些代码,Java正在抱怨。C++到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 

对于某些上下文,我试图重写/模拟在1980年代编写的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 
+3

你能发布[MCVE]所以我们不必从你的线索拼凑出一个呢? – khelwood

+2

尝试从'long'转换为'int'? – erip

+5

'lpszRec [']'不能使用字符串,请尝试使用'lpszRec.charAt(...)'或'lpszRec.toCharArray()[...]'(而使用'int'索引OFC)。但更好的是你可能想使用'lpszRec。getBytes(“你需要的任何编码”)[...]'。 – Thomas

回答

1

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

  1. 数组索引总是诠释

    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 
    
+0

谢谢。好答案。为什么No 3中的错误在C中工作?这是我的困惑。我不知道将&0x00FF全部放在一起的后果。 – thonnor

+0

因为C++是具有不同规则的不同语言。 – Hulk

+0

是的,我明白了。但是因为我“移植到Java”的代码非常类似,所以我希望我坚持的这一点也是一样的。感谢你的帮助,你给了我一些很好的建议,我明天会去尝试。 – thonnor