From 7916e106a69d3c540f44fb3daee08f7c578bb13b Mon Sep 17 00:00:00 2001 From: Solal Pirelli Date: Thu, 29 May 2025 09:49:53 +0200 Subject: [PATCH 1/5] Add note on BigInteger bin/hex formatting of positive values See dotnet/runtime#115618. It's somewhat unexpected that "print `3` with 2 binary digits" returns a string of length 3 (`"011"`), but also makes sense given the round-trip requirement and the historical context of not using `-` for negative bin/hex numbers. I think documenting this behavior would be useful. --- docs/standard/base-types/standard-numeric-format-strings.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/standard/base-types/standard-numeric-format-strings.md b/docs/standard/base-types/standard-numeric-format-strings.md index 2d88f76072324..d8ab53813c07e 100644 --- a/docs/standard/base-types/standard-numeric-format-strings.md +++ b/docs/standard/base-types/standard-numeric-format-strings.md @@ -98,6 +98,9 @@ The binary ("B") format specifier converts a number to a string of binary digits The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. +For , positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed. +For instance, the number `3` converted with the format specifier `"B2"` is `011` because the binary number `11` represents the negative value `-1`. + The result string is not affected by the formatting information of the current object. @@ -309,6 +312,9 @@ The hexadecimal ("X") format specifier converts a number to a string of hexadeci The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. +For , positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed. +For instance, the number `F` converted with the format specifier `"X1"` is `0F` because the hexadecimal number `F` represents the negative value `-1`. + The result string is not affected by the formatting information of the current object. The following example formats values with the hexadecimal format specifier. From 25054581be782e7e17d0b98a2967670d936194ce Mon Sep 17 00:00:00 2001 From: Solal Pirelli Date: Wed, 18 Jun 2025 08:34:58 +0200 Subject: [PATCH 2/5] Update parsing-numeric.md --- docs/standard/base-types/parsing-numeric.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/standard/base-types/parsing-numeric.md b/docs/standard/base-types/parsing-numeric.md index 4e23aa3484e93..0ac4ccf493a98 100644 --- a/docs/standard/base-types/parsing-numeric.md +++ b/docs/standard/base-types/parsing-numeric.md @@ -61,6 +61,7 @@ All numeric types have two static parsing methods, `Parse` and `TryParse`, that ||The group separator is permitted. The group separator character is determined by the or property.| ||The currency symbol is permitted. The currency symbol is defined by the property.| ||The string to be parsed is interpreted as a hexadecimal number. It can include the hexadecimal digits 0-9, A-F, and a-f. This flag can be used only to parse integer values.| +||The string to be parsed is interpreted as a binary number. It can include the binary digits 0 and 1. This flag can be used only to parse integer values.| In addition, the enumeration provides the following composite styles, which include multiple flags. @@ -72,6 +73,13 @@ All numeric types have two static parsing methods, `Parse` and `TryParse`, that ||Includes all styles except and .| ||Includes all styles except .| ||Includes the , , and styles.| +||Includes the , , and styles.| + +## Parsing binary and hexadecimal BigIntegers + +When parsing with the or flags, the input string is interpreted as a hexadecimal/binary number of exactly the length the string has. +For instance, parsing `"11"` as a binary BigInteger yields `-1`, because that is the interpretation of `11` as a signed two's complement value with exactly 2 digits. +If you want a positive result, add a leading `0`, such as `"011"` which is parsed as `3`. ## Parsing and Unicode Digits From 65a0d631db43a4e23e0d75d6c15de146d78eb8ce Mon Sep 17 00:00:00 2001 From: Solal Pirelli Date: Thu, 19 Jun 2025 09:11:07 +0200 Subject: [PATCH 3/5] Update standard-numeric-format-strings.md --- docs/standard/base-types/standard-numeric-format-strings.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/standard/base-types/standard-numeric-format-strings.md b/docs/standard/base-types/standard-numeric-format-strings.md index d8ab53813c07e..e4669d0e6acce 100644 --- a/docs/standard/base-types/standard-numeric-format-strings.md +++ b/docs/standard/base-types/standard-numeric-format-strings.md @@ -98,8 +98,7 @@ The binary ("B") format specifier converts a number to a string of binary digits The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. -For , positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed. -For instance, the number `3` converted with the format specifier `"B2"` is `011` because the binary number `11` represents the negative value `-1`. +For , positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed. For instance, the number `3` converted with the format specifier `"B2"` is `"011"`. That's because the binary number `"11"` represents the negative value `-1`, as it is interpreted as a number with exactly `2` bits due to the `"B2"` format. The result string is not affected by the formatting information of the current object. From ea4aa583e57772dde41e789bbca71f3acc1cb655 Mon Sep 17 00:00:00 2001 From: "Andy (Steve) De George" <67293991+adegeo@users.noreply.github.com> Date: Fri, 27 Jun 2025 16:26:52 -0700 Subject: [PATCH 4/5] Update parsing-numeric.md --- docs/standard/base-types/parsing-numeric.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/standard/base-types/parsing-numeric.md b/docs/standard/base-types/parsing-numeric.md index 0ac4ccf493a98..122767e4f3e1d 100644 --- a/docs/standard/base-types/parsing-numeric.md +++ b/docs/standard/base-types/parsing-numeric.md @@ -77,9 +77,7 @@ All numeric types have two static parsing methods, `Parse` and `TryParse`, that ## Parsing binary and hexadecimal BigIntegers -When parsing with the or flags, the input string is interpreted as a hexadecimal/binary number of exactly the length the string has. -For instance, parsing `"11"` as a binary BigInteger yields `-1`, because that is the interpretation of `11` as a signed two's complement value with exactly 2 digits. -If you want a positive result, add a leading `0`, such as `"011"` which is parsed as `3`. +When parsing with the or flags, the input string is interpreted as a hexadecimal/binary number of exactly the length the string has. For instance, parsing `"11"` as a binary BigInteger yields `-1`, because that is the interpretation of `11` as a signed two's complement value with exactly 2 digits. If you want a positive result, add a leading `0`, such as `"011"` which is parsed as `3`. ## Parsing and Unicode Digits From 536e1368e04db564b8add79796b084af864d9d15 Mon Sep 17 00:00:00 2001 From: "Andy (Steve) De George" <67293991+adegeo@users.noreply.github.com> Date: Fri, 27 Jun 2025 16:28:00 -0700 Subject: [PATCH 5/5] Update standard-numeric-format-strings.md --- docs/standard/base-types/standard-numeric-format-strings.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/standard/base-types/standard-numeric-format-strings.md b/docs/standard/base-types/standard-numeric-format-strings.md index e4669d0e6acce..b084452518e0b 100644 --- a/docs/standard/base-types/standard-numeric-format-strings.md +++ b/docs/standard/base-types/standard-numeric-format-strings.md @@ -311,8 +311,7 @@ The hexadecimal ("X") format specifier converts a number to a string of hexadeci The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. -For , positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed. -For instance, the number `F` converted with the format specifier `"X1"` is `0F` because the hexadecimal number `F` represents the negative value `-1`. +For , positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed. For example, the number `F` converted with the format specifier `"X1"` is `0F` because the hexadecimal number `F` represents the negative value `-1`. The result string is not affected by the formatting information of the current object.