What does C# do when typecasting a letter to an int?

☆樱花仙子☆ 提交于 2019-12-31 04:04:13

问题


We've to implement an encryption for an external interface. The owner of the interface has given documentation of how to preform the same encryption on our side. However, this documentation is in C# and we work in PHP.

Most of the parts we understand except for where they seem to typecast a hash to an int. Their code reads:

// hashString exists and is a md5 a like string
int[] keyBuffer = new int[hashString.length];
for (int i=0; i<hashString.length; i++) {
    keyBuffer[i] = (int)hashString[i];
}

In PHP, when casting a letter as int, you get 0 (int). As we can't imagine this is what the third party means, we believe C# does something else.

Does C# also cast to int 0, or possibly to a char?

Second, the original hashString is 320 long. This means the code will be creating an int which is 320 long?? In PHP you don't have this idea of reserving memory as C# does here. But when we try to typecast a 320 long string to an int we get an int which is 19 'chars' long.

Does C# also create a shorter int when typecasting a really long 'number' in a string?


回答1:


You're converting a char to int. A char is a UTF-16 code unit - an unsigned 16-bit integer (the range is [0, 65535]). You get that value, basically, widened to a 32-bit signed integer. So 'A' ends up as 65, for example, and the Euro symbol (U+20AC) ends up as 8364 (0x20ac).

As for your second part - you're creating an int, you're creating an int array. An yes, you'll be creating an array with 320 elements.




回答2:


C# strings are UTF16. When you cast a UTF16 character to an int, it merely copies the 16-bit UTF16 character value into the 32-bit int.




回答3:


C# can cast a character to an int and will give you the character code.The code above is taking a string, hashString, and turning it into an array of integers, keybuffer. C# is capable of treating a string like an array of chars using the indexer [] syntax. The code above will produce an array of ints, one per character in the hash string, and each int will be the character code of the corresponding character.




回答4:


To expand on Jon Skeet's post, your "decimal" integer values will map to the corresponding char values like in the chart below (which I have had on my development PCs for years).

So, casting the integer value 0 to a char will return a NULL.

EDIT: Looking at your original question, it is possible you would be better served looking at an MD5 Example instead of casting the string to an array of integers.




回答5:


The code actually cast the char (normally ASCII) into an int, not '0' to 0. So if the original string is "d131dd02c5e6eec4", the resulting array will be int[]{100, 49, 51, 49, 100, 100, 48, 50, 99, 53, 101, 54, 101, 101, 99, 52}.

So I imagine you need the function ord in your PHP script.

EDIT:

A bit remarks, casting a string to int in PHP may actually phrase it into int, and the largest int PHP handles is either 32-bit or 64-bit depending on the OS, that's why you get a 19-char long int, which is the maximum of 64-bit int.

In C#, there is another variable type called char, which represents one unicode character, and can cast directly into integer. You cannot cast a string in C# into an int directly in C#.

EDIT2: I imagine your PHP script to look like this:

<?php
$keyBuffer = new array();
for ($i=0; $i<strlen($hashString); $i++) {
    $keyBuffer[$i] = ord($hashString[i]);
}
?>


来源:https://stackoverflow.com/questions/11102194/what-does-c-sharp-do-when-typecasting-a-letter-to-an-int

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!