gpt4 book ai didi

iphone - 将 NSString 转换为二进制字符串

转载 作者:行者123 更新时间:2023-11-28 23:13:05 25 4
gpt4 key购买 nike

我有一个 NSString 表示一些数据的 sha1 哈希,我需要将它转换为二进制字符串,但找不到任何好的解决方案来做到这一点。应该相当于PHP的pack函数。

结果应该是这样的:0xb7, 0xc7, 0x91, ..., 0x90。

我需要将此二进制文件转换为 base64 编码的字符串。

最佳答案

您可以使用 NSString+Base64 目录轻松地做到这一点。代码应该是这样的:

#import "NSString+base64.h"

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

例如NSString+Base64.h文件的代码:

//
// 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

和 NSString+Base64.m 文件:

//
// 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

关于iphone - 将 NSString 转换为二进制字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7519126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com