Binary processing tips (binary processing skills)

Source: Internet
Author: User
ArticleDirectory
    • 2.1 take a specific bit (mask + & +>)
    • 2.2 generate a specific bit (mask + | +>)
    • 2.4 binary Decoding

Recently I am working on protocol analysis (rtmp ),ProgramTo process binary data. I searched the internet and did not find a summary or description of the binary processing skills. I tried to summarize it myself.

1. Basics

Computers are basically transmitted and stored in binary format at the underlying layer. Similar to the following sequence:

0010 1100,000 1 1101

During use, these binary files are translated into "meaningful" formats, such as English letters. If 0 and 1 are directly used to display binary data, it will be too long and not very good. In the program, binary is usually represented in decimal or hexadecimal notation. For example, the preceding binary can be expressed:

44, 29 => decimal

0x2c, 0x1d => hexadecimal

Because every 8 bits is considered as one byte, that is, 1 byte = 8 bits. Bytes are the size and boundary of the storage transmitted by the computer, that is, the minimum unit we usually call. For this reason, we usually process, save, and transmit binary data in the program using 8bits.

In a computer, the hexadecimal and binary systems are naturally adapted. That is to say, every four binary systems (4 bits) exactly correspond to a hexadecimal number (from 0 ~ F ). Therefore, most programming languages directly support hexadecimal literal numbers. For example, in many languages, we can define byte (that is, two 4 bits) as follows ):

Buf [0] = 0x2c

We have said that each byte is 8 bits, which is expressed in decimal format as 0 ~ 255,Programming LanguageThere is no directly for the type to represent the number of this range, so we use the char type with the same size in C language, because the char type is also represented by 8 bits. But we usually process a string of binary, so we can also use int (4 bytes) as the unit size to represent binary, so that an int value can represent 4 bytes.

2, binary Algorithm

We usually need to process binary data:

1. Take one or more specified bits (this is often used in protocol analysis)

2. Generate a specific digit or Digit

2. binary encoding format for transmission and storage

3. decodes binary data into a specific format.

2.1 take a specific bit (mask + & +>)

Use a mask (mask) in a program to obtain a specific bit, for example, for the following binary:

0010 1100

If we start from the third place on the left, we first create the mask: 0010 0000, which is set to 0 except the third place. Then we can get a specific bit by bit and then combine the shift operation to get the result:

0010 11000010 0000 & -------------------- 0010 0000> 5 = 0000 0001

If we take the four digits in the middle, the mask is 0011 1100, and then:

 
0010 11000011 1100 & ------------- 0010 1100> 2 = 0000 1011
2.2 generate a specific bit (mask + | +>)

It is similar to generating specific bits, except that & is replaced with |. Because the "or operation" of 0 and 0 or 1 does not affect the original value. For example, we want to set the following binary value from 3 ~ 5 to 010:

0010 1100

Our algorithm is divided into two steps. The first step is to set 3 ~ For this reason, we generate a mask of 11 (00 0) 111, and then use ~ 5 position 0. After 0 is set, we use the mask 00 (01 0) 000 and

 
0010 11001100 0111 & => 0 mask in the middle position ----------------- 0000 0100 => 3 ~ Result of an intermediate bits operation mask ------------------- 00001 0000 0001 => 00 (01 0) 0100
2.3 binary encoding

Since we talk about encoding and decoding, an agreement must be reached between encoding and decoding. This protocol is usually called a code table. In the process of encoding and decoding, we need to check the code table for encoding and decoding. In fact, encoding is the middle ing, and common mappings include:

8 bits => char (ASCII)

32 bits, 4 bytes => unsigned integer (uint)

For example, we encode the following binary code:

0010 1100,010 1 1101,001 1 0001,011 0 1110

Result:

 
ASCII =>,] 1 nuint => 744305006
2.4 binary Decoding

Decoding is similar to encoding, and is a reverse operation of encoding.

3. program operation instance

To be added...

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.