[绍棠_Swift] Swift中使用Base64编解码

阎博易
2023-12-01

/// swift Base64处理

    /**

     *   编码

     */

    func base64Encoding(plainString:String)->String

    {

        

        let plainData = plainString.data(using: String.Encoding.utf8)

        let base64String = plainData?.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))

        return base64String!

    }

    

    /**

     *   解码

     */

    func base64Decoding(encodedString:String)->String

    {

        let decodedData = NSData(base64Encoded: encodedString, options: NSData.Base64DecodingOptions.init(rawValue: 0))

        let decodedString = NSString(data: decodedData! as Data, encoding: String.Encoding.utf8.rawValue)! as String

        return decodedString

    }

Objective-C 代码

    /**

     *  编码

     */

    + (NSString *)base64Encoding:(NSString *)plainString

    {

    NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];

    NSString *base64String = [plainData base64EncodedStringWithOptions:0];

    return base64String;

    }

    

    /**

     *   解码

     */

    

    + (NSString *)base64Decoding:(NSString *)encodedString

    {

    NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:encodedString options:0];

    NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];

    return decodedString;

    }


 类似资料: