如何将希伯来语字符串 (NSString) 编码为 Unicode 格式,以便在 Objective-C 中作为 URL 发送 [英] How to encode hebrew string (NSString) into a Unicode format in order to send as a URL in Objective-C

查看:100
本文介绍了如何将希伯来语字符串 (NSString) 编码为 Unicode 格式,以便在 Objective-C 中作为 URL 发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎概括了它.我在 NSUrl 中使用了一个包含希伯来语的字符串:

The title pretty much sums it up. I have a hebrew-containing String used in a NSUrl:

NSString * urlS = @"http://irrelevanttoyourinterests/some.aspx?foo=bar&this=that&Text=תל אביב"

我想转换成:

Text=%u05EA%u05DC%20%u05D0%u05d1%u05d9%u05d1

然后将其作为 GET 请求发送.我尝试了很多编码方法都没有成功,最终我尝试将编码后的字符串静态插入到上面引用的 URL 中.

and then send it as a GET request. I have tried many encoding methods unsuccessfully, and eventually I tried statically inserting the encoded string into the URL, quoted above.

NSURL *url1 = [NSURL URLWithString:urlS];

然后我使用你可能熟悉的 startAsyncLoad (here),但是当 URL 用静态 Unicode 字符串构造时,不会发送任何内容(使用 Wireshark 检查),尽管如果我在它发送的 startAsyncLoad 之前使用以下行(错误编码,当然).

Then I use startAsyncLoad which you may be familiar with (here), but when the URL is constracted with the static Unicode string, nothing gets sent (checked with Wireshark), although if I use the following line before the startAsyncLoad it sends (wrongly encoded, of course).

urlS = [urlS stringByAddingPercentEscapesUsingEncoding:NSWindowsCP1254StringEncoding];

提前致谢.

推荐答案

您作为示例给出的百分比转义是 Unicode 代码点.这种类型的编码是非标准的,所以我认为不太可能已经存在可以做到这一点的方法.您可能需要自己动手,但这并不难.

The percent escapes that you have given as an example are Unicode code points. This type of encoding is non-standard, so I think it is unlikely that there exists a method already that can do this. You may have to roll your own, but it's not too difficult.

在标题(可能称为 NSString+NonStandardPercentEscapes.h)中输入以下内容:

In a header (perhaps called NSString+NonStandardPercentEscapes.h) put the following:

#import <Foundation/Foundation.h>

@interface NSString (NonStandardPercentEscapes)
- (NSString *) stringByAddingNonStandardPercentEscapes;
@end

并且,在源文件(可能称为 NSString+NonStandardPercentEscapes.m)中输入以下内容:

And, in a source file (perhaps called NSString+NonStandardPercentEscapes.m) put the following:

#import "NSString+NonStandardPercentEscapes.h"

@implementation NSString (NonStandardPercentEscapes)

- (NSString *) stringByAddingNonStandardPercentEscapes
{
    NSCharacterSet *mustEscape = [NSCharacterSet characterSetWithCharactersInString:@"<>~\"{}|\\-`^% "];
    NSMutableString *result = [[NSMutableString alloc] init];

    NSUInteger length = [self length];
    unichar buffer[length];

    [self getCharacters:buffer range:NSMakeRange(0, length)];

    for (NSUInteger i = 0; i < length; i++)
    {
        if ([mustEscape characterIsMember:buffer[i]])
            [result appendFormat:@"%%%02hhx", buffer[i]];
        else if (buffer[i] > 0xFF)
            [result appendFormat:@"%%u%04hx", buffer[i]];
        else if (!isascii((int)buffer[i]))
            [result appendFormat:@"%%%02hhx", buffer[i]];
        else
            [result appendFormat:@"%c", buffer[i]];
    }

    // return the mutable version, nobody will know unless they check the class
    return [result autorelease];

    // alternatively, you can force the result to be immutable
    NSString *immutable = [[result copy] autorelease];
    [result release];
    return immutable;
}
@end

然后,无论您需要在何处对希伯来语字符串进行编码,都可以执行以下操作(只要您的源文件包含上述标头):

Then, wherever you need to encode your Hebrew string, you can do the following (as long as your source file includes the above header):

NSString * urlS = @"http://irrelevanttoyourinterests/some.aspx?foo=bar&this=that&Text=תל אביב";
urlS = [urlS stringByAddingNonStandardPercentEscapes];

NSUrl *url1 = [NSURL URLWithString:urlS];

免责声明:

我不知道需要转义哪些字符以及转义应该在什么时候开始(这只是对整个 URL 进行编码,这可能不是您想要的),但上面的代码至少应该让您开始.

Disclaimer:

I have no idea what characters need to be escaped and at what point the escaping should start (this just encodes the entire URL which is probably not what you want), but the above code should at least get you under way.

这篇关于如何将希伯来语字符串 (NSString) 编码为 Unicode 格式,以便在 Objective-C 中作为 URL 发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆