【发布时间】:2020-02-22 08:44:40
【问题描述】:
我正在处理c# desktop application。我有一个字符串,我想toggle 它。
c3 = DecimalToBinary(Convert.ToInt32(tbVal3.Text)).PadLeft(16, '0');
// above c3 is 0000001011110100
将上面的字符串一分为二(子字符串)
string part1 = c3.Substring(0, 8); // 00000010
string part2 = c3.Substring(8, 8); // 11110100
对于part1,第一个八位字节的 MSB 应设置为 1,对于 part2,因此该位应移入第一个八位字节的 LSB,第二个(最后一个)八位字节的 MSB 应设置为 0 ,因此该位应移入第一个八位字节的 LSB。这给出了二进制 part1 = 10000101 和 part2 = 01110100
我已经检查了这个解决方案Binary array after M range toggle operations,但仍然无法理解。
规则
in the case of the application context name LN referencing with no ciphering
the arc labels of the object identifier are (2, 16, 756, 5, 8, 1, 1);
• the first octet of the encoding is the combination of the first two
numbers into a single number, following the rule of
40*First+Second -> 40*2 + 16 = 96 = 0x60;
• the third number of the Object Identifier (756) requires two octets: its
hexadecimal value is 0x02F4, which is 00000010 11110100, but following the above rule,
the MSB of the first octet shall be set to 1 and the MSB of the second (last) octet shall
be set to 0, thus this bit shall be shifted into the LSB of the first octet. This gives
binary 10000101 01110100, which is 0x8574;
• each remaining numbers of the Object Identifier required to be encoded on one octet;
• this results in the encoding 60 85 74 05 08 01 01.
如何使用二进制字符串执行此切换?
任何帮助将不胜感激
【问题讨论】:
-
如果你想做二进制操作,你不应该使用
string。使用byte、int或其他一些适当的数字类型。仅在需要显示输出时才转换为字符串。 -
@Herohtar 实际上我已经用
0填充了字符串,所以如果我使用int,填充将消失,因此00000010将变为10:( -
忘记填充,您需要使用正确的类型。如果您分别使用
part1和part2,则应使用byte,因为那是8 位(示例字符串中的字符数)。最后,在您完成所有二进制操作后,然后您可以使用Convert.ToString(result, 2).PadLeft(8, '0')之类的东西,例如,打印带有正确填充的结果。 -
Herohtar 的意思是首先您使用
byte执行操作,然后将结果转换回字符串和填充。在数字类型中,“填充”仍然存在,只是打印时看不到它 -
实际上并不是很清楚您要在这里做什么-这里的吐水操作和上下八位字节处理是什么?..我认为您的意思是在位字符串中进行一些切换?您实际上只是要按位置(移位 n)对其进行 XOR 操作,对吗?
标签: c# string binary toggle bit