简体   繁体   English

检索String的字节在ObjC中返回的结果与Java返回的结果不同

[英]Retrieving bytes of String returns different results in ObjC than Java

I've got a string that I'm trying to convert to bytes in order to create an md5 hash in both ObjC and Java. 我有一个字符串,我试图将其转换为字节,以便在ObjC和Java中创建md5哈希。 For some reason, the bytes are different between the two languages. 由于某种原因,这两种语言的字节不同。

Java 爪哇

System.out.println(Arrays.toString(
("78b4a02fa139a2944f17b4edc22fb175:8907f3c4861140ad84e20c8e987eeae6").getBytes()));

Output: 输出:

[55, 56, 98, 52, 97, 48, 50, 102, 97, 49, 51, 57, 97, 50, 57, 52, 52, 102, 49, 55, 98, 52, 101, 100, 99, 50, 50, 102, 98, 49, 55, 53, 58, 56, 57, 48, 55, 102, 51, 99, 52, 56, 54, 49, 49, 52, 48, 97, 100, 56, 52, 101, 50, 48, 99, 56, 101, 57, 56, 55, 101, 101, 97, 101, 54]

ObjC 对象

NSString *str = @"78b4a02fa139a2944f17b4edc22fb175:8907f3c4861140ad84e20c8e987eeae6";
NSData *bytes = [str dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:NO];
NSLog(@"%@", [bytes description]);

Output: 输出:

<37386234 61303266 61313339 61323934 34663137 62346564 63323266 62313735 3a383930 37663363 34383631 31343061 64383465 32306338 65393837 65656165 36>

I've tried using different charsets with no luck and can't think of any other reasons why the bytes would be different. 我试过使用不同的字符集,但没有运气,也无法想到字节会有所不同的任何其他原因。 Any ideas? 有任何想法吗? I did notice that all of the byte values are different by some factor of 18 but am not sure what is causing it. 我确实注意到所有字节值都相差18左右,但不确定是什么原因引起的。

Actually, Java is printing in decimal, byte by byte. 实际上,Java是用十进制逐字节打印的。 Obj C is printing in hex, integer by integer. Obj C以十六进制打印,一个整数一个整数。

Referring this chart : 参考此

Dec Hex
55  37
56  38
98  62
...

You'll just have to find a way to output byte by byte in Obj C. 您只需要找到一种在Obj C中逐字节输出的方法。

I don't know about Obj C, but if that NSLog function works similar to printf() in C, I'd start with that. 我不了解Obj C,但是如果NSLog函数的工作方式类似于C中的printf(),那么我将从此开始。

A code snippet from Apple 苹果公司的代码片段

unsigned char aBuffer[20];
NSString *myString = @"Test string.";
const char *utfString = [myString UTF8String];
NSData *myData = [NSData dataWithBytes: utfString length: strlen(utfString)];

[myData getBytes:aBuffer length:20];

The change in bytes can be due to Hex representation. 字节的变化可能是由于十六进制表示。 The above code shows how to convert the string to bytes and store the result in a buffer. 上面的代码显示了如何将字符串转换为字节并将结果存储在缓冲区中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:将数字和字符串转换为十六进制会返回不同的结果。 为什么? - Java: converting numbers and string to hex returns different results. Why? 像在Java中那样在objc中解码字符串 - Decoding string in objc as in Java 运行同一查询时,Java语句executeQuery返回的结果与SQLDeveloper中返回的结果不同 - Java Statement executeQuery returns different results than in SQLDeveloper when the same query is run Java-字节到字节的字符串[] - Java - String of bytes to bytes[] String(bytes [] Charset)在Java7和java 8中以不同的方式返回结果 - String (bytes[] Charset) is returning results differently in Java7 and java 8 奇怪的 JAVA UTF-8 编码行为,new String(bytes,&quot;UTF-8&quot;) 在大多数相似的设置上给出了不同的结果 - Weird JAVA UTF-8 encoding behaviour, new String(bytes,"UTF-8") gives different results on mostly similar setup Java字符串拆分返回空结果 - Java string split returns empty results 用于Java字符串文字的字节码长度超过65535字节 - Bytecode for Java string literals longer than 65535 bytes Java:相同的字符串返回不同的字节数组 - Java: Same string returns different byte arrays Java-String.replaceAll返回不同的结果 - Java - String.replaceAll returns different result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM