CodeSOD: A Bit of a Misunderstanding

Mark B sends us another sample from a big-ball-o-wtf.

Let's take a look at two variables. One variable is named taskDetail. The other variable is named binaryTaskDetail. You might ask yourself: what are the differences between these two?

Well, one of them is a C# int- 32-bit integer. The other is a C# long- a 64-bit integer. And, as the name implies, binaryTaskDetail is meant to be a binary representation of the number.

"Wait," you might ask, "aren't all integers just binary representations under the hood? Can't you just bitshift or mask or whatever you need to do? Do you mean they want to output binary data? Like as a print statement? Now I'm confused."

I'm sorry I confused you. Let's take a look at the code, and everything will be clear.

binaryTaskDetail = Int64.Parse(Convert.ToString(taskDetail, 2));

Let's say that taskDetail contained the number 434. We use Convert.ToString to convert it into a string, but note the 2 in the parameters- we're doing that in base two, so that gives us a binary representation, as a string: 110110010. Then we parse the string back into an integer. Which, you'll note, the string doesn't advertize what base it's in, so it turns into the integer 110110010.

So, we convert a number into a binary representation as a string, then parse that string as a decimal. It goes without saying that 0b110110010 and 110110010 are not the same number. In this example, binaryTaskDetail now contains the binary value 110100100000010010100111010.

Why is any of this happening? I have no idea. The most Mark can offer is: "This time the developer knew that you are supposed to do things in binary for… reasons, but not much else."

[Advertisement] ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published. Required fields are marked *