Skip to content

Converting string to byte array: retarget net10.0, add the reverse direction - #2178

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/103955-csharp-converting-string-to-byte-array
Open

Converting string to byte array: retarget net10.0, add the reverse direction#2178
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/103955-csharp-converting-string-to-byte-array

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Retargets collections-arrays/ConvertingStringToByteArray to net10.0 and adds the byte-array-to-string direction the sample was missing.

Retarget and packages

  • All four projects net8.0 -> net10.0.
  • Microsoft.NET.Test.Sdk 17.6.0 -> 18.9.0, xunit 2.4.2 -> 2.9.3, xunit.runner.visualstudio 2.4.5 -> 4.0.0, coverlet.collector 6.0.0 -> 10.0.1, BenchmarkDotNet 0.13.11 -> 0.15.8.

New method

  • MessageConversion.ConvertUTF8BytesToString(byte[]), the other half of the pair.

New tests (10 passing, previously 4)

  • Matching UTF-8 round trip returns the original string.
  • ASCII round trip of "cafe [emoji]" returns "caf? ??", silently.
  • An invalid byte decodes to U+FFFD rather than throwing.
  • (byte) of a char above U+00FF truncates to 0; Convert.ToByte on the same char throws OverflowException. These two back the comparison table in the article, so no table row rests on a scratch console app.
  • Encoding.RegisterProvider(CodePagesEncodingProvider.Instance) makes GetEncoding(1252) resolve, and before it the two overloads fail differently: GetEncoding(1252) throws NotSupportedException while GetEncoding("windows-1252") throws ArgumentException. No System.Text.Encoding.CodePages PackageReference is needed on net10.0, the provider ships in the shared framework (adding it raises NU1510).

Build is clean with zero warnings; all 10 tests pass on .NET 10.0.10 / SDK 10.0.302. Both benchmark classes were re-run on net10.0 and the article's printed tables are being replaced with that output.

…rection

- All four projects retargeted net8.0 -> net10.0.
- Packages: Microsoft.NET.Test.Sdk 18.9.0, xunit 2.9.3,
  xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1,
  BenchmarkDotNet 0.13.11 -> 0.15.8.
- New MessageConversion.ConvertUTF8BytesToString, the byte[] -> string half of
  the pair, with tests for the matching UTF-8 round trip, the lossy ASCII round
  trip ("cafe ??"), and the U+FFFD replacement for an invalid byte.
- Tests backing the article's method-comparison table, so no row rests on a
  scratch console app: (byte)'A-macron' truncates silently to 0 while
  Convert.ToByte on the same char throws OverflowException.
- Test proving Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
  makes GetEncoding(1252) resolve, and that the two overloads fail differently
  before it: GetEncoding(1252) throws NotSupportedException while
  GetEncoding("windows-1252") throws ArgumentException. No PackageReference is
  needed for this on net10.0; the provider ships in the shared framework.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant