Notes for Java and c Communication

Source: Internet
Author: User

1. little_endian

The web-based testing software consists of the C ++ data collection server program and the client Java display program. The former uses C ++ and the latter uses the Java language, and there is a data migration problem. Because
In a computer system, when a binary file containing numbers is moved from one structure to another, a large tail or a small tail problem occurs. There are two methods for storing different CPUs in multiple-node storage (such as four-byte INT ):
The method is called little_endian. The low bytes of data are placed at the top of the continuous storage area. The other method is big_endian.
The first place in the continued storage zone. The Intel 80*86 family processor is the last one that still sticks to the primary structure of tail. All other CPU Structures (Motorola
680 × 0 and all of them.) It is either a pure large tail or an adaptation to both the big tail and the small tail. The big tail is considered to be a more logical method ). When a number is written to a file by a tail processor
When the processor reads (or reverses) the number, it will be messed up (except 0 and-1 ).

Currently, in the project I participated in, the GM of the platform Center
Server is implemented in C language, while GM on our side
The client is implemented in Java. It is necessary to consider the large and small tail conversions during communication, mainly involving short, Int, long, string, and byte types.
Change.

2. Type bytes size issues

The basic types of C language are as follows:

 

Type

Definition

Description

Byte

Typedef
Unsigned char byte

Single byte

Word

Typedef
Unsigned short word

Double-byte unsigned integer

Sword

Typedef
Signed short sword

Double-byte signed integer

DWORD

Typedef
Unsigned int DWORD

Four-byte unsigned integer

Sdword

Typedef
Signed int sdword

Four-byte signed integer

 

The eight basic data types of Java are as follows:

Byte 1 byte
Short
2 bytes
Int 4 bytes

Long
8 bytes (4 bytes in C)
2 bytes of char (1 byte in C)
Float
4 bytes
Double 8 bytes
Boolean bool;
False/true

 

3. Integration:

Examples of big tail and Small Tail conversion:

For example, there is an int of the Java type in hexadecimal notation.

00 00 80 01 (large tail)

To convert to C:

01 80 00 00 (Small Tail)

For example, the Short Type

40 02 (large tail)

02 40 (Small Tail)

For example, the Mina Communication Framework provides ready-made APIs for big-tail conversion.

Iobuffer. Order (byteorder.Little_endian
) // Converted to a small tail

Iobuffer. Order (byteorder.Big_endian
) // Converts it to a big tail

You can use byte [] to flip an array.
Arrayutils. Reverse (byte [])

To complete the conversion.

References:

Research on Real-time Data Migration of Java and C/C ++ programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.