我正在寻找一种很好的可可方法来将NSData对象序列化为十六进制字符串。我们的想法是将用于通知的deviceToken序列化,然后再将其发送到我的服务器。
我有以下实现,但我认为必须有一些更短更好的方法来实现它。
+ (NSString*) serializeDeviceToken:(NSData*) deviceToken
{
NSMutableString *str = [NSMutableString stringWithCapacity:64];
int length = [deviceToken length];
char *bytes = malloc(sizeof(char) * length);
[deviceToken getBytes:bytes length:length];
for (int i = 0; i < length; i++)
{
[str appendFormat:@"%02.2hhX", bytes[i]];
}
free(bytes);
return str;
}
答案 0 :(得分:194)
这是我写的一个应用于NSData的类别。它返回表示NSData的十六进制NSString,其中数据可以是任意长度。如果NSData为空,则返回空字符串。
<强>的NSData + Conversion.h 强>
#import <Foundation/Foundation.h>
@interface NSData (NSData_Conversion)
#pragma mark - String Conversion
- (NSString *)hexadecimalString;
@end
<强>的NSData + Conversion.m 强>
#import "NSData+Conversion.h"
@implementation NSData (NSData_Conversion)
#pragma mark - String Conversion
- (NSString *)hexadecimalString {
/* Returns hexadecimal string of NSData. Empty string if data is empty. */
const unsigned char *dataBuffer = (const unsigned char *)[self bytes];
if (!dataBuffer)
return [NSString string];
NSUInteger dataLength = [self length];
NSMutableString *hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
for (int i = 0; i < dataLength; ++i)
[hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
return [NSString stringWithString:hexString];
}
@end
用法:
NSData *someData = ...;
NSString *someDataHexadecimalString = [someData hexadecimalString];
这可能比调用[someData description]
然后剥离空格,&lt; s和&gt;更好“。剥离字符只是觉得太“hacky”。此外,您永远不知道Apple将来是否会更改NSData -description
的格式。
注意:我已让人们与我联系,了解有关此答案中代码的许可。我特此将我在本公开域名答案中公布的代码的版权保留给我。
答案 1 :(得分:29)
这是一个用于生成十六进制字符串的高度优化的NSData category方法。虽然@Dave Gallagher的答案对于相对较小的尺寸来说足够了,但是对于大量数据,内存和CPU性能会下降。我在iPhone 5上用2MB文件对此进行了分析。时间比较为0.05对12秒。使用此方法可以忽略内存占用,而另一种方法将堆增加到70MB!
- (NSString *) hexString
{
NSUInteger bytesCount = self.length;
if (bytesCount) {
const char *hexChars = "0123456789ABCDEF";
const unsigned char *dataBuffer = self.bytes;
char *chars = malloc(sizeof(char) * (bytesCount * 2 + 1));
if (chars == NULL) {
// malloc returns null if attempting to allocate more memory than the system can provide. Thanks Cœur
[NSException raise:@"NSInternalInconsistencyException" format:@"Failed to allocate more memory" arguments:nil];
return nil;
}
char *s = chars;
for (unsigned i = 0; i < bytesCount; ++i) {
*s++ = hexChars[((*dataBuffer & 0xF0) >> 4)];
*s++ = hexChars[(*dataBuffer & 0x0F)];
dataBuffer++;
}
*s = '\0';
NSString *hexString = [NSString stringWithUTF8String:chars];
free(chars);
return hexString;
}
return @"";
}
答案 2 :(得分:15)
使用NSData的description属性不应被视为HEX编码字符串的可接受机制。该属性仅供说明,可随时更改。作为一个注释,在iOS之前,NSData描述属性甚至没有以十六进制形式返回它的数据。
很抱歉对解决方案进行了苛刻,但重要的是要充分利用它来序列化它,而不需要支持除了数据序列化以外的其他方式的API。
@implementation NSData (Hex)
- (NSString*)hexString
{
NSUInteger length = self.length;
unichar* hexChars = (unichar*)malloc(sizeof(unichar) * (length*2));
unsigned char* bytes = (unsigned char*)self.bytes;
for (NSUInteger i = 0; i < length; i++) {
unichar c = bytes[i] / 16;
if (c < 10) {
c += '0';
} else {
c += 'A' - 10;
}
hexChars[i*2] = c;
c = bytes[i] % 16;
if (c < 10) {
c += '0';
} else {
c += 'A' - 10;
}
hexChars[i*2+1] = c;
}
NSString* retVal = [[NSString alloc] initWithCharactersNoCopy:hexChars length:length*2 freeWhenDone:YES];
return [retVal autorelease];
}
@end
答案 3 :(得分:8)
以下是一种更快捷的转化方式:
BenchMark(重复100次1024字节数据转换的平均时间):
戴夫加拉格尔:~8.070毫秒 NSProgrammer:~0.077 ms@implementation NSData (BytesExtras)
static char _NSData_BytesConversionString_[512] = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
-(NSString*)bytesString
{
UInt16* mapping = (UInt16*)_NSData_BytesConversionString_;
register UInt16 len = self.length;
char* hexChars = (char*)malloc( sizeof(char) * (len*2) );
// --- Coeur's contribution - a safe way to check the allocation
if (hexChars == NULL) {
// we directly raise an exception instead of using NSAssert to make sure assertion is not disabled as this is irrecoverable
[NSException raise:@"NSInternalInconsistencyException" format:@"failed malloc" arguments:nil];
return nil;
}
// ---
register UInt16* dst = ((UInt16*)hexChars) + len-1;
register unsigned char* src = (unsigned char*)self.bytes + len-1;
while (len--) *dst-- = mapping[*src--];
NSString* retVal = [[NSString alloc] initWithBytesNoCopy:hexChars length:self.length*2 encoding:NSASCIIStringEncoding freeWhenDone:YES];
#if (!__has_feature(objc_arc))
return [retVal autorelease];
#else
return retVal;
#endif
}
@end
答案 4 :(得分:8)
一个班轮:
let hexString = UnsafeBufferPointer<UInt8>(start: UnsafePointer(data.bytes),
count: data.length).map { String(format: "%02x", $0) }.joinWithSeparator("")
这是一个可重复使用且自我记录的扩展形式:
extension NSData {
func base16EncodedString(uppercase uppercase: Bool = false) -> String {
let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer(self.bytes),
count: self.length)
let hexFormat = uppercase ? "X" : "x"
let formatString = "%02\(hexFormat)"
let bytesAsHexStrings = buffer.map {
String(format: formatString, $0)
}
return bytesAsHexStrings.joinWithSeparator("")
}
}
或者,使用reduce("", combine: +)
代替joinWithSeparator("")
被同行视为功能主人。
编辑:我将字符串($ 0,基数:16)更改为字符串(格式:“%02x”,$ 0),因为填充零所需的一位数字
答案 5 :(得分:7)
彼得的回答移植到斯威夫特
func hexString(data:NSData)->String{
if data.length > 0 {
let hexChars = Array("0123456789abcdef".utf8) as [UInt8];
let buf = UnsafeBufferPointer<UInt8>(start: UnsafePointer(data.bytes), count: data.length);
var output = [UInt8](count: data.length*2 + 1, repeatedValue: 0);
var ix:Int = 0;
for b in buf {
let hi = Int((b & 0xf0) >> 4);
let low = Int(b & 0x0f);
output[ix++] = hexChars[ hi];
output[ix++] = hexChars[low];
}
let result = String.fromCString(UnsafePointer(output))!;
return result;
}
return "";
}
<强> swift3 强>
func hexString()->String{
if count > 0 {
let hexChars = Array("0123456789abcdef".utf8) as [UInt8];
return withUnsafeBytes({ (bytes:UnsafePointer<UInt8>) -> String in
let buf = UnsafeBufferPointer<UInt8>(start: bytes, count: self.count);
var output = [UInt8](repeating: 0, count: self.count*2 + 1);
var ix:Int = 0;
for b in buf {
let hi = Int((b & 0xf0) >> 4);
let low = Int(b & 0x0f);
output[ix] = hexChars[ hi];
ix += 1;
output[ix] = hexChars[low];
ix += 1;
}
return String(cString: UnsafePointer(output));
})
}
return "";
}
Swift 5
func hexString()->String{
if count > 0 {
let hexChars = Array("0123456789abcdef".utf8) as [UInt8];
return withUnsafeBytes{
let buf = $0.bindMemory(to: UInt8.self);
var output = [UInt8](repeating: 0, count: self.count*2 + 1);
var ix:Int = 0;
for b in buf {
let hi = Int((b & 0xf0) >> 4);
let low = Int(b & 0x0f);
output[ix] = hexChars[ hi];
ix += 1;
output[ix] = hexChars[low];
ix += 1;
}
return String(cString: UnsafePointer(output));
}
}
return "";
}
答案 6 :(得分:4)
我需要解决这个问题并发现这里的答案非常有用,但我担心性能问题。这些答案大多涉及从NSData批量复制数据,因此我编写了以下内容以低开销进行转换:
@interface NSData (HexString)
@end
@implementation NSData (HexString)
- (NSString *)hexString {
NSMutableString *string = [NSMutableString stringWithCapacity:self.length * 3];
[self enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop){
for (NSUInteger offset = 0; offset < byteRange.length; ++offset) {
uint8_t byte = ((const uint8_t *)bytes)[offset];
if (string.length == 0)
[string appendFormat:@"%02X", byte];
else
[string appendFormat:@" %02X", byte];
}
}];
return string;
}
这会在字符串中为整个结果预先分配空间,并避免使用enumerateByteRangesUsingBlock复制NSData内容。将X更改为格式字符串中的x将使用小写十六进制数字。如果您不希望在字节之间使用分隔符,则可以减少语句
if (string.length == 0)
[string appendFormat:@"%02X", byte];
else
[string appendFormat:@" %02X", byte];
直到
[string appendFormat:@"%02X", byte];
答案 7 :(得分:2)
我需要一个适用于可变长度字符串的答案,所以这就是我所做的:
+ (NSString *)stringWithHexFromData:(NSData *)data
{
NSString *result = [[data description] stringByReplacingOccurrencesOfString:@" " withString:@""];
result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
return result;
}
非常适合作为NSString类的扩展。
答案 8 :(得分:1)
您始终可以使用[yourString uppercaseString]来大写数据描述中的字母
答案 9 :(得分:1)
将NSData序列化/反序列化为NSString的更好方法是使用Google Toolbox for Mac Base64编码器/解码器。只需从包基础中拖入您的App Project文件GTMBase64.m,GTMBase64.h和GTMDefines.h并执行类似
的操作/**
* Serialize NSData to Base64 encoded NSString
*/
-(void) serialize:(NSData*)data {
self.encodedData = [GTMBase64 stringByEncodingData:data];
}
/**
* Deserialize Base64 NSString to NSData
*/
-(NSData*) deserialize {
return [GTMBase64 decodeString:self.encodedData];
}
答案 10 :(得分:1)
这是使用Swift 3的解决方案
extension Data {
public var hexadecimalString : String {
var str = ""
enumerateBytes { buffer, index, stop in
for byte in buffer {
str.append(String(format:"%02x",byte))
}
}
return str
}
}
extension NSData {
public var hexadecimalString : String {
return (self as Data).hexadecimalString
}
}
答案 11 :(得分:0)
将%08x
更改为%08X
以获取大写字符。
答案 12 :(得分:0)
@implementation NSData (Extn)
- (NSString *)description
{
NSMutableString *str = [[NSMutableString alloc] init];
const char *bytes = self.bytes;
for (int i = 0; i < [self length]; i++) {
[str appendFormat:@"%02hhX ", bytes[i]];
}
return [str autorelease];
}
@end
Now you can call NSLog(@"hex value: %@", data)
答案 13 :(得分:0)
Swift + Property。
我更喜欢将十六进制表示作为属性(与bytes
和description
属性相同):
extension NSData {
var hexString: String {
let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer(self.bytes), count: self.length)
return buffer.map { String(format: "%02x", $0) }.joinWithSeparator("")
}
var heXString: String {
let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer(self.bytes), count: self.length)
return buffer.map { String(format: "%02X", $0) }.joinWithSeparator("")
}
}
理念借鉴了这个answer
答案 14 :(得分:-2)
[deviceToken description]
您需要删除空格。
我个人base64
编码deviceToken
,但这是一个品味问题。