-
July 12th, 2009, 07:51 AM
#1
Convert.ToString: sbyte oddity
Code:
sbyte x = -4;
Console.WriteLine("Suppose we have an sbyte with value -4.");
Console.WriteLine("The size of sbyte is {0} byte.", sizeof(sbyte));
Console.WriteLine("The sbyte converted to binary string is: {0}", Convert.ToString(x, 2));
Code:
Output:
Suppose we have an sbyte with value -4.
The size of sbyte is 1 byte.
The sbyte converted to binary string is: 1111111111111100
If sbyte is 8 bits, why does Convert.ToString yield 16-bit output?
Thanks.
-
July 12th, 2009, 07:58 AM
#2
Re: Convert.ToString: sbyte oddity
According to MSDN:
Convert.ToString Method (SByte)
Converts the value of the specified 8-bit signed integer to its equivalent String representation.
So, why does it give 16-bit output?
-
July 12th, 2009, 08:06 AM
#3
Re: Convert.ToString: sbyte oddity
There's no method Convert.ToString (sbyte, int). The closest one is Convert.ToString (Int16, int) as an Int16 is the smallest datatype which can fit an sbyte. So what happens is that the (Int16, int) overload is chosen when you pass an (sbyte, int). That's why you see 16 bits of output. If you want just the bits, cast the sbyte to byte first, so you choose the (byte, int) overload:
Code:
sbyte val = 5;
Convert.ToString ((byte) val, 2);
That should give the correct answer.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
July 12th, 2009, 05:07 PM
#4
Re: Convert.ToString: sbyte oddity
Originally Posted by Mutant_Fruit
There's no method Convert.ToString (sbyte, int). The closest one is Convert.ToString (Int16, int) as an Int16 is the smallest datatype which can fit an sbyte. So what happens is that the (Int16, int) overload is chosen when you pass an (sbyte, int). That's why you see 16 bits of output.
Thanks very much for your reply.
However, according to MSDN, there is a method for Convert.ToString(SByte, IFormatProvider), if I am reading this correctly.
http://msdn.microsoft.com/en-us/libr...15(VS.80).aspx
It indicates: "Converts the value of the specified 8-bit signed integer to its equivalent String representation."
So, that is why I'm surprised that it's still returning 16-bit output.
(Perhaps it has to do with IFormatProvider, which I am not entirely clear about.)
Last edited by Fortress7; July 12th, 2009 at 05:09 PM.
-
July 12th, 2009, 05:18 PM
#5
Re: Convert.ToString: sbyte oddity
Because .NEt strings are not 8 bits wide. Just because the input is a byte doesn't mean it will return a string with in an 8-bit wide format.
-
July 12th, 2009, 07:43 PM
#6
Re: Convert.ToString: sbyte oddity
Originally Posted by BigEd781
Because .NEt strings are not 8 bits wide. Just because the input is a byte doesn't mean it will return a string with in an 8-bit wide format.
OK, fair enough.
It just seems odd, because:
If I use int, it returns a 32-bit value.
If I use short, it returns a 16-bit value.
And if I use sbyte, it returns a 16-bit value, too.
So, for whatever reason, it appears that Convert.ToString returns a minimum 16-bit string, as you suggested.
Thanks.
-
July 13th, 2009, 05:46 AM
#7
Re: Convert.ToString: sbyte oddity
Originally Posted by BigEd781
Because .NEt strings are not 8 bits wide. Just because the input is a byte doesn't mean it will return a string with in an 8-bit wide format.
That's not it at all
So, for whatever reason, it appears that Convert.ToString returns a minimum 16-bit string, as you suggested.
Reread my answer.
There's no method Convert.ToString (sbyte, int). The closest one is Convert.ToString (Int16, int) as an Int16 is the smallest datatype which can fit an sbyte. So what happens is that the (Int16, int) overload is chosen when you pass an (sbyte, int).
Sure, there's a Convert.ToString (sbyte, IFormatProvider), but an int (the second parameter) is not an IFormatProvider so you're not calling that method.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
July 13th, 2009, 12:17 PM
#8
Re: Convert.ToString: sbyte oddity
Originally Posted by Mutant_Fruit
That's not it at all
My apologies to the OP, I completely and utterly mistook the entire reason behind posting this thread. That;s what I get for skimming through posts . My mistake, will make sure to read more carefully in the future.
-
July 13th, 2009, 06:13 PM
#9
Re: Convert.ToString: sbyte oddity
Originally Posted by Mutant_Fruit
Sure, there's a Convert.ToString (sbyte, IFormatProvider), but an int (the second parameter) is not an IFormatProvider so you're not calling that method.
Aha... Now I understand. Thank you very much.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|