wiki.php 用Markdown写wiki是一种什么样的体验?

一些简易实用的数据转换函数.md

最后更新于 2019-10-06 15:00:23

自己收集或编写的简易数据转换函数,包括==图像与数据互相转换==、==繁简互相转换==、==半角全角互相转换==、==字符解析为Color==、==清除数组中相同字符串==、==获取字符串字节长==、==int与uint之间的安全转换==。

''' <summary>将图像转换为数据</summary>
    Shared Function m_pic2bytes(ByVal pic As Bitmap) As Byte()
        Using ms As New IO.MemoryStream
            pic.Save(ms, pic.RawFormat)
            Dim b(ms.Length - 1) As Byte
            ms.Position = 0
            ms.Read(b, 0, b.Length - 1)
            Return b
        End Using
    End Function

    ''' <summary>将数据转换为图像</summary>
    Shared Function m_bytes2pic(ByVal ByteArray As Byte()) As Bitmap
        Using ms As New IO.MemoryStream(ByteArray)
            Dim bit As New Bitmap(ms)
            Return bit
        End Using
    End Function

    ''' <summary>int与uint安全互转 不改变内存结构</summary>
    Shared Function m_int2uint(ByVal int As Integer) As UInteger
        If int >= 0 Then Return int
        int = Not int
        Return Not CType(int, UInteger)
    End Function
    ''' <summary>int与uint安全互转 不改变内存结构</summary>
    Shared Function m_uint2int(ByVal uint As UInteger) As Integer
        If uint <= Integer.MaxValue Then Return uint
        uint = Not uint
        Return Not CType(uint, Integer)
    End Function

    ''' <summary>获取字符串字节长度</summary>
    Shared Function GetStringLength(ByVal str As String) As Integer
        Return Encoding.Default.GetBytes(str).Length
    End Function

    ''' <summary>清除字符串数组中的重复项</summary>
    ''' <param name="strArray"></param>
    ''' <param name="maxElementLength">字符串数组中单个元素的最大长度</param>
    Shared Function DistinctStringArray(ByVal strArray As String(), ByVal maxElementLength As Integer) As String()
        Dim h As New Hashtable
        For Each s As String In strArray
            Dim k As String = s
            If maxElementLength > 0 And k.Length > maxElementLength Then
                k = k.Substring(0, maxElementLength)
            End If
            h(k.Trim) = s
        Next
        Dim result(h.Count) As String
        h.Keys.CopyTo(result, 0)
        Return result
    End Function

    ''' <summary>返回数据格式化文本</summary>
    Shared Function FormatBytesStr(ByVal bytes As Integer) As String
        If bytes > 1073741824 Then
            Return (bytes / 1073741824).ToString("0") + "G"
        ElseIf bytes > 1048576 Then
            Return (bytes / 1048576).ToString("0") + "M"
        ElseIf bytes > 1024 Then
            Return (bytes / 1024).ToString("0") + "K"
        Else
            Return bytes.ToString + "字节" '"bytes"
        End If
    End Function

    ''' <summary>从字符串分析出颜色</summary>
    Shared Function ToColor(ByVal color As String) As Color
        If Not color.StartsWith("#") Then
            Return System.Drawing.Color.FromName(color)
        End If

        Dim red, green, blue As Integer
        Dim rgb As Char()

        color = color.TrimStart("#")
        color = Regex.Replace(color.ToLower, "g-zG-Z", "")

        Select Case color.Length
            Case 3
                rgb = color.ToCharArray
                red = Convert.ToInt32(rgb(0).ToString + rgb(0).ToString, 16)
                green = Convert.ToInt32(rgb(1).ToString + rgb(1).ToString, 16)
                blue = Convert.ToInt32(rgb(2).ToString + rgb(2).ToString, 16)
                Return System.Drawing.Color.FromArgb(red, green, blue)
            Case 6
                rgb = color.ToCharArray
                red = Convert.ToInt32(rgb(0).ToString + rgb(1).ToString, 16)
                green = Convert.ToInt32(rgb(2).ToString + rgb(3).ToString, 16)
                blue = Convert.ToInt32(rgb(4).ToString + rgb(5).ToString, 16)
                Return System.Drawing.Color.FromArgb(red, green, blue)
            Case Else
                Return System.Drawing.Color.FromArgb(0)
        End Select
    End Function

    ''' <summary>简体转换为繁体</summary>
    Shared Function ToTraditional(ByVal str As String) As String
        Return StrConv(str, VbStrConv.TraditionalChinese)
    End Function
    ''' <summary>繁体转换为简体</summary>
    Shared Function ToSimplified(ByVal str As String) As String
        Return StrConv(str, VbStrConv.SimplifiedChinese)
    End Function

    ''' <summary>全角转换为半角</summary>
    Shared Function ToDBC(ByVal str As String) As String
        Dim ca As Char() = str.ToCharArray
        Dim c As Integer
        For i As Integer = 0 To ca.Length - 1
            c = AscW(ca(i))
            If c = 12288 Then '如果是空格
                ca(i) = ChrW(32)
            ElseIf c > 65280 AndAlso c < 65375 Then    '如果是其他全角字符
                ca(i) = ChrW(c - 65248)
            End If
        Next
        Return New String(ca)
    End Function

    ''' <summary>半角转换为全角</summary>
    Shared Function ToSBC(ByVal str As String) As String
        Dim ca As Char() = str.ToCharArray
        Dim c As Integer
        For i As Integer = 0 To ca.Length - 1
            c = AscW(ca(i))
            If c = 32 Then
                ca(i) = ChrW(12288)
            ElseIf c < 127 Then
                ca(i) = ChrW(c + 65248)
            End If
        Next
        Return New String(ca)
    End Function