我有一個簡單的功能,抓住從C硬盤序列號:\驅動器,並把它轉換成字符串:寫作硬盤序列串爲二進制文件
然後我試圖轉換將上面的字符串轉換成ASCII碼,然後將其寫入二進制文件,我遇到的問題是,在轉換此字符串並使用streamwriter保存文件並在十六進制編輯器中打開該文件時,我看到更多的字節我原本想這樣寫例如「16342D1F4A61BC」
將出現爲:08 16 34 2d 1f 4a 61 c2 bc
它添加在那裏不知何故08和C2 ......
更完整版如下:
string constructor2 = "16342D1F4A61BC";
string StrValue = "";
while (constructor2.Length > 0)
{
StrValue += System.Convert.ToChar(System.Convert.ToUInt32(constructor2.Substring(0, 2), 16)).ToString();
// Remove from the hex object the converted value
constructor2 = constructor2.Substring(2, constructor2.Length - 2);
}
FileStream writeStream;
try
{
writeStream = new FileStream(Path.GetDirectoryName(Application.ExecutablePath) + "\\license.mgr", FileMode.Create);
BinaryWriter writeBinay = new BinaryWriter(writeStream);
writeBinay.Write(StrValue);
writeBinay.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
誰能幫助我瞭解這些在獲取添加?
你不能直接寫輸出? – tofutim 2011-05-27 17:34:17
while循環的目的是什麼?有一個方便的Encoding.ASCII.GetBytes(字符串),它會從任何字符串返回必要的字節。 – Joe 2011-05-27 17:34:22
我想你是說當你以十六進制查看你的輸出文件時,你想看到硬盤ID的十六進制表示。 – tofutim 2011-05-27 17:48:56