NSData 到 NSString,反之亦然

发布于 2024-12-28 10:49:27 字数 467 浏览 4 评论 0原文

系统会提示我的用户输入 RAW NSData(例如:<0201581d 9fc84f7b bf136a80 e7fc9572>)

此原始数据是 AES 加密的 NSString。

然而,问题是将那些实际的数据字节 <0201581d 9fc84f7b bf136a80 e7fc9572> 转换为转换为 NSData 类型本身。

  1. 提示输入数据
  2. 输入数据 --> @“<0201581d 9fc84f7b bf136a80 e7fc9572>”
  3. 需要将输入的数据转换为 NSData 类型,而不是传递的 NSString 类型。

简而言之;我该怎么做:<0201581d 9fc84f7b bf136a80 e7fc9572>进入 NSData 的数据? 让一个整数保存数据的一半是有效的(因为类型太短),所以它需要是一个 NSString。

My user is prompted to enter in RAW NSData (like: <0201581d 9fc84f7b bf136a80 e7fc9572>)

This raw data is an AES encrypted NSString.

However, the issue is converting those actual bytes of data <0201581d 9fc84f7b bf136a80 e7fc9572> into an NSData type itself.

  1. Prompted to enter data
  2. Enters data --> @"<0201581d 9fc84f7b bf136a80 e7fc9572>"
  3. Needs to make entered data into an NSData type rather than the NSString which was passed.

In short; How do I make this: <0201581d 9fc84f7b bf136a80 e7fc9572> into the NSData's data?
Making an integer hold the data HALF works (because the type is too short) so It needs to be an NSString.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2025-01-04 10:49:27

要将十六进制字符串转换为数据,请参阅我在此处发布的函数:
在 Objective-C 中将十六进制字符串转换为二进制

您需要稍微修改它以允许括号字符和空格。

要走向另一个方向:

NSString *CreateHexStringWithData(NSData *data)
{
    NSUInteger inLength = [data length];
    unichar *outCharacters = malloc(sizeof(unichar) * (inLength * 2));

    UInt8 *inBytes = (UInt8 *)[data bytes];
    static const char lookup[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    NSUInteger i, o = 0;
    for (i = 0; i < inLength; i++) {
        UInt8 inByte = inBytes[i];
        outCharacters[o++] = lookup[(inByte & 0xF0) >> 4];
        outCharacters[o++] = lookup[(inByte & 0x0F)];
    }

    return [[NSString alloc] initWithCharactersNoCopy:outCharacters length:o freeWhenDone:YES];
}

您还可以使用:

[NSString stringWithFormat:@"%@", data]

使用 -[NSData description] 来获取带有括号和空格的版本。

To convert a hexadecimal string to data, see the function that I posted here:
Converting a hexadecimal string into binary in Objective-C

You will want to modify it slightly to allow bracket characters and whitespace.

To go the other direction:

NSString *CreateHexStringWithData(NSData *data)
{
    NSUInteger inLength = [data length];
    unichar *outCharacters = malloc(sizeof(unichar) * (inLength * 2));

    UInt8 *inBytes = (UInt8 *)[data bytes];
    static const char lookup[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    NSUInteger i, o = 0;
    for (i = 0; i < inLength; i++) {
        UInt8 inByte = inBytes[i];
        outCharacters[o++] = lookup[(inByte & 0xF0) >> 4];
        outCharacters[o++] = lookup[(inByte & 0x0F)];
    }

    return [[NSString alloc] initWithCharactersNoCopy:outCharacters length:o freeWhenDone:YES];
}

You can also use:

[NSString stringWithFormat:@"%@", data]

to use -[NSData description] to get a version with the brackets and spaces.

绝不放开 2025-01-04 10:49:27

这与NSString(十六进制)到字节非常相似。如果您删除多余的标点符号,答案将会对您有用。

This is extremely similar to NSString (hex) to bytes. The answer there will work for you if you remove the excess punctuation.

北方的巷 2025-01-04 10:49:27

要将 NSData 转换为 NSString 使用:

NSData* returnData;
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

将 NSString 转换为 NSData 使用:

NSString* str = @"returnString";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];

To convert NSData to NSString use :

NSData* returnData;
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

To convert NSString to NSData use :

NSString* str = @"returnString";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文