string Convert(string inFileName,string outFilename )
{
System.IO.FileStream outFile,inFile;
int j;
   
outFile=new System.IO.FileStream(outFileName,System.IO.FileMode.Create,System.IO.FileAccess.Write) ;
string str= "0605041582000064F00000480E01";
outFile.Write(str); inFile=new System.IO.FileStream(inFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read) ; if(inFile==null)//本判断可能有问题
{
System.Console.Write(inFileName+" Input File Open Error!\n");
return "";
} int b; for( b = 0;b < 14;b++)
{
if(inFile.Length>118+36*(13-b))
{
inFile.Position=118+36*(13-b);
//BEGIN
byte[] tmp=new byte[4];
for(int i = 0;i < 9; i++) 
{
if(inFile.Read(tmp,0,4)>0)
{
int tmpHex = Str2Hex(tmp.ToString()); //可以直接写入,本语句多余
outFile.Write(tmp,0,4);
//fprintf(outFile, "%02x", tmpHex);//直接写入字节流即可,不必要使用
}
else
break;
}
//END
//以上BEGIN和END之间的语句可以使用下面的语句:
//
//byte[] tmp=new byte[36];
//int readLen=inFile.Read(tmp,0,36);
//if(readLen>0)
// outFile.Write(tmp,0,readLen);
//
//代替
}
} outFile.Close();
inFile.Close();
  
System.IO.FileStream otbstr=new System.IO.FileStream(outFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read); if(otbstr==null)//本判断可能有问题
{
System.Console.Write(OutFileName+" Input File Open Error!\n");
return "";
} //BEGIN
byte[] buffer=new byte[280];
int  i;
byte ch; ch = otbstr.ReadByte();
for( i=0; (i < 280 ) && ( otbstr.Position<otbstr.Length); i++ )
{
buffer[i] = ch;
ch = otbstr.ReadByte();
}
//END
//上面BEGIN和END之间的代码可以简单的使用:otbstr.Read(buffer,0,buffer.Length);实现 System.Console.WriteLine(buffer.ToString());
    
otbstr.Close();  return buffer.ToString();
} int Str2Hex(string str)
{
int hex = 0;
for(int i = 3; i >= 0; i--) 

int lowByte = ((int)(str.Substring(i,1)))%16;
hex += lowByte * pow(2, 8 - ( i + 1 ) * 2); //pow功能不明
int highByte = ((int)(str.Substring(i,1)))/16;
hex += highByte * pow( 2, 8 - ( i + 1 ) * 2 + 1); //pow功能不明
}
return hex;
}
[STAThread]
static void Main(string[] args) //C#中的主函数,只有一个参数,必须是静态的
{
string infilename = "test3.bmp";
string outfilename = "bmp2yyr";
Convert(infilename, outfilename);
}

解决方案 »

  1.   

    以上翻译没有测试,可能存在一个或多个错误,请自行参考.NET帮助更正。
      

  2.   

    string Convert(string inFileName,string outFilename )
    {
    System.IO.FileStream outFile,inFile;
    int j; outFile=new System.IO.FileStream(outFileName,System.IO.FileMode.Create,System.IO.FileAccess.Write) ;
    string str= "0605041582000064F00000480E01";
    outFile.Write(str); inFile=new System.IO.FileStream(inFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read) ; if(inFile==null)//本判断可能有问题
    {
    System.Console.Write(inFileName+" Input File Open Error!\n");
    return "";
    } int b; for( b = 0;b < 14;b++)
    {
    if(inFile.Length>118+36*(13-b))
    {
    inFile.Position=118+36*(13-b);
    //BEGIN
    byte[] tmp=new byte[4];
    for(int i = 0;i < 9; i++) 
    {
    if(inFile.Read(tmp,0,4)>0)
    {
    int tmpHex = Str2Hex(tmp.ToString()); //可以直接写入,本语句多余
    outFile.Write(tmp,0,4);
    //fprintf(outFile, "%02x", tmpHex);//直接写入字节流即可,不必要使用
    }
    else
    break;
    }
    //END
    //以上BEGIN和END之间的语句可以使用下面的语句:
    //
    //byte[] tmp=new byte[36];
    //int readLen=inFile.Read(tmp,0,36);
    //if(readLen>0)
    // outFile.Write(tmp,0,readLen);
    //
    //代替
    }
    } outFile.Close();
    inFile.Close(); System.IO.FileStream otbstr=new System.IO.FileStream(outFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read); if(otbstr==null)//本判断可能有问题
    {
    System.Console.Write(OutFileName+" Input File Open Error!\n");
    return "";
    } //BEGIN
    byte[] buffer=new byte[280];
    int  i;
    byte ch; ch = otbstr.ReadByte();
    for( i=0; (i < 280 ) && ( otbstr.Position<otbstr.Length); i++ )
    {
    buffer[i] = ch;
    ch = otbstr.ReadByte();
    }
    //END
    //上面BEGIN和END之间的代码可以简单的使用:otbstr.Read(buffer,0,buffer.Length);实现 System.Console.WriteLine(buffer.ToString()); otbstr.Close();  return buffer.ToString();
    }int Str2Hex(string str)
    {
    int hex = 0;
    for(int i = 3; i >= 0; i--) 

    int lowByte = ((int)(str.Substring(i,1)))%16;
    hex += lowByte * pow(2, 8 - ( i + 1 ) * 2); //pow功能不明
    int highByte = ((int)(str.Substring(i,1)))/16;
    hex += highByte * pow( 2, 8 - ( i + 1 ) * 2 + 1); //pow功能不明
    }
    return hex;
    }
    [STAThread]
    static void Main(string[] args) //C#中的主函数,只有一个参数,必须是静态的
    {
    string infilename = "test3.bmp";
    string outfilename = "bmp2yyr";
    Convert(infilename, outfilename);
    }
      

  3.   

    呵呵,我不知道这个例子的功能,所以我只是对数据简单的翻译。或许就是上面中pow函数的功用吧。
    这样,还是要使用下面的代码:byte[] tmp=new byte[4];
    for(int i = 0;i < 9; i++) 
    {
    if(inFile.Read(tmp,0,4)>0)
    {
    int tmpHex = Str2Hex(tmp.ToString()); //可以直接写入,本语句多余
    outFile.Write(tmp,0,4);
    //fprintf(outFile, "%02x", tmpHex);//直接写入字节流即可,不必要使用
    }
    else
    break;
    }不过要修改为:byte[] tmp=new byte[4];
    for(int i = 0;i < 9; i++) 
    {
    if(inFile.Read(tmp,0,4)>0)
    {
    string tmpHex = Str2Hex(tmp.ToString()).ToString(); 
    tmpHex="0000".Substring(1,4-tmpHex.Length)+tmpHex;
    outFile.Write(tmpHex.ToCharArray(),0,4);
    //fprintf(outFile, "%02x", tmpHex);//直接写入字节流即可,不必要使用
    }
    else
    break;
    }
      

  4.   

    以上代码是用来将bmp文件转换为手机图片格式的。我已经自己写了个别的算法,不用以上算法。谢谢诸位帮助。