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

What is the method of decomposing and merging 8-bit, 16-bit and 32-bit integers in C language programming?

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

Share

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

This article mainly explains "what is the method of decomposing and merging 8-bit, 16-bit and 32-bit integers in C language programming". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "what is the method of decomposing and merging 8-bit, 16-bit and 32-bit integers in C language programming?"

The exercise is carried out in the VC++6.0 programming environment, with the source program:

# include

# include "string.h"

Int main (int argc, char argv [])

{

Unsigned int Data_Uint32=0x12345678

Unsigned short int Data_Uint16_1,Data_Uint16_2

Unsigned char Data_Uint8_1,Data_Uint8_2,Data_Uint8_3,Data_Uint8_4

Struct StructByte8 {

Unsigned char Byte01:1

Unsigned char Byte02:1

Unsigned char Byte03:1

Unsigned char Byte04:1

Unsigned char Byte05:1

Unsigned char Byte06:1

Unsigned char Byte07:1

Unsigned char Byte08:1

} Test1

Unsigned char C1 [] = "A"

Unsigned short int p16 = (unsigned short int *) (& Data_Uint32); / / A pointer defining 16 bits forces the 32-bit address to 16-bit, and the high order discards the low bit.

Unsigned short int Data_Uint16_2p,Data_Uint16_1p

Printf ("32-bit integers: 0x%x\ n", Data_Uint32); printf ("- operate by pointer -\ n"); Data_Uint16_2p=* (unsigned short int *) p16; Data_Uint16_1p=* ((unsigned short int *) p16Secret1) Printf ("converted 16-bit integers: 0x% xjol 0x% x\ n", Data_Uint16_1p,Data_Uint16_2p); Data_Uint8_1= * (unsigned char *) p16; Data_Uint8_2= * ((unsigned char *) p16room1); Data_Uint8_3= * ((unsigned char *) p16room2); Data_Uint8_4= * ((unsigned char *) p16room3) Printf ("converted 8-bit integers: 0x% xMagol 0x% xMagol 0x% xMagne0x% x\ n", Data_Uint8_1,Data_Uint8_2,Data_Uint8_3,Data_Uint8_4); Data_Uint8_1=0;Data_Uint8_2=0;Data_Uint8_3=0;Data_Uint8_4=0 / / decompose the 32-bit integer into two 16-bit integers directly according to the pointer value / /, then take the low 16-bit / / forced conversion, and discard the high-bit 16-bit / / 32-bit to 16-bit Data_Uint16_1= (unsigned short int) (Data_Uint32 > > 16); Data_Uint16_2= (unsigned short int) Data_Uint32; / / 32-bit to 8-bit Data_Uint8_1= (unsigned char) (Data_Uint32 > > 24) Data_Uint8_2= (unsigned char) (Data_Uint32 > > 16); Data_Uint8_3= (unsigned char) (Data_Uint32 > > 8); Data_Uint8_4= (unsigned char) Data_Uint32; printf ("- by bit operation -\ n") Printf ("converted 16-bit integers: 0x% xMagne0x% x\ n", Data_Uint16_1,Data_Uint16_2); printf ("converted 8-bit integers: 0x% xmai 0x% xmao 0x% xmao 0x% x\ n", Data_Uint8_1,Data_Uint8_2,Data_Uint8_3,Data_Uint8_4) Printf ("- through structural operation -\ n"); printf ("character ASCII is 01000001, content after structure transformation\ n"); memcpy (& Test1, C1, sizeof (Test1)); printf ("Test1.Byte08=%d\ n", Test1.Byte08) Printf ("Test1.Byte07=%d\ n", Test1.Byte07); printf ("Test1.Byte06=%d\ n", Test1.Byte06); printf ("Test1.Byte05=%d\ n", Test1.Byte05); printf ("Test1.Byte04=%d\ n", Test1.Byte04); printf ("Test1.Byte03=%d\ n", Test1.Byte03); printf ("Test1.Byte02=%d\ n", Test1.Byte02) Printf ("Test1.Byte01=%d\ n", Test1.Byte01); Data_Uint8_1=0x89;Data_Uint8_2=0xAB;Data_Uint8_3=0xCD;Data_Uint8_4=0xEF; printf ("- merge by bit operation -\ n") Data_Uint16_1 = (unsigned short int) (8-bit integer converted by Data_Uint8_1: 0x69, data length: 1

Description: 8-bit forced to 32-bit, the previous system automatically added 24-bit 0-position 32-bit forced to 8-bit, the system only intercepted the last 8 bits.

Thank you for your reading, the above is the content of "what is the decomposition and merging method of 8-bit, 16-bit and 32-bit integers in C language programming?" after the study of this article, I believe you have a deeper understanding of what the decomposition and merging method of 8-bit, 16-bit and 32-bit integers in C language programming is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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