Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

c# - Binary of a number

Is there a simply way to convert decimal/ascii 6 bit decimal numbers from 1 to 100 to binary representation? To be more specific im interested in 6 bit binary ascii. So I made this to get int 32. For example "u" is changed to 61 instead 117 in standard decimal ascii. Then this 61 is needed to be "111101" instead of traditional "01110101" but after this 48 + 8 math it's not important as now it's normal binary, just with 6 bits used.

foreach (char c in partToDecode)
                        {
                            var sum = c - 48;
                            if (sum>40)
                            {
                                sum = sum - 8;
                            }

Found this, but i don't have a clue how to traspose it to c#

void binary(unsigned n) {
   unsigned i;
   // Reverse loop
   for (i = 1 << 31; i > 0; i >>= 1)
       printf("%u", !!(n & i));
}

. . .

binary(65);
question from:https://stackoverflow.com/questions/65603096/binary-of-a-number

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try Convert.ToString, e.g.

  int source = 61;

  // "111101"
  string result = Convert.ToString(source, 2).PadLeft(6, '0');

Fiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...