Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, January 4, 2012 10:16 AM
Suppose:
int a = 59;
string strA = a.ToString("X");
This will return the hexadecimal number equivalent to 50. It will return "3B". And I use this way to convert an int to a hexadecimal number. Here "X" is a format string. So there may be format strings that can be used to convert 59 into a binary number, or octal etc. What are they? If there are no such format strings for converting to binary or octal, then what are the other format strings and what does they do?
All replies (5)
Wednesday, January 4, 2012 10:38 AM ✅Answered | 1 vote
Hi ddlyprogrammer,
Format specifier |
Name |
Description |
Examples |
|---|---|---|---|
"C" or "c" |
Currency |
Result: A currency value. Supported by: All numeric types. Precision specifier: Number of decimal digits. Default precision specifier: Defined by System.Globalization.NumberFormatInfo. More information: The Currency ("C") Format Specifier. |
123.456 ("C", en-US) -> $123.46 123.456 ("C", fr-FR) -> 123,46 € 123.456 ("C", ja-JP) -> ¥123 -123.456 ("C3", en-US) -> ($123.456) -123.456 ("C3", fr-FR) -> -123,456 € -123.456 ("C3", ja-JP) -> -¥123.456 |
"D" or "d" |
Decimal |
Result: Integer digits with optional negative sign. Supported by: Integral types only. Precision specifier: Minimum number of digits. Default precision specifier: Minimum number of digits required. More information: The Decimal("D") Format Specifier. |
1234 ("D") -> 1234 -1234 ("D6") -> -001234 |
"E" or "e" |
Exponential (scientific) |
Result: Exponential notation. Supported by: All numeric types. Precision specifier: Number of decimal digits. Default precision specifier: 6. More information: The Exponential ("E") Format Specifier. |
1052.0329112756 ("E", en-US) -> 1.052033E+003 1052.0329112756 ("e", fr-FR) -> 1,052033e+003 -1052.0329112756 ("e2", en-US) -> -1.05e+003 -1052.0329112756 ("E2", fr_FR) -> -1,05E+003 |
"F" or "f" |
Fixed-point |
Result: Integral and decimal digits with optional negative sign. Supported by: All numeric types. Precision specifier: Number of decimal digits. Default precision specifier: Defined by System.Globalization.NumberFormatInfo. More information: The Fixed-Point ("F") Format Specifier. |
1234.567 ("F", en-US) -> 1234.57 1234.567 ("F", de-DE) -> 1234,57 1234 ("F1", en-US) -> 1234.0 1234 ("F1", de-DE) -> 1234,0 -1234.56 ("F4", en-US) -> -1234.5600 -1234.56 ("F4", de-DE) -> -1234,5600 |
"G" or "g" |
General |
Result: The most compact of either fixed-point or scientific notation. Supported by: All numeric types. Precision specifier: Number of significant digits. Default precision specifier: Depends on numeric type. More information: The General ("G") Format Specifier. |
-123.456 ("G", en-US) -> -123.456 123.456 ("G", sv-SE) -> -123,456 123.4546 ("G4", en-US) -> 123.5 123.4546 ("G4", sv-SE) -> 123,5 -1.234567890e-25 ("G", en-US) -> -1.23456789E-25 -1.234567890e-25 ("G", sv-SE) -> -1,23456789E-25 |
"N" or "n" |
Number |
Result: Integral and decimal digits, group separators, and a decimal separator with optional negative sign. Supported by: All numeric types. Precision specifier: Desired number of decimal places. Default precision specifier: Defined by System.Globalization.NumberFormatInfo. More information: The Numeric ("N") Format Specifier. |
1234.567 ("N", en-US) -> 1,234.57 1234.567 ("N", ru-RU) -> 1 234,57 1234 ("N", en-US) -> 1,234.0 1234 ("N", ru-RU) -> 1 234,0 -1234.56 ("N", en-US) -> -1,234.560 -1234.56 ("N", ru-RU) -> -1 234,560 |
"P" or "p" |
Percent |
Result: Number multiplied by 100 and displayed with a percent symbol. Supported by: All numeric types. Precision specifier: Desired number of decimal places. Default precision specifier: Defined by System.Globalization.NumberFormatInfo. More information: The Percent ("P") Format Specifier. |
1 ("P", en-US) -> 100.00 % 1 ("P", fr-FR) -> 100,00 % -0.39678 ("P1", en-US) -> -39.7 % -0.39678 ("P1", fr-FR) -> -39,7 % |
"R" or "r" |
Round-trip |
Result: A string that can round-trip to an identical number. Supported by: Single, Double, and BigInteger. Precision specifier: Ignored. More information: The Round-trip ("R") Format Specifier. |
123456789.12345678 ("R") -> 123456789.12345678 -1234567890.12345678 ("R") -> -1234567890.1234567 |
"X" or "x" |
Hexadecimal |
Result: A hexadecimal string. Supported by: Integral types only. Precision specifier: Number of digits in the result string. More information: The HexaDecimal ("X") Format Specifier. |
255 ("X") -> FF -1 ("x") -> ff 255 ("x4") -> 00ff -1 ("X4") -> 00FF |
Any other single character |
Unknown specifier |
Result: Throws a FormatException at run time. |
Here are the reference links
Int32.ToString Method (String)
http://msdn.microsoft.com/en-us/library/8wch442y.aspx
Standard Numeric Format Strings
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
If a post answers your question, please click Mark As Answer on that post and Mark as Helpful. Happy Coding...
Wednesday, January 4, 2012 10:21 AM | 1 vote
Refer the Following URL
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
and you can use the following
string binValue = Convert.ToString(6, 2);
int intValue = Convert.ToInt32("110", 2);
Console.WriteLine("bin: " + binValue.ToString());
Console.WriteLine("int: " + intValue.ToString());
string OctValue = Convert.ToString(36, 8);
intValue = Convert.ToInt32("44", 8);
Console.WriteLine("Oct: " + OctValue.ToString());
Console.WriteLine("int: " + intValue.ToString());
Console.Read();
Surender Singh Bhadauria
Wednesday, January 4, 2012 10:21 AM | 1 vote
Hi,
check out:
Standard Numeric FormatStrings: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
Custom Numeric Format Strings: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
Hannes
If you have got questions about this, just ask.
In a perfect world,
users would never enter data in the wrong form,
files they choose to open would always exist
and code would never have bugs.
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
Wednesday, January 4, 2012 10:24 AM | 1 vote
Hi
this link has all formats
http://msdn.microsoft.com/en-us/library/26etazsy.aspx#FormatStrings
By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
Wednesday, January 4, 2012 10:34 AM | 1 vote
Converting Binary To Int
string binary = "10011";
int integer = Convert.ToInt32(binary, 2);
Console.WriteLine(Convert.ToString(integer, 8));
Output: 23
C# Convert Hexadecimal to Binary String Conversion
string binaryval = "";
binaryval = Convert.ToString(Convert.ToInt32(hexvalue, 16), 2);
return binaryval;
Don't forget to mark the post as answer or vote as helpful if it does, Regards - Rajasekhar.R