【发布时间】:2021-04-05 21:06:33
【问题描述】:
首先,当我问这个长长的问题时,我不想成为“那个人”,即使我知道它已经以不同的方式被问过很多次了,但是我在获取日期格式时遇到了很大的问题正确存储在字符串中。
一些次要背景。
我正在使用需要以 8 个字符的十六进制格式存储的 DOS FileTime 日期格式 - 如下所示:https://doubleblak.com/blogPosts.php?id=7
简单来说就是捕获时间和日期,然后按二进制位排列,再转换成HEX。
我现在需要做的是,将这些 HEX 值存储为字符串,并能够将它们传递给 tagLib sharp 以在 MP3 文件中编写自定义 APE 标签。说起来容易做起来难……
编写自定义标签很简单,基本上就是这样:
TagLib.File file = TagLib.File.Create(filename);
TagLib.Ape.Tag ape_tag = (TagLib.Ape.Tag)file.GetTag(TagLib.TagTypes.Ape, true);
// Write - for my example
/* declarations:
public void SetValue(string key, string value);
public void SetValue(string key, uint number, uint count);
public void SetValue(string key, string[] value);
*/
ape_tag.SetValue("XLastPlayed", history );
那么,进入实际问题:
将日期转换为正确的十六进制值后,我得到以下结果:
928C9D51
但是,为了使其正常工作并正确存储,我需要将其转换为 ASCII 值,以便 TagLibSharp 可以存储它。
如果我将其转换为 ASCII,则会得到以下信息:(这是错误的),因为它应该只有 4 个 ASCII 字符长 - 即使它们不可打印,或者位于 > 127 个字符范围内。
"\u0092\u008c\u009dQ"
您可以在此图中看到已存储的额外 HEX 值,这是不正确的。
这是我一直在尝试使用的代码示例(以各种形式)以使其正常工作。
string FirstHistory = "7D8C9D51";
String test1 = "";
for (int i = 0; i < FirstHistory.Length; i += 2)
{
string hs = FirstHistory.Substring(i, 2);
var enc = Encoding.GetEncoding("iso-8859-1"); //.ASCII;// .GetEncoding(437);
var bytes1 = enc.GetBytes(string.Format("{0:x1}", Convert.ToChar(Convert.ToUInt16(hs, 16))));
string unicodeString = enc.GetString(bytes1);
Console.WriteLine(unicodeString);
test1 = test1 + unicodeString;
}
// needs to be "00 00 00 21" for the standard date array for this file format.
byte[] bytesArray = { 0, 0, 0, 33 }; // A byte array containing non-printable characters
string s1 = "";
string history = "";
// Basically what the history will look like
// "???!???!???!???!???!???!???!???!???!???!???!???!???!???!???!???!???!"
for (int i =0; i < 18; i++)
{
if(i==0) {
history = test1; // Write the first value.
}
s1 = Encoding.UTF8.GetString(bytesArray); // encoding on this string won't effect the array date values
history = history + s1;
}
ape_tag.SetValue("XLastPlayed", history );
我知道有多种编码,我基本上已经尝试了所有我能做的,并且已经阅读了一些东西,但我没有得到任何结果。
有时我认为我已经掌握了它,但是当我查看我正在保存的文件时,它会滑入一个“C2”十六进制值,而它不应该,这是破坏一切的 unicode。我已经包含了没有这些 C2 十六进制值的图像,您实际上可以看到 DOS 时间和日期时间在 HxD 十六进制查看器中正确显示。
我尝试了各种编码,例如 437、ios-8859-1、ASCII 和不同的方法,例如使用字符串生成器、字符、字节等,有时我会得到正确的日期和时间戳,其中 HEX 值不超过扩展的 ASCII 范围,但随后我再次运行它,我又回到了第 1 格。它总是将这些扩展值作为 UTF8 条目插入,并且无论我做什么都会中断。
我确定 VS 中没有错误,但我正在运行 Microsoft Visual Studio Community 2019,版本 16.8.2,如果这增加了案例的话。
我似乎找不到解决办法。有人对此有什么想法吗?
提前致谢。
***更新***
感谢@xanatos,这次更新
public static byte[] ConvertHexStringToByteArray(string str)
{
Dictionary<string, byte> hexindex = new Dictionary<string, byte>();
for (int i = 0; i <= 255; i++)
hexindex.Add(i.ToString("X2"), (byte)i);
List<byte> hexres = new List<byte>();
for (int i = 0; i < str.Length; i += 2)
hexres.Add(hexindex[str.Substring(i, 2)]);
return hexres.ToArray();
}
string FirstHistory = "7D8C9D51";
string s1 = "";
string history = "";
byte[] bytes = { 0, 0, 33, 0 }; // A byte array contains non-ASCII (or non-readable) characters
for (int i =0; i < 18; i++)
{
s1 = Encoding.UTF8.GetString(bytes); // ???
history = history + s1;
}
var theArray_SO = ConvertHexStringToByteArray(FirstHistory);
ape_tag.SetItem(new TagLib.Ape.Item("XLastPlayed", (new TagLib.ByteVector(theArray_SO)) + history));
*** 更新 2 - 2021 年 1 月 30 日 ***
编辑其他值并重新保存后,我遇到了一些麻烦。似乎 TagLib 和自定义 APE 标签可能会导致数据损坏,专门针对此 ByteVector 数据。如果您只是使用 save 方法来编辑其他自定义值,那么这不是问题,但如果您有自定义值,这些值带有 ByteVector 值,您很可能会遇到麻烦。这是我仍然用于保存文件的内容。
TagLib.File file = TagLib.File.Create(filename);
// changes
file.save();
但是,为了克服这种数据损坏,我首先将文件作为 FileStream 读取(搜索)以找到我需要的值,然后将找到的值之后的 72 个字节的值放入一个新的字节数组中,然后保存它返回文件。
我发现通过字符串读取 ByteVector 数据非常失败,结果到处都是。
TagLib.Ape.Item item_Duration = ape_tag.GetItem("XLastScheduled");
虽然这可能会被重写一千种方式,但这是我的代码。
int foundlocation = 0;
int loop1 = 0;
byte[] sevenItems = new byte[80] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
string match = "XLastScheduled";
byte[] matchBytes = Encoding.ASCII.GetBytes(match);
{
using (var fs = new FileStream(filename, FileMode.Open))
{
int i = 0;
int readByte;
while ((readByte = fs.ReadByte()) != -1)
{
if (foundlocation == 0)
{
if (matchBytes[i] == readByte)
{
i++;
}
else
{
i = 0;
}
}
if (i == matchBytes.Length)
{
//Console.WriteLine("It found between {0} and {1}.", fs.Position - matchBytes.Length, fs.Position);
// set to true.
foundlocation = 1;
}
if (foundlocation==1)
{
//if (loop1 > 1)
{
// Start adding it at 2 bytes after it's found.
sevenItems[loop1] = (byte)readByte;
}
loop1++;
if(loop1 > 79)
{
fs.Close();
Console.WriteLine("Found the XLastScheduled data");
// 72/4 = 18 date/times
break;
}
}
// Then, I can save those values back as a vector byte array, instead of a string - hopefully...
}
fs.Close();
}
}
byte[] dst = new byte[sevenItems.Length - 8];
Array.Copy(sevenItems, 2, dst, 0, dst.Length);
TagLib.File file = TagLib.File.Create(filename);
// Get the APEv2 tag if it exists.
TagLib.Ape.Tag ape_tag = (TagLib.Ape.Tag)file.GetTag(TagLib.TagTypes.Ape, true);
// Save the new byteVector.
ape_tag.SetItem(new TagLib.Ape.Item("XLastScheduled", (new TagLib.ByteVector(dst))));
Console.WriteLine("XLastScheduled: set" );
【问题讨论】:
-
"因为它应该只有 4 个 ASCII 字符长" - 字符串
"\u0092\u008c\u009dQ"是 4 个字符长:例如'\u0092' 是单个字符(代码为 0x92) -
是的,但它不会以这种方式存储文件。 "\u0092" 将它们存储为 4 个字节,而不是 2 个字节...
-
我不知道 TaglibSharp 库以及如何在那里传递二进制标记值,但也许这会有所帮助:stackoverflow.com/questions/34507982/…
-
@KlausGütter,是的,这基本上就是我正在做的事情。不过感谢您的建议。