c++到Java的按位等效
C++ to Java Bitwise equivalent
我正在尝试将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
我在这里看到了多个独立的问题:
-
数组索引总是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
-
字符串不能作为数组直接访问——你需要先将其转换为数组或者使用访问器:
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
-
在
char
和int
之间的操作结果是int
-如果想将其存储为char
,则需要显式强制类型转换:char c = 'h'; char var1 = c & 0x00FF; //Error: Type mismatch: cannot convert from int to char char var2 = (char) (c & 0x00FF); //works
相关文章:
- 在java中解决这段代码时面临循环中的问题
- 尝试用java代码编译和运行c++代码
- 在这种情况下,java对象是否可以调用本机函数
- 在java中读取c++字节的位字段
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 使用已使用 java 编码的 openssl 解码数据
- SWIG Java 在使用 -DSWIGWORDSIZE64 时将int64_t转换为 jlong
- Android Java USB for native cpp
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- C++ equivalent to Java Map getOrDefault?
- C++ 中的 Java 样式枚举
- 是否有技术原因阻止 Java 中的 final C++ 像 const 一样严格?
- 加密在 Windows、C++ 和 Java 中传输中的数据
- Java从C++回调到C++回调
- 在 c++ 中模拟输入并在 JAVA 中读取它?
- 用C++包装 Java 库 (JNI)
- 使用 TreeSet Java 对反转进行计数
- 验证openssl c++中的签名,这是由JAVA DSA签名的?
- 如何通过 JNI 将 C 字符串表情符号传递给 Java
- 如何从保存在 Java 中C++的字节数组中读取数字?