Sayan Sen
Sayan Sen

Reputation: 1824

How to make a 4 bit binary to 8bit binary number?

I am using C# I have a variable byte a=0b00001111. When i print it on console it shows as truncated value: 1111 I want to print it right as it is: 00001111

I want a GENERALIZED method or code snippet which does the exact same thing which I want. I tried ANDing it with 11111111 but still the same answer.

Please help.

Upvotes: 2

Views: 2831

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063704

Presumably just left-pad?

    byte a = 0b00001111;
    var s = Convert.ToString(a, 2).PadLeft(8, '0');
    Console.WriteLine(s);

Upvotes: 7

Related Questions