连接两个Base64编码的字符串 [英] Concatenate two Base64 encoded strings

查看:787
本文介绍了连接两个Base64编码的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解码两个Base64编码的字符串,并将它们组合成一个128位的字符串.我能够解码Base64编码的字符串.有人可以指导我如何组合这两个解码的字符串吗?

I want to decode two Base64 encoded strings and combine them to make one 128 bit string. I am able to decode the Base64 encoded strings. Can some one guide me on how to combine these two decoded strings?

这是我用来解码两个编码字符串的代码.

This is the code I used for decoding the two encoded strings.

NSData *decodedData_contentKey = [[NSData alloc] initWithBase64EncodedString:str_content options:0];
NSString *decodedString_contentKey = [[NSString alloc] initWithData:decodedData_contentKey encoding:NSUTF8StringEncoding];
NSLog(@"%@", decodedString_contentKey); 

谢谢.

推荐答案

Base 64是八位字节/字节到字符/文本的静态大小编码:字节的6位表示为可打印的ASCII字符.因此,名称为:2 ^ 6 = 64,它使用64个字符的字母对二进制数据进行编码(+加上分隔符:'=',其中不包含编码位).

Base 64 is a statically sized encoding of octets/bytes into characters/text: 6 bits of a byte are represented as a printable ASCII character. Hence the name: 2^6 = 64, it uses a alphabet of 64 characters to encode the binary data (+ plus a delimiter character: '=' that does not contain encoded bits).

UTF-8是字符编码.它用于以八位字节编码字符. 所以字符编码的工作方式相反.实际上,您要做的是从字节中解码字符. UTF-8不使用128位值,也不是静态大小的.多个字节可用于表示一个字符.如果遇到未合并为有效字符编码的一个或多个八位字节,则可能会失败.

UTF-8 - used in your sample code - on the other hand is a character-encoding. It is used to encode characters in octets. So character encoding works the other way around. What you are actually doing is to decode the characters back from the bytes. UTF-8 does not use 128 bit values, nor does is it statically sized; multiple bytes may be used to represent one character. It will likely fail when it comes across an octet or octets that do not combine into a valid character encoding.

没有没有诸如base 128编码之类的东西.请考虑一下您要完成的工作,并提出一个新问题,如果您遇到困难,我们可以对其进行解码.

There is no such thing as base 128 encoding. Please think of what you are trying to accomplish and ask a new question that we can decode, if you get stuck.

猜测的答案:

Base 64编码将为每6个字节输出64位(8个字节)的ASCII文本.因此,如果要输出128位(16字节)的编码,则只需输入12个字节.但是随着base 64编码在每个4个字符边界处重新开始(4 * 8 = 32位编码,每个8位字符代表6位,4 * 6 = 24位数据,24位是3个字节->每个4个字符串精确地保留3个字节的输入),您可以简单地将两个基本的64个字符串连接起来而无需解码.

Base 64 encoding will output 64 bits (8 bytes) of ASCII text for each 6 bytes. Therefore, if you want 128 bit (16 bytes) of encoding output, you simply have to input 12 bytes. As the base 64 encoding restarts at each 4 character boundary however (4 * 8 = 32 bits of encoding, each 8 bit character represents 6 bits, 4 * 6 = 24 bits of data, 24 bits is 3 bytes -> each 4 character string holds precisely 3 bytes of input), you can simply concatenate the two base 64 strings without decoding.

这篇关于连接两个Base64编码的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆