从 iPhone 应用程序的 xcode 中的字符串获取 ascii 代码

发布于 2025-01-03 15:29:34 字数 466 浏览 2 评论 0原文

嘿,只是一些关于编写我的第一个 iOS 应用程序的新手问题。我一直在搜索这里的问题,但它们似乎都解决了比我更高级的问题,所以我很困惑。

(1) 我想要做的就是将一个字符串转换为表示 ASCII 代码的整数数组。换句话说,我想将:转换

"This is some string. It has spaces, punctuation, AND capitals."

成一个有62个整数的数组。

(2) 如何从 NSArray 返回字符串?

(3) 另外,这些操作在内存或计算时间方面是否昂贵?如果我们必须在每次迭代或其他事情中创建一个新变量,那么似乎可能会这样。

我知道如何声明所有变量,并且假设我在字符串的长度上运行一个循环,并且在每次迭代时我都会以某种方式获取字符并将其转换为数字,并通过调用内置命令将其转换为数字。

感谢您提供的任何帮助或可能有帮助的帖子链接!

hey just a couple quick noob questions about writing my first ios app. Ive been searching through the questions here but they all seem to address questions more advanced than mine so im getting confused.

(1) All I want to do is turn a string into an array of integers representing the ASCII code. In other words, I want to convert:

"This is some string. It has spaces, punctuation, AND capitals."

into an array with 62 integers.

(2) How do I get back from the NSArray to a string?

(3) Also, are these expensive operations in terms of memory or computation time? It seems like it might be if we have to create a new variable at every iteration or something.

I know how to declare all the variables and im assuming I run a loop through the length of the string and at each iteration I somehow get the character and convert it into a number with some call to a built in command.

Thanks for any help you can offer or links to posts that might help!

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

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

发布评论

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

评论(3

你是暖光i 2025-01-10 15:29:34

如果你想将 ascii 值存储在 nsarray 中,这将是昂贵的。 NSArray 只能保存对象,因此您必须为每个 ASCII 值创建一个 NSNumber:


unsigned len = [string length];
NSMutableArray arr = [NSMutableArray arrayWithCapacity:len];
for (unsigned i = 0; i < len; ++i) {
   [arr addObject:[NSNumber numberWithUnsignedShort:[string characterAtIndex:i]]];
}

2) to go back to an NSString you'll need to use an MSMutableString and append each byte to the NSMutableString.

说了这么多,如果可以避免的话,我建议你不要使用这种方法。

更好的方法是使用@EmilioPelaez 的答案。与迭代和连接字符串相比,从内存缓冲区返回到 NSString 既简单又便宜。


NSString * stringFromMemory = [[NSString alloc] initWithBytes:buffer length:len encoding: NSASCIIStringEncoding];

if you want to store the ascii values in an nsarray it is going to be expensive. NSArray can only hold objects so you're going to have to create an NSNumber for each ASCII value:


unsigned len = [string length];
NSMutableArray arr = [NSMutableArray arrayWithCapacity:len];
for (unsigned i = 0; i < len; ++i) {
   [arr addObject:[NSNumber numberWithUnsignedShort:[string characterAtIndex:i]]];
}


2) to go back to an NSString you'll need to use an MSMutableString and append each byte to the NSMutableString.

After saying that I'd suggest you don't use this method if you can avoid it.

A better approach would be to use @EmilioPelaez's answer. To go back from a memory buffer to an NSString is simple and inexpensive compared to iterating and concatting strings.


NSString * stringFromMemory = [[NSString alloc] initWithBytes:buffer length:len encoding: NSASCIIStringEncoding];

情绪 2025-01-10 15:29:34

我最终使用了在这里找到的语法。感谢您的帮助

如何将 ASCII 值转换为Objective-C 中的一个角色?

I ended up using the syntax I found here. Thanks for the help

How to convert ASCII value to a character in Objective-C?

对你再特殊 2025-01-10 15:29:34

NSString有一个方法来获取数组中的字符:

NSString *string = "This is some string. It has spaces, punctuation, AND capitals.";

unichar *buffer = malloc(sizeof(unichar) * [string lenght]);

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

如果你检查unichar的定义,它是一个无符号短整型。

NSString has a method to get the characters in an array:

NSString *string = "This is some string. It has spaces, punctuation, AND capitals.";

unichar *buffer = malloc(sizeof(unichar) * [string lenght]);

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

If you check the definition of unichar, it's an unsigned short.

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