Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 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);

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...