简体   繁体   English

将char数组转换为字符串的问题

[英]Problems with converting char array to string

I have a function in a small application that I'm writing to break a recycled one-time pad cypher. 我在编写的一个小型应用程序中有一个功能,用于破坏回收的一次性填充密码。 Having used VB.NET for most of my career I thought it would be interesting to implement the app in C#. 在我职业生涯的大部分时间里都使用VB.NET,我认为用C#实现该应用程序会很有趣。 However, I have encountered a problem due to my present unfamiliarity with C#. 但是,由于我现在不熟悉C#,因此遇到了问题。

The function takes in two strings (of binary digits), converts these strings to char arrays, and then performs an XOR on them and places the result in a third char array. 该函数接收两个字符串(二进制数字),将这些字符串转换为char数组,然后对其进行XOR运算,然后将结果放入第三个char数组中。

This is fine until I try to convert the third char array to a string. 这很好,直到我尝试将第三个char数组转换为字符串为止。 Instead of the string looking like "11001101" etc, I get the following result: " \\0\\0 \\0 " ie the "1"s are being represented by spaces and the "0"s by "\\0". 代替看起来像“ 11001101”等的字符串,我得到以下结果:“ \\ 0 \\ 0 \\ 0”,即“ 1”由空格表示,“ 0”由“ \\ 0”表示。

My code is as follows: 我的代码如下:

    public string calcXor(string a, string b)
    {
        char[] charAArray = a.ToCharArray();
        char[] charBArray = b.ToCharArray();

        int len = 0;

        // Set length to be the length of the shorter string
        if (a.Length > b.Length)
            len = b.Length - 1;
        else
            len = a.Length - 1;

        char[] result = new char[len];

        for (int i = 0; i < len; i++)
        {
            result[i] = (char)(charAArray[i] ^ charBArray[i]);
        }

        return new string(result);
    }

Your problem is in the line 你的问题就在这里

  result[i] = (char)(charAArray[i] ^ charBArray[i]);

that should be 那应该是

  // (Char) 1 is not '1'!
  result[i] = (char)((charAArray[i] ^ charBArray[i]) + '0');

More compact solution is to use StringBuilder , not arrays: 更紧凑的解决方案是使用StringBuilder ,而不是数组:

public string calcXor(String a, String b) {
  int len = (a.Length < b.Length) ? a.Length : b.Length;

  StringBuilder Sb = new StringBuilder();

  for (int i = 0; i < len; ++i) 
    // Sb.Append(CharToBinary(a[i] ^ b[i])); // <- If you want 0's and 1's  
    Sb.Append(a[i] ^ b[i]); // <- Just int, not in binary format as in your solution

  return Sb.ToString();
}

public static String CharToBinary(int value, Boolean useUnicode = false) {
  int size = useUnicode ? 16 : 8;

  StringBuilder Sb = new StringBuilder(size);

  Sb.Length = size;

  for (int i = size - 1; i >= 0; --i) {
    Sb[i] = value % 2 == 0 ? '0' : '1';
    value /= 2;
  }

  return Sb.ToString();
}

Your solution just computes xor's (eg "65") and put them into line (eg 65728...); 您的解决方案只计算xor(例如“ 65”),然后将它们放在一起(例如65728 ...); if you want 0's and 1's representation, you should use formatting 如果要用0和1表示,则应使用格式

Have a look at the ASCII Table. 看一下ASCII表。 0 is the Null character \\0. 0是空字符\\ 0。 You could try ToString() 您可以尝试ToString()

Have you tried using binary / byte[]? 您是否尝试过使用二进制/字节[]? It seems like the fastest way to me. 这似乎是我最快的方法。

public string calcXor(string a, string b)
{
  //String to binary
  byte[] ab = ConvertToBinary(a);
  byte[] bb = ConvertToBinary(b);
  //(XOR)
  byte[] cb = a^b  

  return cb.ToString();
}

public static byte[] ConvertToBinary(string str)
{
  System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
  return encoding.GetBytes(str);
}

I just wanted to add that the solution I eventually chose is as follows: 我只是想补充一点,我最终选择的解决方案如下:

//Parameter binary is a bit string
public void someroutine(String binary) 
{
 var data = GetBytesFromBinaryString(binary);
 var text = Encoding.ASCII.GetString(data);
}

public Byte[] GetBytesFromBinaryString(String binary)
{
 var list = new List<Byte>();

 for (int i = 0; i < binary.Length; i += 8)
     {
     String t = binary.Substring(i, 8);
     list.Add(Convert.ToByte(t, 2));
     }

 return list.ToArray();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM