如何将NSString转换为十六进制值

Ben*_*ron 29 iphone objective-c

我想将常规NSString转换为带有(我假设的)ASCII十六进制值的NSString并返回.

我需要生成与下面的Java方法相同的输出,但我似乎无法在Objective-C中找到一种方法.我在C和C++中找到了一些例子,但是我很难将它们用到我的代码中.

以下是我正在尝试重现的Java方法:

/**
* Encodes the given string by using the hexadecimal representation of its UTF-8 bytes.
*
* @param s The string to encode.
* @return The encoded string.
*/
public static String utf8HexEncode(String s) {
    if (s == null) {
        return null;
    }
    byte[] utf8;
    try {
        utf8 = s.getBytes(ENCODING_UTF8);
    } catch (UnsupportedEncodingException x) {
        throw new RuntimeException(x);
    }
    return String.valueOf(Hex.encodeHex(utf8));
}

/**
* Decodes the given string by using the hexadecimal representation of its UTF-8 bytes.
*
* @param s The string to decode.
* @return The decoded string.
* @throws Exception If an error occurs.
*/
public static String utf8HexDecode(String s) throws Exception {
if (s == null) {
    return null;
}
    return new String(Hex.decodeHex(s.toCharArray()), ENCODING_UTF8);
} 
Run Code Online (Sandbox Code Playgroud)

更新:感谢drawonward的答案,这是我写的创建十六进制NSStrings的方法.它给我一个"初始化在char声明行上丢弃指针目标类型的限定符"警告,但是它有效.

- (NSString *)stringToHex:(NSString *)string
{
    char *utf8 = [string UTF8String];
    NSMutableString *hex = [NSMutableString string];
    while ( *utf8 ) [hex appendFormat:@"%02X" , *utf8++ & 0x00FF];

    return [NSString stringWithFormat:@"%@", hex];
}
Run Code Online (Sandbox Code Playgroud)

尚未有时间编写解码方法.当我这样做时,我会编辑它以发布给其他感兴趣的人.

Update2:所以我上面发布的方法实际上并没有输出我正在寻找的东西.它不是输出0-f格式的十六进制值,而是输出所有数字.我终于回到了解决这个问题的方法,并且能够为NSString写一个类别,它完全复制了我发布的Java方法.这里是:

//
//  NSString+hex.h
//  Created by Ben Baron on 10/20/10.
//

@interface NSString (hex) 

    + (NSString *) stringFromHex:(NSString *)str;
    + (NSString *) stringToHex:(NSString *)str;

@end

//
//  NSString+hex.m
//  Created by Ben Baron on 10/20/10.
//

#import "NSString+hex.h"

@implementation NSString (hex)

+ (NSString *) stringFromHex:(NSString *)str 
{   
    NSMutableData *stringData = [[[NSMutableData alloc] init] autorelease];
    unsigned char whole_byte;
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    for (i=0; i < [str length] / 2; i++) {
        byte_chars[0] = [str characterAtIndex:i*2];
        byte_chars[1] = [str characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [stringData appendBytes:&whole_byte length:1]; 
    }

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

+ (NSString *) stringToHex:(NSString *)str
{   
    NSUInteger len = [str length];
    unichar *chars = malloc(len * sizeof(unichar));
    [str getCharacters:chars];

    NSMutableString *hexString = [[NSMutableString alloc] init];

    for(NSUInteger i = 0; i < len; i++ )
    {
        [hexString appendString:[NSString stringWithFormat:@"%x", chars[i]]];
    }
    free(chars);

    return [hexString autorelease];
}

@end
Run Code Online (Sandbox Code Playgroud)

Pra*_*nna 28

将nsstring转换为十六进制值的完美和简短方法

NSMutableString *tempHex=[[NSMutableString alloc] init];

[tempHex appendString:@"0xD2D2D2"];

unsigned colorInt = 0;

[[NSScanner scannerWithString:tempHex] scanHexInt:&colorInt];

lblAttString.backgroundColor=UIColorFromRGB(colorInt);
Run Code Online (Sandbox Code Playgroud)

用于此代码的宏是----

#define UIColorFromRGB(rgbValue) 
[UIColor \colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
Run Code Online (Sandbox Code Playgroud)


dra*_*ard 22

对于这些Java行

utf8 = s.getBytes(ENCODING_UTF8);
new String(decodedHexString, ENCODING_UTF8);
Run Code Online (Sandbox Code Playgroud)

Objective-C等价物将是

utf8 = [s UTF8String];
[NSString initWithUTF8String:decodedHexString];
Run Code Online (Sandbox Code Playgroud)

使用字符串的十六进制表示形成NSString:

NSMutableString *hex = [NSMutableString string];
while ( *utf8 ) [hex appendFormat:@"%02X" , *utf8++ & 0x00FF];
Run Code Online (Sandbox Code Playgroud)

您必须自己创建decodeHex函数.只需从字符串中拉出两个字符,如果它们有效,则在结果中添加一个字节.

  • 应该是"NSMutableString*hex = [NSMutableString string];" (2认同)

Chi*_*ilv 8

你的stringToHex方法有问题 - 它会丢弃前导0,并忽略00.就像快速修复一样,我做了以下内容:

+ (NSString *) stringToHex:(NSString *)str
{   
    NSUInteger len = [str length];
    unichar *chars = malloc(len * sizeof(unichar));
    [str getCharacters:chars];

    NSMutableString *hexString = [[NSMutableString alloc] init];

    for(NSUInteger i = 0; i < len; i++ )
    {
        // [hexString [NSString stringWithFormat:@"%02x", chars[i]]]; /*previous input*/
        [hexString appendFormat:@"%02x", chars[i]]; /*EDITED PER COMMENT BELOW*/
    }
    free(chars);

    return [hexString autorelease];
}
Run Code Online (Sandbox Code Playgroud)

  • 顺便说一下你的代码中有一个小bug,`[hexString [NSString stringWithFormat:@"%02x",chars [i]]];`应该是`[hexString appendString:[NSString stringWithFormat:@"%02x",chars [i ]];``甚至更好`[hexString appendFormat:@"%02x",chars [i]];` (3认同)

sme*_*mee 7

感谢所有为此主题做出贡献的人.这对我很有帮助.由于自原始帖子以来事情已经发生了一些变化,这里是iOS 6的更新实现.我选择了类别方法,但选择在NSData和NSString之间分配负载.欢迎评论.

首先,NSString的一半,它处理十六进制编码的字符串解码为NSData对象.

@implementation NSString (StringToHexData)

//
// Decodes an NSString containing hex encoded bytes into an NSData object
//
- (NSData *) stringToHexData
{
    int len = [self length] / 2;    // Target length
    unsigned char *buf = malloc(len)
    unsigned char *whole_byte = buf;
    char byte_chars[3] = {'\0','\0','\0'};

    int i;
    for (i=0; i < [self length] / 2; i++) {
        byte_chars[0] = [self characterAtIndex:i*2];
        byte_chars[1] = [self characterAtIndex:i*2+1];
        *whole_byte = strtol(byte_chars, NULL, 16);
        whole_byte++;
    }

    NSData *data = [NSData dataWithBytes:buf length:len];
    free( buf );
    return data;
}
@end
Run Code Online (Sandbox Code Playgroud)

这些变化主要是出于效率的考虑:一些简单的老式指针算法意味着我可以一次性分配整个缓冲区,并逐字节填充它.然后整个过程一次性传递给NSData.

NSData中的编码部分如下所示:

@implementation NSData (DataToHexString)

- (NSString *) dataToHexString
{
    NSUInteger          len = [self length];
    char *              chars = (char *)[self bytes];
    NSMutableString *   hexString = [[NSMutableString alloc] init];

    for(NSUInteger i = 0; i < len; i++ )
        [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]];

    return hexString;
}
@end
Run Code Online (Sandbox Code Playgroud)

再次,一些微小的变化,虽然我怀疑这里没有效率提升.使用"%0.2hhx"解决了丢失前导零的所有问题,并确保一次只输出一个单字节.

希望这有助于下一个人接受这个!