c#hex byte 0x09(ascii - > tab)to“\t”串 [英] c# hex byte 0x09 (ascii -> tab) to "\t" string

查看:185
本文介绍了c#hex byte 0x09(ascii - > tab)to“\t”串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要到一个文本文件的字节数组转换成它的字符串的字符表示。

I need to convert a byte array of a text file to it's string character representation.

例如,如果我有了一个文本文件:

For example, if I have a text file that has:

您好(标签)有(新行)朋友

hello (tab) there (newline) friend

我想将其转换成一个数组:

I would like to convert that to an array:

my_array  = {'h', 'e' ,'l','l','o', '\t', 't', 'h','e','r','e', '\r','\n', 'f', 'r' ,'i','e','n', 'd'};



我在与转换控制字符转义的字符串,麻烦即:

I'm having trouble with converting the control characters to their escaped strings, i.e.:


  • ×09 ='\t';

  • 0X0D ='\r';

  • 的0x0A ='\\\
    ';

我已经试过这一点,但制表符和新行不在这里代表:

I have tried this, but the tabs and new lines aren't represented here:

byte[] text_bytes = File.ReadAllBytes("ok.txt");
char[] y = Encoding.ASCII.GetChars(text_bytes);



我知道我可以通过每个字节的循环,并有一个条件,寻找×09 ,如果我找到它,然后用\t更换,但我不知道是否有内置的东西。

I know I can just loop through each byte and have a condition to look for 0x09 and if I find it, then replace with "\t", but I'm wondering if there is something built in.

推荐答案

有几种方法可以做到这一点。最简单的将是整个文件加载到内存中:

There are several ways you could do it. The simplest would be to load the entire file into memory:

string theText = File.ReadAllText(filename);



然后使用与string.replace 来代替你感兴趣的内容:

Then use string.Replace to replace the items you're interested in:

// "escaping" the '\t' with '\\t' makes it write the literal characters '\' and 't'
theText = theText.Replace("\t", "\\t");

theText = theText.Replace("\r", "\\r");
theText = theText.Replace("\n", "\\n");



然后,你可以创建你的字符数组。如果你确定它的所有ASCII文本,你可以使用 Encoding.ASCII

byte[] theChars = Encoding.ASCII.GetBytes(theText);



或者,如果你想要一个字符数组:

Or, if you want a character array:

char[] theChars = theText.ToCharArray();



这可能将是足够快的目的。您也许能够通过通过字符串单传,由性格和复制到的StringBuilder 读取字符以加快其速度:

That's probably going to be fast enough for your purposes. You might be able to speed it up by making a single pass through the string, reading character by character and copying to a StringBuilder:

StringBuilder sb = new StringBuilder(theText.Length);
foreach (char c in theText)
{
    switch (c)
    {
        case '\t' : sb.Append("\\t"); break;
        case '\r' : sb.Append("\\r"); break;
        case '\n' : sb.Append("\\n"); break;
        default : sb.Append(c); break;
    }
}

byte[] theChars = Encoding.ASCII.GetBytes(sb.ToString());

这篇关于c#hex byte 0x09(ascii - > tab)to“\t”串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆