一个字符串基于base64方式实现的编码算法,用二进制的表达,采用C语言实现

Problem Description
Mike does not want others to view his messages, so he find a encode method Base64.

Here is an example of the note in Chinese Passport.

The Ministry of Foreign Affairs of the People's Republic of China requests all civil and military authorities of foreign countries to allow the bearer of this passport to pass freely and afford assistance in case of need.

When encoded by \texttt{Base64}, it looks as follows

VGhlIE1pbmlzdHJ5IG9mIEZvcmVpZ24gQWZmYWlycyBvZiB0aGUgUGVvcGxlJ3MgUmVwdWJsaWMgb2Yg
Q2hpbmEgcmVxdWVzdHMgYWxsIGNpdmlsIGFuZCBtaWxpdGFyeSBhdXRob3JpdGllcyBvZiBmb3JlaWdu
IGNvdW50cmllcyB0byBhbGxvdyB0aGUgYmVhcmVyIG9mIHRoaXMgcGFzc3BvcnQgdG8gcGFzcyBmcmVl
bHkgYW5kIGFmZm9yZCBhc3Npc3RhbmNlIGluIGNhc2Ugb2YgbmVlZC4=

In the above text, the encoded result of \texttt{The} is \texttt{VGhl}. Encoded in ASCII, the characters \texttt{T}, \texttt{h}, and \texttt{e} are stored as the bytes 84, 104, and 101, which are the 8-bit binary values 01010100, 01101000, and 01100101. These three values are joined together into a 24-bit string, producing 010101000110100001100101.
Groups of 6 bits (6 bits have a maximum of 26=64 different binary values) are converted into individual numbers from left to right (in this case, there are four numbers in a 24-bit string), which are then converted into their corresponding Base64 encoded characters. The Base64 index table is

0123456789012345678901234567890123456789012345678901234567890123
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

In the above example, the string 010101000110100001100101 is divided into four parts 010101, 000110, 100001 and 100101, and converted into integers 21,6,33 and 37. Then we find them in the table, and get V, G, h, l.

When the number of bytes to encode is not divisible by three (that is, if there are only one or two bytes of input for the last 24-bit block), then the following action is performed:

Add extra bytes with value zero so there are three bytes, and perform the conversion to base64. If there was only one significant input byte, only the first two base64 digits are picked (12 bits), and if there were two significant input bytes, the first three base64 digits are picked (18 bits). '=' characters are added to make the last block contain four base64 characters.

As a result, when the last group contains one bytes, the four least significant bits of the final 6-bit block are set to zero; and when the last group contains two bytes, the two least significant bits of the final 6-bit block are set to zero.

For example, base64(A) = QQ==, base64(AA) = QUE=.

Now, Mike want you to help him encode a string for k times. Can you help him?

For example, when we encode A for two times, we will get base64(base64(A)) = UVE9PQ==.

Input
The first line contains an integer T(T≤20) denoting the number of test cases.

In the following T lines, each line contains a case. In each case, there is a number k(1≤k≤5) and a string s. s only contains characters whose ASCII value are from 33 to 126(all visible characters). The length of s is no larger than 100.

Output
For each test case, output Case #t:, to represent this is t-th case. And then output the encoded string.

Sample Input
2
1 Mike
4 Mike

Sample Output
Case #1: TWlrZQ==
Case #2: Vmtaa2MyTnNjRkpRVkRBOQ==

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!

相似问题

3
java PDF 转二进制字符串
2
asp将字符串内容转换为2进制数据流,要实现字符串到2进制数据流的转换
1
vbscript将图片转换为base64字符串
4
关于html字符串拼接的问题
5
字符串编码的一个奇怪问题
99
只要回答,都给C币,请问怎么实现javascript对字符串和数字相互转换?
3
怎么样对一个遍历一个不规则字符串数组,找到每列中最长字符串的大小
2
Python如何判断一个字符串是否一个字典中存在的英文单词?
4
怎么javascrpt语言实现将一个字符串转换为多个字符串构成的数组并且判断每个字符串的数组?
2
字符串怎么在jacvscropt语言里实现字符串的判断
2
java中使用Base64的加密方法将byte[]加密成一个字符串的问题?
4
js怎么获取base64文件流?
0
h5的audio标签怎么播放音频文件的base64字符串
1
c# base64 文件流字符串 ,转成视频MP4 或者语音的方法
0
关于字符串编码和加密算法的一个问题,请问各位怎么采用C语言的实现?
0
一个有关于字符串加密编码的方式的问题,采用C语言编码字符串的实现
0
一个字符串的匹配算法采用KMP的实现,字符串匹配怎么实现啊,用C语言
0
一个利用字符串规则实现的快速求和怎么使用C语言的方式编程实现算法计算
0
字符串文本加密编码算法的实现过程,采用的是C语言的方式如何实现的?
0
给定一个长度为偶数位的0,1字符串,运用C编程语言的编程实现串的奇偶位互换。