简体   繁体   中英

c# How to convert int array to string binary array

I'm writing a program to convert numbers in an array znaky to array in binary numbers called binary , but it returns me error.

System.IndexOutOfRangeException: Index was outside the bounds of the array.

char[] znaky = new char[moje.Length];                                                                                                                                                                 
for (int i = 0; i < znaky.Length; i++)
{                                                                                          
    znaky[i] = moje[i];                                                                         
}

string binary = "";
foreach (int a in znaky)
{
    binary += Convert.ToString(znaky[a], 2);  
}

In array moje was the numbers, but for me I change his positions. In this program I change words to binary code.

Second foreach should be a for :

for (int a = 0; a < znaky.Length; a++)

OR keep the foreach and then change the Convert :

Convert.ToString(a, 2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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