In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to get each byte of integers in C++. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Topic: get each byte of 0x12345678.
Method 1: structure & Joint # include
Typedef unsigned int uint32_t
Typedef unsigned char uint8_t
Union bit32_data
{
Uint32_t data
Struct
{
Uint8_t byte0
Uint8_t byte1
Uint8_t byte2
Uint8_t byte3
} byte
}
Int main (void)
{
Union bit32_data num
Num.data = 0x12345678
Printf ("byte0 = 0x%x\ n", num.byte.byte0)
Printf ("byte1 = 0x%x\ n", num.byte.byte1)
Printf ("byte2 = 0x%x\ n", num.byte.byte2)
Printf ("byte3 = 0x%x\ n", num.byte.byte3)
Return 0
}
Running result:
The disadvantage of this method is that it will be affected by the size of the end. It can only be used slightly if the size of the end is clear. The above is the result of running in small-end mode.
The structure-federation method is not only useful in obtaining each byte of data, but also useful in data protocol, data bit splitting, etc., and then share it in more detail.
Method 2: shift operation # include
# define GET_LOW_BYTE0 (x) ((x > > 0) & 0x000000ff) / * get the 0th byte * /
# define GET_LOW_BYTE1 (x) ((x > 8) & 0x000000ff) / * get the first byte * /
# define GET_LOW_BYTE2 (x) ((x > > 16) & 0x000000ff) / * get the second byte * /
# define GET_LOW_BYTE3 (x) ((x > 24) & 0x000000ff) / * get the third byte * /
Int main (void)
{
Unsigned int a = 0x12345678
Printf ("byte0 = 0x%x\ n", GET_LOW_BYTE0 (a))
Printf ("byte1 = 0x%x\ n", GET_LOW_BYTE1 (a))
Printf ("byte2 = 0x%x\ n", GET_LOW_BYTE2 (a))
Printf ("byte3 = 0x%x\ n", GET_LOW_BYTE3 (a))
Return 0
}
Running result:
This is also the most common and effective way to get each byte of data. This similar bit operation is widely used in embedded systems, such as representing pixel color values in LCD operations, FLASH operations, and so on.
This is the end of the article on "how to get integer bytes in C++". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.