我如何找到二进制字符串

编程入门 行业动态 更新时间:2024-10-24 18:18:14
本文介绍了我如何找到二进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建一个程序,我可以用一定的数字来编写以触发多个函数。 但是我无法确定何时使用某个数字。举个例子说我有数字10我想用某种算法自动将它改成二进制字符串(00001010)。之后我想知道哪些位是打开和关闭所以我可以调用不同的函数。

I'm trying to make a program where i can write in a certain number to trigger multiple functions. But I'm having trouble identifying when a certain number is used. For an example say i have the number "10" i would like to change it to a binary string(00001010) automatically using an algorithm of some-sort. After that i would like to know which bits are on and off so i can call different functions.

推荐答案

你要找的术语不是字节形式 ,它是二进制或可能是二进制字符串。 转换它非常简单: The term you are looking for is not "byte-form", it's "binary" or possibly "binary string". Converting it is pretty easy: int i = 10; Console.WriteLine(Convert.ToString(i, 2).PadLeft(8, '0'));

但是你不需要将其转换为检查位:

But you don't need to convert it to check bits:

int i = 10; for (int j = 7; j >= 0; j--) { Console.Write(BitValue(i, j)); } Console.WriteLine(); } private int BitValue(int i, int bitNo) { return (i >> bitNo) & 1; }

或者甚至:

Or even:

int i = 10; Console.WriteLine(Convert.ToString(i, 2).PadLeft(8, '0')); for (int j = 7; j >= 0; j--) { Console.Write(BitValue(i, j) ? '*' : '.'); } Console.WriteLine(); } private bool BitValue(int i, int bitNo) { return (i & (1 << bitNo)) != 0; }

更多推荐

我如何找到二进制字符串

本文发布于:2023-11-30 11:35:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649823.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!