none
C#中如何将如下这段代码中的chirp转换为16位二进制数据,并取后12位写入文件呢? RRS feed

  • 问题

  • using (FileStream fs = File.Create(filePath))//优化 { double beta = 900000d/4.194304d; double chirp = 0; 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; byte[] b=Encoding.Unicode.GetBytes(Convert.ToString(chirp,2)); fs.Write(b, 0, b.Length); } }

    以上代码中的chirp是十进制的


    2017年10月20日 1:27

全部回复

  • Hi,

    你有尝试过这个方法吗?

    Convert.ToString Method (Int32, Int32)

    你可以尝试测试下面这段代码。

    int[] bases = { 2, 8, 10, 16};
    int[] numbers = { Int32.MinValue, -19327543, -13621, -18, 12, 
                                 19142, Int32.MaxValue };
    
    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));
       }
    

    这个函数第二个参数就是指定是什么进制的数。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年10月20日 7:34
    版主