【发布时间】:2012-05-22 19:13:06
【问题描述】:
看看下面的 C# 代码:
byte[] StringToBytesToBeHashed(string to_be_hashed) {
byte[] to_be_hashed_byte_array = new byte[to_be_hashed.Length];
int i = 0;
foreach (char cur_char in to_be_hashed)
{
to_be_hashed_byte_array[i++] = (byte)cur_char;
}
return to_be_hashed_byte_array;
}
(以上函数摘自these lines of code from the WMSAuth github repo)
我的问题是:从字节到字符的转换在编码方面有什么作用?
我想它在编码方面确实没有做任何事情,但这是否意味着 Encoding.Default 是使用的那个,因此返回的字节将取决于框架如何编码特定 Operative 中的底层字符串系统?
此外,char 实际上是否大于一个字节(我猜是 2 个字节)并且实际上会省略第一个字节?
我正在考虑用以下方式替换所有这些:
Encoding.UTF8.GetBytes(stringToBeHashed)
你怎么看?
【问题讨论】:
标签: c# character-encoding casting