ios NSString 的 Base64 编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13245300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:06:57  来源:igfitidea点击:

Base64 Encoding for NSString

iphoneiosbase64nsmutableurlrequest

提问by Cyril

I am trying to post the username and password to the server. I want to convert this username and password into Base64 Encoding. So this encoded string will be added for the Authorization field.

我正在尝试将用户名和密码发布到服务器。我想将此用户名和密码转换为 Base64 编码。因此,将为授权字段添加此编码字符串。

Is there any API already available to do the Base64 encoding in iOS or we have write on our own ??

是否有任何 API 可用于在 iOS 中进行 Base64 编码,或者我们自己编写?

采纳答案by Amit Hooda

How do I do base64 encoding on iphone-sdk?

如何在 iphone-sdk 上进行 base64 编码?

Have a look at this.

看看这个。

and then use these methods to make use of the above methods(given in link) Converting NSString to Base64 Data for XML Serializationtaken from above link.

然后使用这些方法来利用上述方法(在链接中给出) 将 NSString 转换为 Base64 数据以用于从上述链接中获取的 XML 序列化

+ (NSString *)toBase64String:(NSString *)string {
NSData *data = [string dataUsingEncoding: NSUnicodeStringEncoding];

NSString *ret = [NSStringUtil base64StringFromData:data length:[data length]];

return ret;
}

+ (NSString *)fromBase64String:(NSString *)string {
NSData  *base64Data = [NSStringUtil base64DataFromString:string];

NSString* decryptedStr = [[NSString alloc] initWithData:base64Data encoding:NSUnicodeStringEncoding];

return [decryptedStr autorelease];
}

回答by Rahul

This can be done by implementing a class method for base64 conversion the below code is for conversion..

这可以通过实现 base64 转换的类方法来完成,下面的代码用于转换..

+ (NSString*)base64forData:(NSData*)theData 
{
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}

and in your URL request add the following code for to sent the username and password for authorization...

并在您的 URL 请求中添加以下代码以发送用于授权的用户名和密码...

[request addValue:[NSString stringWithFormat:@"Basic %@",[className base64forData:[[NSString stringWithFormat:@"%@:%@",UsernameString,passwordString] dataUsingEncoding: NSUTF8StringEncoding]]] forHTTPHeaderField:@"Authorization"];

[请求 addValue:[NSString stringWithFormat:@"Basic %@",[className base64forData:[[NSString stringWithFormat:@"%@:%@",UsernameString,passwordString] dataUsingEncoding: NSUTF8StringEncoding]]] forHTTPHeaderField:@"Authorization"] ;