将英文数字转换为阿拉伯数字C时出错

时间:2018-04-03 12:38:31

标签: c#

我需要将我的英文数字转换成阿拉伯数字存储在数据库中 当我使用此代码编码

  public static string ConvertNumerals(string input)
  {
        UTF8Encoding utf8Encoder = new UTF8Encoding();
        Decoder utf8Decoder = utf8Encoder.GetDecoder();
        StringBuilder convertedChars;
        convertedChars = new StringBuilder();
        char[] convertedChar = new char[1];
        byte[] bytes = new byte[] { 217, 160 };
        char[] inputCharArray = input.ToCharArray();
        foreach (char c in inputCharArray)
        {
            if (char.IsDigit(c))
            {
                bytes[1] = Convert.ToByte(160 + char.GetNumericValue(c));
                utf8Decoder.GetChars(bytes, 0, 2, convertedChar, 0);
                convertedChars.Append(convertedChar[0]);
            }
            else
            {
                convertedChars.Append(c);
            }
        }
        return convertedChars.ToString();
  }

将字符串转换为int64时返回字符串会出现此错误???

Error Pic

1 个答案:

答案 0 :(得分:0)

试试这个方法 public static string ConvertNumerals(string input) { char[] inputCharArray = input.ToCharArray().Where(ch=>char.IsDigit(ch)).ToArray(); var newCharArr = new string[inputCharArray.Length]; for (int i = 0; i<inputCharArray.Length; i++) { var c = inputCharArray[i];
newCharArr[i] = char.GetNumericValue(c).ToString(); } return string.Join("", newCharArr); }