Wednesday, January 18, 2012

3.1.2. Conversion from string to integer

If we get these kinds of data from a text string, Convert.ToInt32 will not be of use, as not all data are 32 bits integers (4 bytes long). For data type "byte", we would use Convert.ToByte (unsigned, only positive values) or ToSByte (signed, positive or negative values), for 2-byte data we would use ToInt16 (signed) and ToUInt16 (unsigned), and for 8-byte date, we have ToInt64 (signed) and ToUInt64 (unsigned).

Suggested exercises: 

  • Ask the user for his (or her) age, which will be stored in a "byte". Then the program will say that he looks younger (eg, "You do not look like being 20 years old").
  • Ask the user for two two-digit numbers ("byte"), calculate their multiplication, which should be stored in a "ushort" and displayed on screen.
  • Ask the user for two long integers ("long") and show how much is their sum, their subtraction and their product.

No comments:

Post a Comment