If you want data conversion in C#.net to follow the below steps.
Step 1
Create an integer variable and assign integer value. Integer size is 4 bytes.
- int val=1235;
Step 2
Create a NULL string variable. The string is 2 bytes.
- string str="";
If I want to convert the integer to string we have to use this inbuilt function Convert.ToString(Parameter) and store this value as a string variable.
- str=Convert.toString(Val);
Above code output will be:
Step 5
- str="1235"
If you want to convert to another format, for example, string to int, int to float, float to string etc.
- //int32 and int16 this are used for 16 bit and 32 bit.
- int temp=Convert.Int32(str);
- //or
- int temp=Convert.Int16(str);
Integer to char
In the below code we see conversion from integer to String.
- int a=123;
- String str=Convert.toString(a);
- output
- "123"
In the below code conversion from string to integer.
- string str="1234";
- int val=Convert.toInt32(str);
- output
- 1234
The below code shows Integer to float data type conversion.
- int a=12;
- float val=Convert.ToSingle(a);
- output
- 12.00
Int to Double
The below code is int to double data type conversion.
- int val=54;
- Double d=Convert.ToDouble(val);
- output
- 54.00
The below code is float to double data type conversion.
- float f=1.5f;
- Double val=Convert.ToDouble(f);
- output
- 1.500
Join the conversation! Your thoughts help the community grow.