转换 c++ printf 格式与 VB .NET 字符串格式之间的转换

发布于 2024-09-25 15:01:53 字数 516 浏览 1 评论 0原文

我有一些 VB .NET 软件,可以连接到大量旧的(但健全的)COM 对象。 VB 为 COM 对象提供了一个 GUI,其中一部分包括在 COM 对象上设置各种选项 - 其中一些选项与字符串格式相关。

我有一对简单的 VB .NET 函数,它们使用涵盖特定常见字符串的大型选择案例将基本 %f、%d、%g 格式转换为 .NET 等效格式,但它们并不涵盖所有格式。这就是我所拥有的……

        ElseIf f = "%.3f" Then
            return "0.000"
        ElseIf f = "%.2f" Then
            return "0.00"
        ElseIf f = "%.1f" Then
            return "0.0"

在我开始深入研究并通过一些解析使其更加通用之前,有人知道有一个类(例如 VB 或 C# .NET)可以提供不错的现成实现吗?或者也许可以使用一些正则表达式向导?

非常感谢

蒂姆

I have some VB .NET software that interfaces to a load of old (but sound) COM objects. The VB provides a GUI for the COM objects, part of which consists of setting various options on the COM objects - several of which relate to string formatting.

I have a simple pair of VB .NET functions that convert basic %f, %d, %g formats to/from .NET equivalents using a large select case covering specific common strings, but they don't cover all formats. This is the kind of thing I have...

        ElseIf f = "%.3f" Then
            return "0.000"
        ElseIf f = "%.2f" Then
            return "0.00"
        ElseIf f = "%.1f" Then
            return "0.0"

Before I start diving in and making it more versatile with some parsing, does anyone know of a class (eg VB or C# .NET) that provides a decent ready-made implementation? Or perhaps some regexp wizadry could be used?

Many thanks

Tim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白龙吟 2024-10-02 15:01:53

您是否真的需要这两种格式,或者您的用户采用一种格式,而另一种格式在您的软件的实现细节中使用 - 但如果您具有直接识别用户选项的字符串格式化函数,则可能会消失?

Do you really need both formats, or is one format adopted by your users and the other is used inside the implementation details of your software -- but could go away if you had string formatting functions that recognized your users' options directly?

少钕鈤記 2024-10-02 15:01:53

你不需要。 Windows 内置了这种格式。您可以简单地从 User32.dll P/Invoke wsprintf

You don't need to. Windows has that formatting built-in. You can simply P/Invoke wsprintf from User32.dll.

吃不饱 2024-10-02 15:01:53
Private Const _format_string = "\%(?<length>\d+)?(\.?(?<precision>\d+)?)(?<type>\w)"

Public Shared Function ToNet(format As String) As String
    Dim regex As New Regex(_format_string, RegexOptions.IgnoreCase _
                           Or RegexOptions.CultureInvariant _
                           Or RegexOptions.IgnorePatternWhitespace _
                           Or RegexOptions.Compiled)

    Dim m As Match = regex.Match(format)
    Dim numberTypeFormat As String = String.Empty
    Dim precision As Integer = 1
    Dim precisionFieldName As String = "precision"

    If m.Success Then
        Select Case m.Groups("type").Value
            Case "d", "i", "n", "u", "o"
                numberTypeFormat = "D"
                precisionFieldName = "length"

            Case "x", "X"
                numberTypeFormat = "X"

            Case "f", "F"
                numberTypeFormat = "N"
                precision = 6

            Case "e", "E"
                numberTypeFormat = "E"
                precision = 6

            Case "s"
                Throw New ArgumentException("String type format string not supported", "format")

        End Select

        If m.Groups(precisionFieldName).Success Then
            precision = Integer.Parse(m.Groups(precisionFieldName).Value)

        End If

        Return String.Format("{0}{1}", numberTypeFormat, precision)


    Else
        Throw New ArgumentException("C++ Format string not recognized", "format")

        Return String.Empty

    End If

End Function
Private Const _format_string = "\%(?<length>\d+)?(\.?(?<precision>\d+)?)(?<type>\w)"

Public Shared Function ToNet(format As String) As String
    Dim regex As New Regex(_format_string, RegexOptions.IgnoreCase _
                           Or RegexOptions.CultureInvariant _
                           Or RegexOptions.IgnorePatternWhitespace _
                           Or RegexOptions.Compiled)

    Dim m As Match = regex.Match(format)
    Dim numberTypeFormat As String = String.Empty
    Dim precision As Integer = 1
    Dim precisionFieldName As String = "precision"

    If m.Success Then
        Select Case m.Groups("type").Value
            Case "d", "i", "n", "u", "o"
                numberTypeFormat = "D"
                precisionFieldName = "length"

            Case "x", "X"
                numberTypeFormat = "X"

            Case "f", "F"
                numberTypeFormat = "N"
                precision = 6

            Case "e", "E"
                numberTypeFormat = "E"
                precision = 6

            Case "s"
                Throw New ArgumentException("String type format string not supported", "format")

        End Select

        If m.Groups(precisionFieldName).Success Then
            precision = Integer.Parse(m.Groups(precisionFieldName).Value)

        End If

        Return String.Format("{0}{1}", numberTypeFormat, precision)


    Else
        Throw New ArgumentException("C++ Format string not recognized", "format")

        Return String.Empty

    End If

End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文