In-depth understanding of byte-order and type conversion in C + +

Source: Internet
Author: User
If you take the byte-order, type-conversion alone, you may find it simple, but not in depth. If we put it together, maybe we can understand it very thoroughly.

On the basis of byte order and type conversion, if you do not understand, you can refer to the following blog:

https://my.oschina.net/u/1783725/blog/647973 size byte order

https://my.oschina.net/u/1783725/blog/700970 Type Conversions

Before getting to the point, in a wordy sentence:

The number of system bits varies by byte size of the corresponding data type

A detailed description of the relationship between the two

BYTE order: The operating memory is the rule that stores the data we see in memory.

Big-endian: Big end effective, high-level data into the low-address memory, lower data into the high-address memory; Small byte order: Small end is effective, low-level data is placed in the lower address memory, high-bit data is put into the higher address memory

Manipulating memory (for example: memcpy) requires a byte-order to be considered

Type conversion: The action is to read the data, that is, the data read from memory, according to the size of the type byte conversion.

The number of bits converted to a small number of bits (truncation of high-level data, leaving the status of the data), small number of bits converted to more than the number of bits (the high-level data to fill 0).

An example to read them

1, assignment is independent of byte order, operation memory (memcpy) need to consider byte order

1: Assigns a unsigned long long 0xabcdef1234 to a variable of type unsigned long, regardless of the byte order

2:unsigned a long long type of 0xabcdef1234 variable using memcpy to short type, found to be 0, related to the byte order

The code is as follows:

#include <stdio.h>int main (int argc, char *argv[]) {unsigned long long Ullvar = 0xabcd1234;unsigned long ulVar1 = 0;u nsigned long ulVar2 = 0;/*memcpy operation memory, you need to consider the size of the system byte order, if it is a large byte order, high-level data is saved at the low address */memcpy (&ulvar1,&ullvar,sizeof ( ULVAR1);/* When assigning a value, the read data is converted according to the size of the data type to be assigned */ULVAR2 = ullvar;printf ("ulvar1=%x      ulvar2=%x \ n", ulvar1,ulvar2); Output: ulvar1=0      Ulvar2=abcd1234return 0;}
  • 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.