2011.5.18 Converting C++ Unsigned Numbers to Java
来源:互联网 发布:西科软件java 编辑:程序博客网 时间:2024/05/29 07:59
Converting C++ Unsigned Numbers to Java
原址:http://jwinblad.com/resources/unsignedtojava.html
Dealing with unsigned values from C++ can seem like a headache sometimes, if you don't understand what's going on "under the hood". Luckily you don't have to understand everything under the hood to read a binary file written by a C++ app in Java or vice versa, just have a vague understanding of the issues involved..
The major differences that can cause challenges are:
1) Figuring out which data types are "equivalent"
2) Java doesn't have unsigned types, but C++ types can be signed or unsigned.
3) Some types just don't have a direct equivalent (eg: C++ float has no exact match in Java)
Here's a table showing how internally C++ (on a 32-bit version of Windows) and Java store integer types:
Size 1 byte 2 bytes 4 bytes 8 bytes 16 bytes 8 bits 16 bits 32 bits 64 bits 128 bits C++ (on Win32)byte
shortint/longlong long
__int128Javabyte
shortint
long
BigIntegerIf you are using a 64 bit operating system or a 16 bit operating system, the C++ sizes such as how many bytes an int is will vary. Microsoft invented some funky looking type names like __int32 for those times when you want to tell the compiler "when I said I wanted 4 bytes for my integer, I meant 4 bytes", but that may only work with Microsoft compilers.
Here are some other binary data types that are frequently encountered:
Size 1 byte 2 bytes 4 bytes 8 bytes 16 bytes 8 bits 16 bits 32 bits 64 bits 128 bits C++ (on Win32) char/bool floatdouble/long double Javaboolean char float doubleGoing from C++ to Java:
C++ TypeJava Type Read from File **Then translate the value
byte a = in.readByte();
short b = (short)(a & 0xff);
unsigned short (signed) int short a = in.readShort();
int b = a & 0xffff;
unsigned int/long (signed) long int a = in.readInt();
long b = a & 0xffffffffL;
float* floatint a = in.readInt();
float b = Float.intBitsToFloat(a);
doublefloatfloat a = in.readFloat();
//doing nothing extra
*note: because Java does not have a 4 byte float, converting 4 byte c++ float to 8 byte java float and then back to 4 byte c++ float may result in rounding errors. Use Float.floatBitsToInt(floatValue);
to convert in the other direction.
** in refers to a binary input stream type of your choice.
To convert back and forth, you just cast in the to C++ direction. Easy.
Code example: (int or short could be used here just as well)
public static short toUnsigned(byte b) { return (short)(b & 0xff);}public static byte toSigned(short i) { return (byte) i; }
*C++ type sizes are assuming you are running a 32 bit version of Windows. You can always explicitly set your sizing by declaring the types as __int8 (unsigned __int8)
For a C++ unsigned byte (value 0 to 255), mask with 0xff and store the result in a short:
byte a = inputStream.readByte(); // assuming you have your stream already set up
short b = (short)(a & 0xff);
short c = inputStream.readShort();
另:
如:C++中的unsigned long,无符号的long型,如果是VC的话,应该是4个字节,只不过由于它是无符号的,所以它的补码就是原码,表示范围是0-2^32-1. 那么Java程序接收时,也会接收到一个4字节的东西.但是,由于这个是四字节,无符号.所以很有可能,它的第31位(第0位是最低位)是1,而Java接收到这个,如果给int型的话,Java的int就是有符号的,最高位是1,表示是负数,这样就产生了问题(当然,如果最高位不是1,就没有问题).现在假设这个值已经给一个int了
int a=...;//Java的a中已经接收了这个unsigned long,并且最高位是1,所以变成了负数
long b=a; //将a转换为long型,由于它是负数,所以它的符号位会填充到long的高32位中.你现在只需要做一件事情况:
b=(0xffffffff&b);//哈哈,这样就可以了
- 2011.5.18 Converting C++ Unsigned Numbers to Java
- converting unsigned char * to unsigned char**
- JavaScript:Converting Strings to Numbers
- Converting numbers to strings and strings to numbers
- Converting numbers to strings and strings to numbers
- C++: Converting an MFC CString to a std::string
- C++: Converting an MFC CString to a std::string
- Error:Error converting bytecode to dex:Cause:c
- Converting Drive Letters to MS-DOS INT 13H Disk Drive Numbers
- Java: Best way of converting List<Integer> to List<String>
- Converting DIB to DDB
- Converting DDB to DIB
- Converting CEDB to EDB
- Converting LPDWORD to DWORD
- Converting DDB to DIB
- Converting JSONarray to ArrayList
- 【C/C++】warning: ISO C++ forbids converting a string constant to 'char*'
- java中模拟c/c++中unsigned
- 运动物体检测——光流法
- 使用回车或换行分割字符串例子
- 随性
- Eclipse 插件安装与卸载的10个小窍门
- android 2.2及以上使用adb出现“sqlite3:not found”解决办法
- 2011.5.18 Converting C++ Unsigned Numbers to Java
- 菜单(1)
- 二值形态学——膨胀,腐蚀
- 通过Live Writer Blog客户端来写blog
- 软件测试工程师发展之路
- 背包算法(Java 实现)
- 如何彻底删除系统服务项
- 数据线和下载线
- 判断点在多边形内部