Как вывести char массив в строке через функцию WriteLine?
Пытаюсь разбить строку на четыре 16-ричных числа и вывести их в на консоль.
И столкнулся с проблемой как вывести из консоли ( функцией Console.WriteLine) char[] массив.
вот программа :
using System;
using System.Text;
// логарифм:
// 1. считываем строку +
// 2. убираем все пробелы и запятые +
// 3. разбиваем на строки с количеством символов в 4 числа
namespace trimtram
{
class Program
{
static void Main()
{
Console.WriteLine("Тестовая прога.");
string stroka = Console.ReadLine(); // 1
stroka = stroka.Replace(" ", ""); // 2
stroka = stroka.Replace(",", ""); // 2
stroka = stroka.Replace(".", ""); // 2
//int t_per = 0; int t_per1 =0;
int t_per_str = stroka.Length/4;
Console.WriteLine("stroka.Length ={0} ,t_per_str = {1}.", stroka.Length, t_per_str);
Console.WriteLine(stroka);
char[] stroka_t = new char[t_per_str];
int t_per = 0;
for (int i =0; i<stroka.Length; i++)
{
stroka_t[t_per] = stroka[i];
t_per = t_per+1;
if (t_per == 4)
{ Console.WriteLine("stroka_t ={0}",stroka_t.ToString()); t_per=0; }
//{ Console.WriteLine("stroka_t =" + stroka_t.ToString()); t_per = 0; }
}
}
}
}
Вот вывод :
comp@comp0:~/Qt_project/TCP_IP$ mcs tehn7.cs
comp@comp0:~/Qt_project/TCP_IP$ mono tehn7.exe
Тестовая прога.
0xa1 0x01 0x02 0x00
stroka.Length =16 ,t_per_str = 4.
0xa10x010x020x00
stroka_t =System.Char[]
stroka_t =System.Char[]
stroka_t =System.Char[]
stroka_t =System.Char[]
Также пробовал так :
...
{ Console.WriteLine("stroka_t =" + stroka_t.ToString()); t_per = 0; }
...
Результат такой :
comp@comp0:~/Qt_project/TCP_IP$ mcs tehn7.cs
comp@comp0:~/Qt_project/TCP_IP$ mono tehn7.exe
Тестовая прога.
0xa1 0x01 0x02 0x00
stroka.Length =16 ,t_per_str = 4.
0xa10x010x020x00
stroka_t =System.Char[]
stroka_t =System.Char[]
stroka_t =System.Char[]
stroka_t =System.Char[]
Ответы (1 шт):
Моя ошибка была в том что я комбинировал char[] со строковым литералом.
Console.WriteLine("stroka_t="+stroka_t.ToString());
Метод .ToString() в char[] возвращает его тип, а не значение.
Надо было изменить строку так :
{ Console.WriteLine("stroka_t ={0}", new string(stroka_t)); t_per = 0; }
либо
Console.WriteLine(stroka_t);
В этом случае Console.WriteLine(stroka_t); интерфейс Console делает перегрузку и оборачивает массив char[] в строку.
Вот исправленная программа :
using System;
using System.Text;
// логарифм:
// 1. считываем строку +
// 2. убираем все пробелы и запятые +
// 3. разбиваем на строки с количеством символов в 4 числа +
namespace trimtram
{
class Program
{
static void Main()
{
Console.WriteLine("Тестовая прога.");
string stroka = Console.ReadLine(); // 1
stroka = stroka.Replace(" ", ""); // 2
stroka = stroka.Replace(",", ""); // 2
stroka = stroka.Replace(".", ""); // 2
// 3 ---
int t_per_str = stroka.Length/4;
Console.WriteLine("stroka.Length ={0} ,t_per_str = {1}.", stroka.Length, t_per_str);
Console.WriteLine(stroka);
char[] stroka_t = new char[t_per_str];
int t_per = 0;
for (int i =0; i<stroka.Length; i++)
{
stroka_t[t_per] = stroka[i];
t_per = t_per+1;
if (t_per == 4)
{ Console.WriteLine("stroka_t ={0}", new string(stroka_t)); t_per = 0; }
}
// ---
}
}
}
Вот вывод :
comp@comp0:~/Qt_project/TCP_IP$ mcs tehn7.cs
comp@comp0:~/Qt_project/TCP_IP$ mono tehn7.exe
Тестовая прога.
0xa1 0x01 0x02 0x00
stroka.Length =16 ,t_per_str = 4.
0xa10x010x020x00
stroka_t =0xa1
stroka_t =0x01
stroka_t =0x02
stroka_t =0x00
Также замечу что очень интересно себя ведет WriteLine :3