0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

<VB.net> Memo: Using Shift-JIS + UTF8 without BOM encoding (.NET core)

Last updated at Posted at 2021-08-22

Environment

  • VB.net
  • .NET core 3.1

Aim

Define a Text.Encoding instance which defines Shift-JIS and UTF-8 without BOM.

References

Code

Dim UTF8 as Boolean

'(...)

Dim enc As System.Text.Encoding

If UTF8 Then
    'UTF-8 encoding without BOM
    enc = New System.Text.UTF8Encoding(False)
Else
    'Shift-JIS encoding
    System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance)
    Dim encFallBack As System.Text.DecoderReplacementFallback = New System.Text.DecoderReplacementFallback("?")
    enc = System.Text.Encoding.GetEncoding("shift_jis", System.Text.EncoderFallback.ReplacementFallback, encFallBack)
End If

Note

  • New System.Text.UTF8Encoding(False) defines UTF-8 without BOM.
  • On .Net Frameworks, the simple code enc = System.Text.Encoding.GetEncoding("shift_jis") may work. However, it is not the case on .Net Core nor .Net, and RegisterProvider is needed.
  • Plus, the code above replaces unencodable characters to "?", owing toencFallBack.
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?