CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2009
    Posts
    8

    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.

  2. #2
    Join Date
    Jul 2009
    Posts
    8

    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?

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    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.

  4. #4
    Join Date
    Jul 2009
    Posts
    8

    Re: Convert.ToString: sbyte oddity

    Quote Originally Posted by Mutant_Fruit View Post
    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.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  6. #6
    Join Date
    Jul 2009
    Posts
    8

    Re: Convert.ToString: sbyte oddity

    Quote Originally Posted by BigEd781 View Post
    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.

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    Re: Convert.ToString: sbyte oddity

    Quote Originally Posted by BigEd781 View Post
    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.

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Convert.ToString: sbyte oddity

    Quote Originally Posted by Mutant_Fruit View Post
    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.

  9. #9
    Join Date
    Jul 2009
    Posts
    8

    Re: Convert.ToString: sbyte oddity

    Quote Originally Posted by Mutant_Fruit View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured