Ringil
Ringil

Reputation: 6537

.NET Standard F# library does not store non-English characters properly

I create a .NET Standard F# library with F# 4.3.4 (I also tested with 4.5) with the following code:

namespace ClassLibrary2

module Say =
    let a = "国".Length.ToString()
    let b = sprintf "%A" ("国".ToCharArray() |> Array.map int)
    let c = "国"

When referencing that library from another project (.net core or .net framework):

Console.WriteLine(Say.a); // F# .net standard
Console.WriteLine(Say.b);
Console.WriteLine(Say.c == "国");

I get the following output:

2
[|65533; 65533|]
False

The equivalent C# .NET Standard library:

using System;
using System.Linq;

namespace ClassLibrary1
{
    public static class Class1
    {
        public static string a = "国".Length.ToString();
        public static string b = String.Join(", ", "国".ToCharArray().Select(i => ((int)i).ToString()));
        public static string c = "国";
    }
}

gives the expected output:

1 
22269
True

Here's a repo showing the issue: https://github.com/liboz/Kanji-Bug.

This looks likely to be a bug, but I was wondering what would be a reasonable workaround for this problem? Specifically, I want to be able to be able to check equality for strings with something like Say.c = "国" where I might be using non-English characters while using a .NET Standard library.

Upvotes: 4

Views: 133

Answers (1)

Ringil
Ringil

Reputation: 6537

So, the issue appears to be that the first file that the dotnet cli generates in an F# library does not use Unicode for its encoding. So, when creating a .NET Standard F# library that file for me was generated with Shift-JIS encoding, likely due to region settings on my own computer. Therefore, the solution to my issue was to simply save the default Library1.fs file with UTF-8 encoding manually so that it would have the same encoding as all the other files.

Upvotes: 1

Related Questions