C# 如何在c中转义变量#

C# 如何在c中转义变量#,c#,hex,C#,Hex,我希望标题足够清楚,但我想要的是: string hello = "0012"; string escapedHello = "\u" + hello; 原因是,该程序用于在brother ptouch打印机上打印标签,该打印机存储了一个模板,它可以打印文件中的变量,我将用我的程序创建 我的程序现在可以运行了,但不是“最聪明”的方式。现在它有一定的长度,所以我在值上附加了一些空格,它们没有达到所需的长度。 值的长度以十六进制存储,现在,我用以下方式写入值

我希望标题足够清楚,但我想要的是:

string hello = "0012";
string escapedHello = "\u" + hello;
原因是,该程序用于在brother ptouch打印机上打印标签,该打印机存储了一个模板,它可以打印文件中的变量,我将用我的程序创建

我的程序现在可以运行了,但不是“最聪明”的方式。现在它有一定的长度,所以我在值上附加了一些空格,它们没有达到所需的长度。 值的长度以十六进制存储,现在,我用以下方式写入值长度:

\u0004
我想获取我通过网络获得的价值观。我的程序,得到长度,并将其写为十六进制,在文件中,因为这样长度将是动态的,打印机打印的文本将更易于阅读

以下是我的程序代码:

    static void Main(string[] lines)
    {

        //Directory.SetCurrentDirectory(@"C:\HireLabel");
        //string[] lines = File.ReadAllLines("var.txt");
        string[] hexstrings = new string[lines.Length-1];

        string[] replaceWith = { "Modelnavn1111111", "Variant111111111111111111111", "Bchass", "Nmplade", "Year", "color1111111111111" };
        string binFile = "\u001bia\u0003^II^TS001^ONModel\0^DI\u0010\0Modelnavn1111111^ONVariant\0^DI\u001c\0Variant111111111111111111111^ONbchass\0^DI\u0006\0Bchass^ONnmplade\0^DI\u0007\0Nmplade^ONyear\0^DI\u0004\0Year^ONColor\0^DI\u0012\0color1111111111111^FF";


        for (int i = 0; i <replaceWith.Length - 1; i++)
        {
            hexstrings[i] = @"\u" + lines[i].Length.ToString("X4");
            Console.WriteLine(hexstrings[i]);
        }

        for (int i = 0; i < replaceWith.Length; i++)
        {
            while (replaceWith[i].Length > lines[i].Length)
            {
                lines[i] += " ";
            }

            binFile = binFile.Replace(replaceWith[i], lines[i]);
        }

        //File.WriteAllText("C:\\HireLabel\\print.bin", binFile);
        //Process.Start("cmd.exe", "/C C:\\HireLabel\\spool.exe C:\\HireLabel\\print.bin " + "\"" + lines[replaceWith.Length] + "\"");


    }
static void Main(字符串[]行)
{
//SetCurrentDirectory(@“C:\HireLabel”);
//string[]lines=File.ReadAllLines(“var.txt”);
string[]hextstrings=新字符串[lines.Length-1];
字符串[]替换为={“Modelnavn1111111”、“variant111111111”、“Bchass”、“Nmplade”、“Year”、“color1111111”};
字符串binFile=“\u001bia\u0003^II^TS001^ONModel\0^DI\u0010\0Modelnavn1111111^ONVariant\0^DI\u001c\0variant111111111111111^ONbchass\0^DI\u0006\0Bchass^ONnmplade\0^DI\u0007\0Nmplade^ONyear\0^DI\u0004\0Year^ONColor\0^DI\u0012\0color111111111^FF”;
对于(int i=0;i行[i]。长度)
{
行[i]+=”;
}
binFile=binFile.Replace(替换为[i],行[i]);
}
//writealText(“C:\\HireLabel\\print.bin”,binFile);
//Process.Start(“cmd.exe”,“/C:\\HireLabel\\spool.exe C:\\HireLabel\\print.bin”+“\”+行[replaceWith.Length]+“\”);
}

程序现在就写,在控制台中写一个例子“0007”,它应该给出一些看起来像wierd的中文字母,而不是数字。但是数字是十六进制的数字,因此该部分被正确转换。

不太清楚您想要实现什么。您是否希望
escapedHello
包含单个字符“\u0012”?@KlausGütter正是:)对我来说,使用字符串的长度来确定要写入的unicode字符是没有意义的。我错过什么了吗?如果您只想将长度写入十六进制,请删除
\u
打印机使用十六进制来限定单词的长度,因此现在,长度是“硬编码”的,这意味着文本有时不必要的小:)这将是
var hello=0x0012;string escapedHello=新字符串((char)hello,1)
您没有编写文本文件,因此
file.WriteAllText
不合适。