[英]Converting HEX to (extended) ASCII 0-255 with NO UTF for DOS C#
首先,當我問這個長問題時,我不想成為“那個家伙”,即使我知道有人以不同的方式問過很多次,但我在獲取日期格式以存儲在串正確。
一些小背景。
我正在使用需要以 8 個字符的十六進制格式存儲的 DOS FileTime 日期格式 - 如下所示: https ://doubleblak.com/blogPosts.php?id =7
簡而言之,時間和日期被捕獲,然后以二進制位排列,然后轉換為HEX。
我現在需要做的是,將這些十六進制值存儲為字符串,並能夠將它們傳遞給tagLib 銳利以在 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 data 。 如果您只是使用 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)));
不清楚您是否需要從/向 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.