Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the Byte order of C _

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "C/C++ byte order example analysis". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian slowly and deeply to study and learn "C/C++ byte order example analysis" together.

endian

Recently, I have been looking at the memory encoding of redis, which involves byte order-related content. Here's a quick review.

Data is stored in memory in bytes, and if it is single-byte data (such as char, unsigned char, int8), there will be no byte order problem. But multi-byte data (int, float, double) has to consider byte order. There are two types of endian: big endian and small endian.

large endian order

The high byte of data is stored at the low end of the address; the low byte is stored at the high end of the address. The figure shows the memory arrangement of a four-byte integer with a value of 0x12345678 on a big-endian host.

microtelosequence

The high byte of data is stored at the high end of the address; the low byte is stored at the low end of the address. The figure shows the memory arrangement of a four-byte integer with a value of 0x12345678 on a small-endian host.

Host endian and network endian

In addition to host endian, there is network endian. Host endianness is determined by CPU, Intel Core has been tested to be little-endian. The network endian is big endian. Testing endian can be done with a C source code.

#include int main(int argc, char *argv[]) { int i; int x = 0x12345678; for (i = 0; i

< sizeof(int); ++i) { unsigned char *p = ((unsigned char *)(&x)) + i; unsigned char v = *p; printf("%p 0x%d%d\n", p, v>

>4, v & 0xf ); } return 0;}

The first address of integer x is converted into unsigned char* pointer and then shifted forward i units to obtain the address of this sizeof(int) bytes respectively, and then the value on each address is obtained with *, and converted into hexadecimal output through bit operation.

Linux system can get CPU type by instruction:

cat /proc/cpuinfo | grep name | cut -f2 -d: |uniq -c4 Intel(R) Core(TM) i3-2120 CPU@3.30GHz large endian and small endian interconversion

The conversion between big endian and small endian is actually a memory flip. When you know the byte number of an integer or a pointer, you are doing a mirror exchange. Here is an example of a 64-bit integer:

void memrev64(void *p) { unsigned char *x = p, t; t = x[0]; x[0] = x[7]; x[7] = t; t = x[1]; x[1] = x[6]; x[6] = t; t = x[2]; x[2] = x[5]; x[5] = t; t = x[3]; x[3] = x[4]; x[4] = t;} uint64_t intrev64(uint64_t v) { memrev64(&v); return v;}

64-bit integers have 8 bytes, so when converting byte order:

The 0th byte and the 7th byte are exchanged;

The 1st byte and the 6th byte are exchanged;

The 2nd byte and the 5th byte are exchanged;

The third byte and the fourth byte are exchanged;

For 32-bit integers and 16-bit integers, it is even simpler and will not be repeated.

Thank you for reading, the above is the "C/C++ byte order instance analysis" content, after the study of this article, I believe that we have a deeper understanding of C/C++ byte order instance analysis, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report