[英]how to split 16 bit binary string into two 8 bit binary string in c# console
I have 1 fix binary number ie:0110. 我有1个固定的二进制数字,即:0110。 I have two user input numbers which will be converted to binary with 6 bit. 我有两个用户输入数字,这些数字将转换为6位二进制。 So now I have 16 bit binary frame. 所以现在我有16位二进制帧。 Now I want to split 16 bit frame into 2 8-bit binary frame and i dont know how to do that. 现在,我想将16位帧拆分为2个8位二进制帧,我不知道该怎么做。 Please help me. 请帮我。 HERE is my code: 这是我的代码:
int num1, num2;
Console.WriteLine("Enter 1st number:");
num1 = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter 2nd number:");
num2 = Int32.Parse(Console.ReadLine());
string bin1 = Convert.ToString(num1, 2).PadLeft(6, '0');
Console.WriteLine(bin1);
string bin2 = Convert.ToString(num2, 2).PadLeft(6, '0');
Console.WriteLine(bin2);
string bin3 = "0110";
string res = string.Concat(bin3, bin1, bin2);
Console.WriteLine(res);
string result = res;
Console.WriteLine(result);
Console.ReadKey();
OutPut of this code: 此代码的输出:
Last line in the image is the 16 bit binary frame and i want to split that frame into two 图像的最后一行是16位二进制帧,我想将该帧分成两部分
This is just a question of implementing your own logic. 这只是实现自己的逻辑的问题。
You can try this, it divides a string based on the count you give it, You'll supply it with 8, since you want 8-bit frames. 您可以尝试一下,它会根据您给它的计数来划分一个字符串,因为您想要8位帧,所以将它提供8。
public static string[] SplitBy(string str, int count)
{
var some = new string[str.Length/count];
int i = 0;
while (true)
{
try
{
string temp = str.Substring(i * 8, count);
if (temp != null)
{
some[i] = temp;
i++;
}
}
catch (Exception)
{
break;
}
}
return some;
}
I left the exception handling for you. 我将异常处理留给您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.