7-bit compression coding length calculation method (C Programming)

Source: Internet
Author: User

Snail bait 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height = "19" alt = "" src = "http://www.bkjia.com/uploads/allimg/131228/1U63a009-0.gif" width = "19" border = "0"/>] Everybody knows, ASCII (excluding extended ASCII) the maximum bit is 0. Therefore, we can use this space to store data. This is what we do with "7-bit compression encoding. For details about the 7-bit compression algorithm, you can find Simple on the Internet ). Here, I will talk about my own C language implementation method for calculating the length of 7-bit encoding. There should be a better way ~)1. If a 7-bit encoded string is given, the decoded length is calculated without decoding, that is, the source code (ASCII) length.PSrc -- encoded string address
NSrc -- length calculated by using the strlen (pSrc) Function
NLength -- the length of the original encoding (the result we want to know)
If (nSrc % 7 = 0)
{
If (* (pSrc + nSrc-1)> 1)
NLength = (nSrc/7) * 8;
Else
NLength = (nSrc/7) * 8-1;
}
Else
{
If (* (pSrc + nSrc-1)> (8-nSrc % 7 ))
NLength = (nSrc/7) * 8 + nSrc % 7 + 1;
Else
NLength = (nSrc/7) * 8 + nSrc % 7;
} Explanation:
The first one checks whether the encoded length is a multiple of 7. We all know that 7-bit compression encoding can compress 8-byte ASCII into 7 bytes. If the encoded length is exactly a multiple of 7, it is very likely that the source code is a multiple of 8. It can only be said "very likely" here, Because 8 * n-1 ASCII encoding may also occupy 7 * n Bytes or 7 * n-1 bytes ). That is to say, 160 ASCII encoding is followed by 140 bytes, and 159 ASCII encoding is followed by 140 bytes or 139 bytes). Now we know that the encoding is 140 bytes, is the original ASCII code 160 or 159?
If (* (pSrc + nSrc-1)> 1) made a judgment, if the result is true, then 8 * n original codes, if false, 8 * n-1 original codes. Here, I believe you will understand it after a bit of thinking.) Hey, if you don't understand it, ask me via email ~

If the length after encoding is not a multiple of 7, it is slightly more complicated to judge the last shift. The truth is the same as above, but the number of shifts is different. When calculating the length, first extract the 8-bit ASCII integer multiple (nSrc/7) * 8, then take the remainder nSrc % 7, and then judge the result based on the shift of the last digit, determine whether or not you have to add one digit. (Why? Why not subtract one digit from the integer above 7? Haha ~ Think about it)
2. If the original ASCII string is given, the length after 7-bit encoding is calculated without encoding.PSrc -- source ASCII string address
NSrc -- length calculated by using the strlen (pSrc) Function
NLength -- the length after 7BIT encoding (the result we want to know) if (nSrc % 8 = 0)
{
NLength = (nSrc/8) * 7;
}
Else
{
If (* (pSrc + nSrc-1)> nSrc % 8-1)
NLength = (nSrc/8) * 7 + (nSrc % 8) * 7)/8 + 1;
Else
NLength = (nSrc/8) * 7 + (nSrc % 8) * 7)/8;
} Explanation:
If the length is an integer multiple of 8, it is very simple. If not, you have to determine the last shift. The number of digits to be shifted is nSrc % 8-1, if there is still "surplus" data after the shift, then we will add one more bit :)

Well, let's talk about it. If you have any questions, find something wrong, or have a better computing method, you can discuss it together!By the way, when 7-bit encoding is used to send text messages, if all the text messages are in ASCII format, the 7-bit encoding will be used to compress 160 ASCII codes into 140 bytes for transmission. :)Bytes ------------------------------------------------------------------------------------------Zhao XiaoqianQQ: 755721501E-mail: snailwarrior@qq.comSeeking for survival and development in constant dedication, creating your own excellent quality, and using the most essential and touching aspect of human nature as "marketing" yourself!

This article from the "Little snail tech home" blog, please be sure to keep this source http://snailwarrior.blog.51cto.com/680306/153333

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.