转换一个4字符字符串转换成INT32 [英] Convert a 4 char string into int32

查看:158
本文介绍了转换一个4字符字符串转换成INT32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有4字符转换成32位的int类型的快速方法?我知道我可以遍历它像:

Is there a fast way to convert 4 chars into a 32bit int? I know i can loop through it like :

string key = "ABCD";
int val = 0;
for (int i = 0; i < 4; i++)
{
    int b = (int)key[i] * (int)Math.Pow(256, i);
    val += b;
}
// val = 1145258561



我想的东西较低的水平,我所知道的字符被存储为字节。因为我基本上想写一个4字符字符串整数指针的位置,我不如果不安全的代码介意。

I would like something lower level, I know the chars are stored as bytes. I don't mind if its unsafe code because I am basically trying to write a 4 char string to an integer pointer location.

推荐答案

你可以先使用适当的编码字符串转换为字节数组(请参见 <强> Encoding.GetEncoding ),那么你可以使用的 BitConverter.ToInt32 的字节数组转换为。整数

You can first convert the string to a byte array using an appropriate encoding (see Encoding.GetEncoding) then you can use BitConverter.ToInt32 to convert the byte array to an integer.

string s = "ABCD";
byte[] bytes = encoding.GetBytes(s);  /* Use the correct encoding here. */
int result = BitConverter.ToInt32(bytes, 0);



结果:

Result:

1145258561

要取回从整数字符串你根本相反的过程:

To get back the string from the integer you simply reverse the process:

int i = 1145258561;
byte[] bytes = BitConverter.GetBytes(i);
string s = encoding.GetString(bytes);



结果:

Result:

ABCD

注意BitConverter类给出的结果是依赖的字节顺序它的机器上运行。如果你想要的代码是平台无关的,你可以看看EndianBitConverter中的乔恩斯基特 MiscUtil 库。

Note that BitConverter class gives a result which is dependant on the endianness of the machine it is running on. If you want the code to be platform independent you could look at EndianBitConverter in Jon Skeet's MiscUtil library.

我测试了三种实现的性能:

I tested the performance of three implementations:

Math.Pow

int convert1(string key)
{
    int val = 0;
    for (int i = 0; i < 4; i++)
    {
        int b = (int)key[i] * (int)Math.Pow(256, i);
        val += b;
    }
    return val;
}



BitConverter

int convert2(string key)
{
    byte[] bytes = encoding.GetBytes(key);
    int result = BitConverter.ToInt32(bytes, 0);
    return result;
}



位移

int convert3(string key)
{
    int val = 0;
    for (int i = 3; i >= 0; i--)
    {
        val <<= 8;
        val += (int)key[i];
    }
    return val;
}



循环展开

int convert4(string key)
{
    return (key[3] << 24) + (key[2] << 16) + (key[1] << 8) + key[0];
}



结果



跌幅最大的是最佳性能:

Results

Biggest is best performance:


Method         Iterations per second
------------------------------------
Math.Pow                      690000
BitConverter                 2020000
Bit shifting                 4940000
Loop unrolled                8040000

结论

如果性能是至关重要的,然后写你自己的方法做到位移位得到最好的性能。使用标准类BitConverter是可能是在性能并不重要(假设你不介意它仅适用于小端计算机上)大多数情况下的罚款。

If performance is critical then writing your own method to do bit shifting gets the best performance. Using the standard class BitConverter is probably fine for most situations where performance is not critical (assuming that you don't mind that it only works on little endian computers).

这篇关于转换一个4字符字符串转换成INT32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆