c++和java之间的数据转换

Converting C++ Unsigned Numbers to Java

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:

Size1 byte2 bytes4 bytes8 bytes16 bytes
8 bits16 bits32 bits64 bits128 bits
C++ (on Win32)

byte

shortint/long

long long

__int128
Java

byte

shortint

long

BigInteger

If 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:

Size1 byte2 bytes4 bytes8 bytes16 bytes
8 bits16 bits32 bits64 bits128 bits
C++ (on Win32)char/bool floatdouble/long double 
Javabooleanchar floatdouble

Going from C++ to Java:

C++ TypeJava Type Read from File **

Then translate the value

unsigned byte(signed) shortbyte a = in.readByte();short b = (short)(a & 0xff);
unsigned short(signed) intshort a = in.readShort(); int b = a & 0xffff;
unsigned int/long(signed) longint 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. UseFloat.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)

?
1
2
3
4
5
6
7
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:

?
1
2
byte a = inputStream.readByte();
short b = ( short )(a & 0xff );

 另附一排关于此主题的文章

Java和C/C++程序实时通讯数据移植问题的研究


点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值