Convert single byte character string (half width) to double byte (full width)

冷暖自知 提交于 2019-12-12 18:18:38

问题


Recently I came across this code in a C# application.

cDataString = Strings.StrConv(cDataString, VbStrConv.Wide);

I understand that the StrConv is a string function of VB. You can call it by including 'using Microsoft.VisualBasic;'.

It is supposed to covert half width japanese characters into full width ones.

My question is: Is there a way to achieve the same WITHOUT using the VB functions and WITHOUT including the VB headers, using only the standard c# functions? I know there are many c# string conversion functions and some of them can convert from unicode to ansi and vice versa and so on. But I am not sure if any of those will directly get the exact same result as the above VB one. So, can this be done in c#?

Thank you for your time and efforts.

Update: I came across this question that was asked 5 years ago. The answers and discussions do show some ways in which it could be done. What I would specifically like to know is that, after 5 years and new versions and what nots, is there a simpler and better way to do this in .NET without depending on VB functions or VB libraries?


回答1:


There is no equivalent function in C#.

If you follow the source code for Microsoft.VisualBasic.dll's StrConv, you'll see it actually p/invokes LCMapString internally similar to the answer you linked.

If you don't want to reference Microsoft.VisualBasic.dll, you could wrap the p/invoke into a helper class or service written in C#, something like this...

// NOTE: CODE NOT TESTED
// Code from John Estropia's StackOverflow answer
// https://stackoverflow.com/questions/6434377/converting-zenkaku-characters-to-hankaku-and-vice-versa-in-c-sharp

public static class StringWidthHelper
{
    private const uint LOCALE_SYSTEM_DEFAULT = 0x0800;
    private const uint LCMAP_HALFWIDTH = 0x00400000;
    private const uint LCMAP_FULLWIDTH = 0x00800000;

    public static string ToHalfWidth(string fullWidth)
    {
        StringBuilder sb = new StringBuilder(256);
        LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_HALFWIDTH, fullWidth, -1, sb, sb.Capacity);
        return sb.ToString();
    }

    public static string ToFullWidth(string halfWidth)
    {
        StringBuilder sb = new StringBuilder(256);
        LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_FULLWIDTH, halfWidth, -1, sb, sb.Capacity);
        return sb.ToString();
    }

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
    private static extern int LCMapString(uint Locale, uint dwMapFlags, string lpSrcStr, int cchSrc, StringBuilder lpDestStr, int cchDest);
}

Otherwise, you could build a Dictionary to act as a look-up table.



来源:https://stackoverflow.com/questions/40835855/convert-single-byte-character-string-half-width-to-double-byte-full-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!