简体   繁体   中英

trying to convert a string to a array without having any spaces in the string C#

ok so i just kind of figured out how to turn a 8-digit binary code into numbers (or so i think) and whats a better way to learn it then by making a program that does it for you so! and well i kind of gotten stuck. im trying to figure out how to convert a string into a string array[] so i can go loop through it and add everything together but i cant seem to find anything like that without needing a space or anything. anyone got any ideas? heres my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace binary_to_number
{
    class Program
    {
        static int GetAddition(int place)
        {
            switch(place) // goes through the switch until if finds witch place the binary is in 
            {
                case 1:
                    return 128;
                case 2:
                    return 64;
                case 3: return 32;
                case 4: return 16;
                case 5: return 8;
                case 6: return 4;
                case 7: return 2;
                case 8: return 1;
                default: return 0;


            }

        }

        static int ToInt(string input)
        {
            string[] binary = input.Split(); // right here is where im stuck 
            int thenumber = 0; // this is the number it adds to
            for(int i = 0;i < 9;i++)
            {
                Console.WriteLine(binary[i]);
            }
            return thenumber;
        }


        static void Main(string[] args)
        {

            while (true)
            {
                Console.WriteLine("Please put in a 8-digit binary");
                string input = Console.ReadLine();
                if (input.Length < 9) // binary has 8 digits plus the null at the end of each string so if its 
                { // not binary

                    Console.WriteLine(ToInt(input)); // function converts the input into binary
                }

            }



        }


    }
}

Hope this will help you. String implements IEnumerable interface which will give you an Enumerator to iterate with.

        Console.WriteLine("Please enter 8 digit binary number");
        string input = Console.ReadLine();
        foreach (var item in input)
        {
            Console.WriteLine("Item is {0}", item);
        }

To start fixing your program:

string[] binary = input.Split(); // right here is where im stuck

should be

char[] binary = input.ToCharArray();

Also for (int i = 0; i < 9; i++) should be for (int i = 0; i < 8; i++) or better for (int i = 0; i < binary.Length; i++)


A better way?

You can save yourself plenty of code by using the Convert class.

while (true)
{
    Console.WriteLine("Please put a value as binary");
    string input = Console.ReadLine();
    var number = Convert.ToUInt16(input, 2);

    Console.WriteLine($"input:{input}, value: {number}, as binary: {Convert.ToString(number, 2)}");
}
/*
Please put a value as binary
1
input:1, value: 1, as binary: 1

Please put a value as binary
11
input:11, value: 3, as binary: 11

Please put a value as binary
10000001
input:10000001, value: 129, as binary: 10000001
*/

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