Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.ArgumentOutOfRangeException:&x27;索引和长度必须引用字符串中的位置。在c中# 字符串输入=”; 字符串结果=”; if(Directory.Exists(Path.GetDirectoryName(filePath))) { if(File.Exists(filePath)) { input=File.ReadAllText(filePath.ToString(); List bList=新列表(); 对于(int i=0;i_C#_Substring - Fatal编程技术网

C# System.ArgumentOutOfRangeException:&x27;索引和长度必须引用字符串中的位置。在c中# 字符串输入=”; 字符串结果=”; if(Directory.Exists(Path.GetDirectoryName(filePath))) { if(File.Exists(filePath)) { input=File.ReadAllText(filePath.ToString(); List bList=新列表(); 对于(int i=0;i

C# System.ArgumentOutOfRangeException:&x27;索引和长度必须引用字符串中的位置。在c中# 字符串输入=”; 字符串结果=”; if(Directory.Exists(Path.GetDirectoryName(filePath))) { if(File.Exists(filePath)) { input=File.ReadAllText(filePath.ToString(); List bList=新列表(); 对于(int i=0;i,c#,substring,C#,Substring,在for循环内部的子字符串中发生异常 帮我摆脱这个例外。 提前感谢您正在尝试访问子字符串,而无需验证字符串中是否有足够的字符 假设文件包含6个字符。这意味着input.Length是6。for循环的的第一次迭代将使此方法调用:input.Substring(0,8)。但是,字符串中只有6个字符,因此您会得到ArgumentOutOfRangeException。当字符串结束时,它尝试读取8个字符,但没有剩余的字符。这是否回答了您的问题?仅供参考,第二次i

在for循环内部的子字符串中发生异常 帮我摆脱这个例外。
提前感谢

您正在尝试访问子字符串,而无需验证字符串中是否有足够的字符


假设文件包含6个字符。这意味着
input.Length
是6。for循环的
的第一次迭代将使此方法调用:
input.Substring(0,8)
。但是,字符串中只有6个字符,因此您会得到ArgumentOutOfRangeException。

当字符串结束时,它尝试读取8个字符,但没有剩余的字符。这是否回答了您的问题?仅供参考,第二次
ii+=8
之后完成的所以代码唯一有效的时候是8的倍数。@juharr你当然是对的。哇,我这方面真是个笨蛋。谢谢你,我删除了不正确的部分。
        string input = "";
        string result = "";
        if (Directory.Exists(Path.GetDirectoryName(filePath)))
        {
            if (File.Exists(filePath))
            {
                input = File.ReadAllText(filePath).ToString();
                List<byte> bList = new List<byte>();
                for (int i = 0; i < input.Length; i += 8)
                {
                    bList.Add(Convert.ToByte(input.Substring(i, 8), 2));
                }
                result = Encoding.UTF8.GetString(bList.ToArray());
                return result;

            }