Methods 1, 2, 3 for converting. Net cold knowledge UInt32 to Byte

Source: Internet
Author: User

God, you win. It's cool and cool. Recently I have learned a wealth of medical experience for various reasons (well, it's just as meaningless as this blog post Get★Daze ).

The content of this blog post is the result of the wide opening of the brain during the shower last week. As a result, the original (dong) of each (lan) type (de) has been delayed for today due to (shou, then we will give you a gift for Women's Day (<yunω success), and thank you.

Today we will discuss how many boring means are there to split a UInt32 into four bytes. As for the dismantling of other length shapers, please do it yourself.

First of all, it must be the first thought, that is, bitwise operations. Nothing to say, close the door and put the code:

 p1=()(ipt >>  p2=()(ipt >>  p3=()(ipt >>  p4=()(ipt);

The above code gets four bytes from high to low by displacement, which is simple and practical. This method is usually enough (the only problem is that you need to calculate the displacement ...... Annoying ). However, the titles are spoiler 1, 2, and 3. At this time, giving up the blog is over. So, if I haven't been frozen to death, go to method 2.

The idea of method 2 is very direct: since a UInt32 is allocated with four bytes in the memory, why don't we directly access these four bytes? Just like the toast, just divide four slices equally.

To divide a UInt32 equally, You Need To directly operate the memory. In C #, except for a few special points, no one usually needs pointers. However, since we are playing with the brain hole, we should choose the pointer we haven't seen for a long time:

* p = (*)& p1=p[ p2=p[ p3=p[ p4=p[];

Because the X86 platform uses the Little Ending layout, the memory access sequence here is opposite to the intuition (well, in fact, the memory layout problem is forgotten, if you don't see the result, you won't go over the WIKI ). Don't be so concerned about small details. Let's talk about the next speed.

Method 3 is actually a variant of method 2. Since pointers can be used to access each byte, what other methods besides pointers can achieve the same effect?

C contains a special type called union. The magic effect of this type is that its fields share a memory location, that is to say, a UInt32 can overlap with four bytes in the memory. If we can use this method, doesn't it mean we can directly read the split results? So the only problem is that the shared object has never been seen in C? No such thing as union?

Actually, this stuff exists. Except for calling P/Invoke, it is too useless. In fact, StructLayoutAttribute is used to specify the manual memory layout, and FieldOffsetAttribute is used to manually overlap the memory layout. The Code is as follows:

      

Then the next thing is simple, that is, directly reading the result after assigning values to the Source.

So this blog is over. Is there anything strange?

Yes?

No?

Yes?

Is this word count?

Isn't that word count?

......

......

......

......

Well, I didn't say that there are only three methods from the beginning of the title. There is at least one way I see someone else using this (but it should be meaningless for a low-water programmer ).

Method 0 (Yes, I just like starting from zero, you bite me): convert it into a string, split it, and then convert it back.

At first glance, we can see that the efficiency is very low, and it is also very troublesome to write ......

 temp=ipt.ToString( p1=.Parse(temp.Substring(,  p2=.Parse(temp.Substring(,  p3=.Parse(temp.Substring(,  p4=.Parse(temp.Substring(, ), NumberStyles.HexNumber);

What is the efficiency of the above methods? To tell the truth, apart from method 3, there must be one more object to be created, while method 0 is completely flawed. Method 1 is enough for a person who does not stop the drug (but I always think that method 3 has some miraculous effects ). Please download the code and run the test on your own.

Download Code

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.