我想将一个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
你能发布[MCVE]所以我们不必从你的线索拼凑出一个呢? – khelwood
尝试从'long'转换为'int'? – erip
'lpszRec [']'不能使用字符串,请尝试使用'lpszRec.charAt(...)'或'lpszRec.toCharArray()[...]'(而使用'int'索引OFC)。但更好的是你可能想使用'lpszRec。getBytes(“你需要的任何编码”)[...]'。 – Thomas