2013-05-30 58 views
0

我正在使用SDK,要求將AVI編解碼器作爲FourCC值的8位int表示形式傳遞。 FourCC的值是WVC1,我試圖將ASCII轉換爲每個字符的相應的int值,我認爲這是87864301,但這是不正確的。FourCC as int C#

有誰知道是否有代表FourCC值的整數值的標準集合,或某種方式來轉換它?

+0

這是否幫助:http://msdn.microsoft.com/en-us/library/windows/desktop/dd375802(v=vs.85).aspx – DonBoitnott

回答

2

http://msdn.microsoft.com/en-us/library/windows/desktop/dd375802(v=vs.85).aspx 建議FOURCC的字符需要爲十六進制值,並在轉換之前反轉。

下面是一個示例控制檯應用程序,使用它匹配YUY2(WVC1 = 31435657)的值。 更新後的代碼包含big/little endian和GUID轉換。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace FourCC 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string fourCC = "YUY2"; 

      Console.WriteLine("Big endian value of {0} is {1}", fourCC, ConvertFourCC(fourCC, toBigEndian:true)); 
      Console.WriteLine("Little endian value of {0} is {1}", fourCC, ConvertFourCC(fourCC)); 
      Console.WriteLine("GUID value of {0} is {1}", fourCC, ConvertFourCC(fourCC, toGuid:true)); 
      Console.ReadKey(); 

     } 

     static string ConvertFourCC(string fourCC, bool toBigEndian = false, bool toGuid = false) 
     { 
      if (!String.IsNullOrWhiteSpace(fourCC)) 
      { 
       if (fourCC.Length != 4) 
       { 
        throw new FormatException("FOURCC length must be four characters");      
       } 
       else 
       { 
        char[] c = fourCC.ToCharArray(); 

        if (toBigEndian) 
        { 
         return String.Format("{0:X}", (c[0] << 24| c[1] << 16 | c[2] << 8 | c[3])); 
        } 
        else if (toGuid) 
        { 
         return String.Format("{0:X}", (c[3] << 24) | (c[2] << 16) | (c[1] << 8) | c[0]) + "-0000-0010-8000-00AA00389B71"; 
        } 
        else 
        { 
         return String.Format("{0:X}", (c[3] << 24) | (c[2] << 16) | (c[1] << 8) | c[0]); 
        } 
       } 
      } 
      return null; 
     } 
    } 
} 
+0

謝謝!這是最有幫助的 – user1934821