Understanding of data storage in the byte sequence during communication between android java and windows C/C ++/QT

Source: Internet
Author: User

Ava: data is stored in large-end byte order. [the data is stored at a high address, the data is stored at a low address, and the data is stored at a high level before the array]

Windows (intel Platform): data is stored in a small byte order. [low-level data is stored at a low address, and high-level data is stored at a high address, high data is stored behind the array] (windows receives the short sent by java, int needs to call ntohs and ntohl to convert

Decimal side)
[Data High Point]: the high point of 0x1234 is 0x12
[Data low position]: the low position of 0x1234 is 0x34


For example, int ihex = 0x12345678;
Short shex = 0x1234;
Java memory storage:
Byte [] = {'0x12', '0x34', '0x56', '0x78 '}
Byte [] = {'0x12', '0x34 '}

Windows (intel cpu ):
C/c ++/qt storage is
The address of a character array increases progressively from the first address, that is, the larger the array subscript, the larger the memory address.
===== Char [] Front is a low address, followed by a high address
Char [] = {'0x78', '0x56', '0x34', '0x12'} // QByteArray and char [] are consistent
Char [] = {'0x34', '0x12 '}

When designing the java client program, we need to explicitly use the large-end byte order in the program to process int, short, long (strings do not need to be considered), that is, int, short, convert long to byte []. [Store the high positions of short and int in front of the character array

Surface] Of course, you can use a small byte to store the data. After receiving the data, the socket prints the data to see the 16-digit sequential number of int and short, and then processes the data.
The function is as follows: (the function for storing large-end bytecode)
/* Convert an integer to a character sequence. Similar functions can be found in baidu.
* Execute the bitwise operation to convert int I = 0x12345678 to binary:
* Memory storage: Low address
* 00010010 0x12
* 00110100 0x34
* 01010110 0x56
* 01111000 0x78
* High address
*/
// Store it at byte [] = {'0x12', '0x34', '0x56', '0x78 '}
Public static byte [] intToBytes (int I)
{
// Bytes [0] = 00010010
// Bytes [1] = 00110100
// Bytes [2] = 01010110
// Bytes [3] = 01111000
Byte bytes [] = new byte [4];
For (int j = 3; j> = 0; j --)
Bytes [3-j] = (byte) (I> 8 * (3-j) & 0xff); // bytes [0]: directly reduce the 8bits, 0xff, and operation of I. At this time, j = 3

Return bytes;
}

Public static byte [] bytes tobytes (short s)
{
Byte bytes [] = new byte [2];

Bytes [0] = (byte) (0xFF & (s> 8); // The low address stores high data, and the low data is deleted after 8 bits is shifted to the right. (Returns an 8-bit high value)
Bytes [1] = (byte) (0xFF & s); // high address stores low-level data. Bit operations are of course performed and operated at low levels. Currently, only 8 bits and 8 bits are returned)

Return bytes;
}

Related Article

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.