当前位置: 首页 > 电脑故障 > 系统故障 > 详细页面

如何把for循环里的chirp存到一个数组里呢?转化为16位二进制如何

时间:2017-10-21 14:02来源:未知 作者:admin

系统之家官网为您整理,转自:www.microsoft.com

问题描述:

public Form1()
        {
            InitializeComponent();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            string filePath = System.AppDomain.CurrentDomain.
                SetupInformation.ApplicationBase + @"cp.txt";//设置路径将文件保存在目标文件下

          
            double t0 = 0, f0 = 100000d;
                //double beta = (f1 - f0) / t1;
                double beta = 900000d/4.194304d;
                double chirp = 0;
            int c;
            int[] bases = { 2, 8, 10, 16 };
            int[] numbers = { Int32.MinValue, -19327543, -13621, -18, 12,
                             19142, Int32.MaxValue };
            using(FileStream fs = File.Create(filePath))
            {

            for (int i=0; i<=167772160; i++)
                {
                    chirp = Math.Cos(Math.PI * (2 * f0 + beta * t0) * t0);
                    t0 += 0.000000025;
                    chirp = Math.Round(chirp * 2046);
                    chirp = chirp*2 + 2046;
                    c = (int)chirp;
                foreach (int baseValue in bases)
                {
                    Console.WriteLine("Base {0} conversion:", baseValue);
                    foreach (int number in numbers)
                    {
                        Console.WriteLine("   {0,-15}  -->  0x{1}",
                                          number, Convert.ToString(number, baseValue));
                    }
                }

            }
            
            sw.Stop();
            MessageBox.Show("耗时为:  " + sw.ElapsedMilliseconds.ToString() + "  ms");

            }
        }
 


问题回答:
 

不太明白你的意思,你标题写的是16位二进制,也就是short,可代码中用的是txt文件,到底是写二进制到文件还是写文本到文件呢?不管怎样还是提供一个示例,isText为true时写文本false时写数据,你自己去选吧。当然这段代码只是示例,没有仔细测试过,可能有bug,请不要仅仅只是复制粘贴,了解一下思路就好。

static void Test(int length = 167772160, bool isText = true)
{
    Stopwatch sw = new Stopwatch();
    sw.Start();

    var group = Environment.ProcessorCount * 16;
    var size = length / group;
    group = length / size;
    if (length % group != 0) ++group;
    const double f0 = 2.0 * 100000.0;
    const double beta = 900000.0 / 4.194304;
    int bits = isText ? 4 : 2;
    using (var mmf = MemoryMappedFile.CreateFromFile(@"d:\Test." + (isText ? "txt" : "dat"), FileMode.OpenOrCreate, (new Guid()).ToString(), (long)length * bits))
    {
        using (var accessor = mmf.CreateViewAccessor())
        {
            Parallel.For(0, group, (int i) =>
            {
                var begin = i * size;
                var end = begin + size;
                if (end > length) end = length;
                double t0;
                var buffer = new byte[(end - begin) * bits];
                int chirp;
                if (isText)
                {
                    var data = new byte[bits];
                    int temp, n;
                    for (var j = 0; begin != end; ++begin, ++j)
                    {
                        t0 = begin * 0.000000025;
                        chirp = (int)Math.Round(Math.Cos(Math.PI * (2.0 * f0 + beta * t0) * t0) * 2046.0) * 2 + 2046;
                        for (n = 0; n != bits; ++n)
                        {
                            temp = chirp & 0xF;
                            data[n] = (byte)(temp + (temp < 10 ? 48 : 55));
                            chirp = chirp >> 4;
                        }
                        buffer[j] = data[1];
                        buffer[++j] = data[0];
                        buffer[++j] = data[3];
                        buffer[++j] = data[2];
                    }
                }
                else
                {
                    for (var j = 0; begin != end; ++begin, ++j)
                    {
                        t0 = begin * 0.000000025;
                        chirp = (int)Math.Round(Math.Cos(Math.PI * (2.0 * f0 + beta * t0) * t0) * 2046.0) * 2 + 2046;
                        buffer[j] = (byte)(chirp & 0xFF);
                        chirp = chirp >> 8;
                        buffer[++j] = (byte)(chirp & 0xFF);
                    }
                }
                accessor.WriteArray((long)i * size * bits, buffer, 0, buffer.Length);
            });
        }
    }

    sw.Stop();
    Debug.WriteLine("耗时为:" + sw.ElapsedMilliseconds.ToString() + "ms");
}

系统教程栏目