如何在c#中解码文件的内容

时间:2012-01-05 04:17:44

标签: c# .net

我在.net中读取文件。这个文件是编码格式的。现在我必须解码文件。你能帮我在C#中怎么做吗?

我的编码文件格式是

DARHnAyyakAALdDALdDALWGALY.AAAEQWAAAHQO
DARJMAyyakAALshALshALjEALoTAAAEA4AAAKcI
DARKwAyyakAAL2wAL2wALvkAL0VAAABJIAAAD$O
DARHnAyyvqAALXCALbkALUYALYXAAAD3MAAAHA6
DARJMAyyvqAALnXALrRALiwALmgAAAEmsAAAKUK
DARKwAyyvqAALzeAL3EALtdALxIAAABFYAAAEBC
DARHnAyzEwAALXlALXlALP7ALU7AAAEpMAAAG5Q
DARJMAyzEwAALlkALlkALeEALjYAAAGysAAAKTs

我必须在c#中解码这个文件。请帮助我..我在java中有逻辑。但我不知道在c#中做的很热。

private void update(String s){
int exp = decode(s.substring(1,5));
exp += 20000000;
int dt = decode(s.substring(5,11));
int op = decode(s.substring(11,15));
int hi = decode(s.substring(15,19));
int lo = decode(s.substring(19,23));
int cl = decode(s.substring(23,27));
int v = decode(s.substring(27,33));
int ni = decode(s.substring(33,39));
append(""+exp,dt, op/100.0f, hi/100.0f, lo/100.0f, cl/100.0f, v, ni);
}

private int decode(String s)
{
int n = 0;
for (int i = 0; i < s.length(); i++)
{
  int c = (int)s.charAt(i);
  if (c >= (int)'A' && c <= (int)'Z')
    c = c - (int)'A';
  else if (c >= (int)'a' && c <= (int)'z')
    c = c - (int)'a' + 26;
  else if (c >= (int)'0' && c <= (int)'9')
    c = c - (int)'0' + 52;
  else if (c == (int)'$')
    c = 62;
  else if (c == (int)'.')
    c = 63;
  else
    c = 0;
  n <<= 6;
  n += c;
}
return n;
}

1 个答案:

答案 0 :(得分:0)

这几乎直接翻译成c#:

   private void update(String s)
    {
        int exp = decode(s.Substring(1, 5));
        exp += 20000000;
        int dt = decode(s.Substring(5, 11));
        int op = decode(s.Substring(11, 15));
        int hi = decode(s.Substring(15, 19));
        int lo = decode(s.Substring(19, 23));
        int cl = decode(s.Substring(23, 27));
        int v = decode(s.Substring(27, 33));
        int ni = decode(s.Substring(33, 39));
        //append(""+exp,dt, op/100.0f, hi/100.0f, lo/100.0f, cl/100.0f, v, ni);
    }

    private int decode(String s)
    {
        int n = 0;
        for (int i = 0; i < s.Length; i++)
        {
            int c = (int)s[i];
            if (c >= (int)'A' && c <= (int)'Z')
                c = c - (int)'A';
            else if (c >= (int)'a' && c <= (int)'z')
                c = c - (int)'a' + 26;
            else if (c >= (int)'0' && c <= (int)'9')
                c = c - (int)'0' + 52;
            else if (c == (int)'$')
                c = 62;
            else if (c == (int)'.')
                c = 63;
            else
                c = 0;
            n <<= 6;
            n += c;
        }
        return n;
    }

我唯一不确定的是追加声明。不确定是否应该将数字加在一起或从中构建字符串。

假设它是一个字符串,它将类似于:

        var sbText = new System.Text.StringBuilder(500);
        sbText.Append(exp).Append(dt).Append(op/100.0f).Append(hi/100.0f).Append(lo/100.0f).Append(cl/100.0f).Append(v).Append(ni);
        var sText = sbText.ToString();