将 HEX 转换为(扩展的)ASCII 0-255,不使用 DOS C# 的 UTF
Converting HEX to (extended) ASCII 0-255 with NO UTF for DOS C#
首先,当我问这么长的问题时,我不想成为 'That Guy',尽管我知道有人以不同的方式问了很多次,但我在获得正确存储在字符串中的日期格式。
一些小背景。
我正在使用需要以 8 个字符的十六进制格式存储的 DOS 文件时间日期格式 - 如下所示:https://doubleblak.com/blogPosts.php?id=7
简而言之,时间和日期被捕获,然后运行以二进制位编码,然后转换为十六进制。
我现在需要做的是将这些十六进制值存储为字符串,并能够将它们传递给 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 个字符 运行ge.
"\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 和不同的方法,例如使用字符串生成器、字符、字节等,有时我会得到日期和时间在值正确的地方标记,其中十六进制值不超过扩展的 ASCII 运行ges,但随后我再次 运行 它,然后我回到方块 1。它总是插入无论我做什么,这些扩展值作为 UTF8 条目和中断。
我确定 VS 中没有错误,但我 运行ning 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" );
二进制数据还有另一种方法:
var bytes = new byte[4] { 0xFF, 0, 0, 0xFF };
ape_tag.SetItem(new TagLib.Ape.Item("XLastPlayed", new ByteVector(bytes)));
不清楚是否需要转换方法from/to DOS FileTime:
public static uint ToDosFileTimeDate(DateTime dt)
{
ushort date = (ushort)(((dt.Year - 1980) << 9) | (dt.Month << 5) | (dt.Day));
ushort time = (ushort)((dt.Hour << 11) | (dt.Minute << 5) | (dt.Second >> 1));
uint dateTime = ((uint)date << 16) | time;
return dateTime;
}
public static DateTime FromDosFileTimeDate(uint ui)
{
ushort date = (ushort)(ui >> 16);
ushort time = (ushort)(ui & 0xFFFF);
var year = (date >> 9) + 1980;
var month = (date >> 5) & 0xF;
var day = date & 0x1F;
var hour = time >> 11;
var minute = (time >> 5) & 0x3F;
var second = (time & 0x1F) << 1;
return new DateTime(year, month, day, hour, minute, second, DateTimeKind.Local);
}
并将 uint
转换为 byte[4]
数组有
uint ui = BitConverter.ToUInt32(bytes);
和
byte[] bytes = BitConverter.GetBytes(ui);
首先,当我问这么长的问题时,我不想成为 'That Guy',尽管我知道有人以不同的方式问了很多次,但我在获得正确存储在字符串中的日期格式。
一些小背景。
我正在使用需要以 8 个字符的十六进制格式存储的 DOS 文件时间日期格式 - 如下所示:https://doubleblak.com/blogPosts.php?id=7
简而言之,时间和日期被捕获,然后运行以二进制位编码,然后转换为十六进制。
我现在需要做的是将这些十六进制值存储为字符串,并能够将它们传递给 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 个字符 运行ge.
"\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 和不同的方法,例如使用字符串生成器、字符、字节等,有时我会得到日期和时间在值正确的地方标记,其中十六进制值不超过扩展的 ASCII 运行ges,但随后我再次 运行 它,然后我回到方块 1。它总是插入无论我做什么,这些扩展值作为 UTF8 条目和中断。
我确定 VS 中没有错误,但我 运行ning 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" );
二进制数据还有另一种方法:
var bytes = new byte[4] { 0xFF, 0, 0, 0xFF };
ape_tag.SetItem(new TagLib.Ape.Item("XLastPlayed", new ByteVector(bytes)));
不清楚是否需要转换方法from/to DOS FileTime:
public static uint ToDosFileTimeDate(DateTime dt)
{
ushort date = (ushort)(((dt.Year - 1980) << 9) | (dt.Month << 5) | (dt.Day));
ushort time = (ushort)((dt.Hour << 11) | (dt.Minute << 5) | (dt.Second >> 1));
uint dateTime = ((uint)date << 16) | time;
return dateTime;
}
public static DateTime FromDosFileTimeDate(uint ui)
{
ushort date = (ushort)(ui >> 16);
ushort time = (ushort)(ui & 0xFFFF);
var year = (date >> 9) + 1980;
var month = (date >> 5) & 0xF;
var day = date & 0x1F;
var hour = time >> 11;
var minute = (time >> 5) & 0x3F;
var second = (time & 0x1F) << 1;
return new DateTime(year, month, day, hour, minute, second, DateTimeKind.Local);
}
并将 uint
转换为 byte[4]
数组有
uint ui = BitConverter.ToUInt32(bytes);
和
byte[] bytes = BitConverter.GetBytes(ui);