none
Trying to understand differences between Convert.ToByte() Vs Byte.Parse() RRS feed

  • Question

  • HI

    I have a requirement to convert a string to byte. I am looking at Convert.toByte and Byte.Parse methods.

    Can you please help me to understand differences between the two methods and which one to use.

    Thanks & Regards,

    Rajesh Kaki


    Rajesh Kaki

    Tuesday, July 2, 2013 5:24 PM

Answers

  • Tuesday, July 2, 2013 5:29 PM
  • I would suggest using Byte.Parse if you know the format of the string will always be a value that can be parsed using one of the overloads.  Use Convert.ToByte if the format of the string requires it (for instance, prefixed with a 0x as per the link provided).  If you are unsure of the format of the string, or it may not always be consistent (e.g. the string is entered by a user who might make a mistake) then use Byte.TryParse to ensure a valid value before continuing the processing.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, July 3, 2013 1:29 AM
    Moderator

All replies

  • Tuesday, July 2, 2013 5:29 PM
  • I would suggest using Byte.Parse if you know the format of the string will always be a value that can be parsed using one of the overloads.  Use Convert.ToByte if the format of the string requires it (for instance, prefixed with a 0x as per the link provided).  If you are unsure of the format of the string, or it may not always be consistent (e.g. the string is entered by a user who might make a mistake) then use Byte.TryParse to ensure a valid value before continuing the processing.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, July 3, 2013 1:29 AM
    Moderator