开发者

Convert NSString to binary string

开发者 https://www.devze.com 2023-04-06 14:55 出处:网络
I have a NSString representing sha1 hash of some data and I need to convert it to binary string, but cant find any good sol开发者_如何学Cution to do that. It should be equivalent to PHP\'s pack functi

I have a NSString representing sha1 hash of some data and I need to convert it to binary string, but cant find any good sol开发者_如何学Cution to do that. It should be equivalent to PHP's pack function.

The result should be like this: 0xb7, 0xc7, 0x91, ..., 0x90.

I than need to convert this binary to base64-encoded string.


You can easily to do so with a NSString+Base64 catalog. And the code should like this:

#import "NSString+base64.h"

//....
//some codes
//....
NSString *yourNSString = @"Some string you want to be encoded";
NSString *base64string = [yourNSString base64String];

Here is the Code of NSString+Base64.h file for example:

//
//  NSString+Base64.h
//  HudsonGrowl
//
//  Created by Benjamin Broll on 06.12.10.
//
//  This source code is licensed under the terms of the BSD license.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
//  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 
//  ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
//  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
//  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#import <Foundation/Foundation.h>


@interface NSString (Base64)

+ (NSString *) base64StringFromData: (NSData *)data;

- (NSString *)base64String;

@end

And the NSString+Base64.m file:

//
//  NSString+Base64.m
//  HudsonGrowl
//
//  Created by Benjamin Broll on 06.12.10.
//
//  This source code is licensed under the terms of the BSD license.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
//  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 
//  ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
//  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
//  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#import "NSString+Base64.h"


@implementation NSString (Base64)

+ (NSString *)encode:(const uint8_t *)input length:(NSInteger)length {
    // this code seems to be floating on a lot of stackoverflow posts.
    // i'm not aware of any license issues but let me know in case there are
    // problems including the source in this project.

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

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

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

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

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

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

+ (NSString *) base64StringFromData: (NSData *)data {
    return [self encode:data.bytes length:data.length];
}

- (NSString *)base64String {
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    return [NSString base64StringFromData:data];
}

@end
0

精彩评论

暂无评论...
验证码 换一张
取 消