简体   繁体   English

将.mjpeg文件的十六进制数据另存为C#中的.mjpeg格式文件

[英]Save hex datas of .mjpeg file as a .mjpeg formatted file in C#

I have a text file filled with hex datas(like "FFD8FE00..") of a .mjpeg formatted file. 我有一个文本文件,里面装有.mjpeg格式文件的十六进制数据(例如“ FFD8FE00 ..”)。 I have to play it with a converter. 我必须和转换器一起玩。 So, i am trying to write the data in a .mjpeg file with these lines: 因此,我正在尝试使用以下行将数据写入.mjpeg文件中:

string myData  = File.ReadAllText("hexData.txt");
string newData;
int remainder  = myData.Length%500;
byte[] data_toWrite=newByte[250];

for(int i=0;i<myData.Length-remainder; i+=500)
{
    newData     = myData.Substring(i,500);
    data_toWrite = StringToByteArray(newData);
    File.WriteAllBytes("video.mjpeg",data_toWrite);
}

newData     = myData.Substring(myData.Length-remainder,remainder);
data_toWrite = StringToByteArray(newData);
File.WriteAllBytes("video.mjpeg",data_toWrite);

public static byte[] StringToByteArray(String hex)
{
  int NumberChars = hex.Length;
  byte[] bytes = new byte[NumberChars / 2];
  for (int i = 0; i < NumberChars; i += 2)
  bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  return bytes;
}

But i couldn't make it play. 但我无法发挥作用。 I don't know where I am wrong at. 我不知道我在哪里错。 I tried to convert newData to ascii then byte array but it failed,too. 我试图将newData转换为ascii然后是字节数组,但是它也失败了。

Any ideas,many thanks! 任何想法,非常感谢!

Kane 凯恩

This 这个

File.WriteAllBytes("video.mjpeg",data_toWrite);

overwrites the file every time, not appends. 每次都覆盖文件,而不是追加。

I'm sure better code can be written, but this should be enough: 我确信可以编写更好的代码,但这应该足够了:

string input = "test.hex";
string output = "output.bin";

using (var sr = new StreamReader(input))
using (var fs = File.Create(output))
{
    // We accumulate the 2 hex digits needed for a byte here
    string h = string.Empty;

    while (true)
    {
        int ch1 = sr.Read();

        if (ch1 == -1)
        {
            // The file finished but we have a pending partial hex code
            if (h.Length == 1)
            {
                throw new Exception("Malformed file");
            }

            break;
        }

        char ch2 = (char)ch1;

        // Skip white space and end-of-line
        if (char.IsWhiteSpace(ch2))
        {
            continue;
        }

        h += ch2;

        // We have collected 2 hex digits, so we have 1 byte
        if (h.Length == 2)
        {
            byte b = Convert.ToByte(h, 16);
            fs.WriteByte(b);
            h = string.Empty;
        }
    }
}

Note that both StreamReader and File.Create (that returns a FileStream ) do some buffering, so no explicit buffering is needed. 请注意, StreamReaderFile.Create (返回FileStream )都进行一些缓冲,因此不需要显式缓冲。 My hands are quivering because they want to remove the string h buffer and do the parsing hex digit by hex digit directly in the byte b . 我的手在颤抖,因为他们想删除string h缓冲区并直接在byte b逐个进行十六进制数字解析。 But I'll try to not overcomplicate the code :-) 但是我将尽量不使代码过于复杂:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM