Answer the question
In order to leave comments, you need to log in
How to split a number into equal parts?
Here is my first experience with C#.
I was given the following task: It is
necessary to write a converter of numbers in the binary system to hexadecimal and octal.
The user enters the numbers that should be stored in the array, and then clicking on the index number to get the entered number in the other (specified above) SS. the use of standard C# functions for this task is prohibited. Those. all arithmetic operations with numbers should be performed by my program.
The problem is that I don’t know what function to split a binary number into groups of 3, and at the same time, the split should occur from right to left and the high bit should achieve zeros up to 3 numbers if there are not enough of them.
I was supposed to copy the last 3 characters from the number -> assign them to a variable -> convert -> delete already unnecessary used numbers from the original number 3 -> copy the last 3 and further in a circle, but the fact is that the last 3 are not copied, and I can't figure out how to fill the high bit with zeros if there aren't enough numbers in it.
I understand that this is clumsy, but should it work?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using System.Text.RegularExpressions;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
// Создаем массив
string[] nums = new string[12];
// Создаем временный массив для конвертации чисел
string[] temp = new string[3];
// Количество символов в группе для перевода в восьмеричную систему
int oct = 3;
// Получаем двоичные числа для конвертации
Console.WriteLine("Введите 12 чисел");
for (int i = 0; i < nums.Length; i++)
{
Console.Write("{0} - е число: ", i + 0);
nums[i] = Console.ReadLine();
}
// Получаем индекс конвертируемого числа
Console.WriteLine("Выберите число по порядковому номеру, для конвертации.");
// Передаем переменной значение, хранящееся в массиве
int s;
s = Int32.Parse(Console.ReadLine());
// Тут должна быть работа с переменной.
int binary;
// Переменная binary получает первые 3 числа с ней проводятся арифметические операции, и т.д.
// В итоге должно получится число, которое будет результатом перевода из двоичной системы в восьмеричную.
string b = s.Length;
Console.WriteLine(nums[s]);
Console.ReadLine();
}
}
}
Answer the question
In order to leave comments, you need to log in
What is the meaning of "split a number into equal parts"?
Like this: 100,000,000?
Use mask N0:
Or, if the number is always the same length, then you can make your own mask:
ps in the case of binary numbers - you can reverse the array and get it "from right to left"
Thank you. The length of the number can be different each time, just to translate from one number system to another, you need to work with groups of numbers, for example, 3. And I need to process each group separately. those. I then need to pass it to a variable, and so on until I go through the entire number
And what's the point of translating the entered strings into numbers?
Just store everything and treat it as strings.
The original array, as I understand it, is in the binary system? { "10001", "1202", etc. }?
Just bite off each number 3 (for octal) or 4 (for hexadecimal) characters from the end (on the right); pad up to 3 or 4 characters with zeros on the left (to correctly process the highest part); and with the usual switch select the appropriate number in octal or hexadecimal system:
"101" -> "5" (oct)
"1101" -> "D" (hex)
Of course, you can get confused, and make the translation a standard method of dividing and checking the remainder in a cycle - in this case, check 1 bit at a time and accumulate them. But this method is good for universal converters, and for a highly specialized one, as in this case, a dictionary translation is quite suitable.
It's a strange requirement not to use standard C# methods - I understand that this is most likely a learning task, but the whole essence of the C# language is in a very developed, large library out of the box.
Console.ReadLine and Int32.Parse are also standard functions... So it won't work without standard ones at all, that's 100%.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question