NSMutableString stringWithFormat
准备消息
private static char[] jimsCopyRight = {
我有 java 代码,为 MD5 munge 'C'、'o'、'p'、'y'、'r'、'i'、'g'、'h' , 't', ':', ' ', 0xa9, ' '};
用于
StringBuffer message = new StringBuffer();
message.append(name.toLowerCase()); message.append(new String(jimsCopyRight));
打印消息时
for(int i = 0; i < message.length(); i++){
当我使用System.out.println(" i = " + i + " char " + message.substring(i, i + 1) + " charAT " + message.charAt(i)); 我得到 i = 14 char \251 charAT \251并且
message.toString 是 jimCopyright: \251
我需要构造一个具有相同字符的 NSMutableString 。
在我尝试过的事情中,
wDevCopyright = [NSString stringWithFormat:@"jimCopyright: %c ", 0xa9];
for(int i = 0; i < [message length]; i++){
NSLog(@"i = %d char %c %d", i, [message characterAtIndex:i], [message characterAtIndex:i]);
}
这给了我 i = 14 char © 169
任何使 NSMutableString 与 StringBuffer 相同的帮助将不胜感激。
问题是,当我合并 MD5 中的两个字符串时,添加 0xa9 后会得到不同的结果。这些印刷品只是为了看一下琴弦。
我认为这与 Java 中的 char[] 和 NSMutableString 的构造有关。我不相信他们有相同的价值观。
我有一些 C 代码,它声明版权为
#define jimsCopyRight "Copyright: � "
Java MD5 和 C MD5 的版权是相同的。
I have java code that prepares a message for a MD5 munge
private static char[] jimsCopyRight = {
'C', 'o', 'p', 'y', 'r', 'i', 'g', 'h',
't', ':', ' ', 0xa9, ' '};
which is used in
StringBuffer message = new StringBuffer();
message.append(name.toLowerCase());
message.append(new String(jimsCopyRight));
When I print out the message using
for(int i = 0; i < message.length(); i++){
System.out.println(" i = " + i + " char " + message.substring(i, i + 1) + " charAT " + message.charAt(i));
}
I get i = 14 char \251 charAT \251 and the message.toString is jimCopyright: \251
I need to construct a NSMutableString with the same characters.
Among the things I have tried
wDevCopyright = [NSString stringWithFormat:@"jimCopyright: %c ", 0xa9];
for(int i = 0; i < [message length]; i++){
NSLog(@"i = %d char %c %d", i, [message characterAtIndex:i], [message characterAtIndex:i]);
}
Which gives me i = 14 char © 169
Any help in getting the NSMutableString to be the same as the StringBuffer will be appreciated.
The problem is that when I munge the two strings in MD5 I get different results when I add the 0xa9. The prints are just for getting a look at the strings.
I'm thinking it has something to do with the char[] in Java and the construct of the NSMutableString. I do not believe they are the same values.
I have some C code and it declares the copyright as
#define jimsCopyRight "Copyright: � "
The Java MD5 and C MD5 of the copyright are the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
版权符号就是您想要的,对吗?根据你的问题,这就是你得到的。那么问题出在哪里呢?
(这就是为什么幻数不好。我无法仅从你的代码中看出你想要什么编码
0xa9
。)如果你想知道为什么它说 169 而不是 251,那是因为 Java 打印出来八进制(基数为 8)字符转义序列,而 C 标准库、Core Foundation 和 Foundation 中的
%d
将值打印为十进制 (base-10) 。\251
不是第 251 个字符。使用%o
将值打印为八进制,或使用 %x 将其打印为十六进制(基数 16)。或者,使用 Calculator.app 的编程模式将 251 从八进制转换为十进制。顺便说一句,您可以使用
%@
格式序列和NSUserName
函数将您的用户名插入到字符串中,就像在 Java 代码中所做的那样。无需对其进行硬编码。The copyright symbol is what you wanted, right? According to your question, that's what you got. So what's the problem?
(This is why magic numbers are bad. I can't tell from your code alone what encoding's
0xa9
you wanted.)If you're wondering why it says 169 instead of 251, it's because Java prints out the octal (base-8) character escape sequence, whereas
%d
in the C standard library, Core Foundation, and Foundation prints out the value as decimal (base-10).\251
is not the two hundred and fifty-first character. Use%o
to print the value as octal, or %x to print it as hexadecimal (base-16). Or, use Calculator.app's Programming mode to convert 251 from octal to decimal.BTW, you can use the
%@
format sequence and theNSUserName
function to insert your username into the string, just like you did in the Java code. No need to hard-code it.在 Objective-C 版本中,您在“jim[s]Copyright”中缺少一个“s”。
You’re missing an “s” in “jim[s]Copyright” in the Objective-C version.