• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

VB.NET Encoding.UTF8属性代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了VB.NET中System.Text.Encoding.UTF8属性的典型用法代码示例。如果您正苦于以下问题:VB.NET Encoding.UTF8属性的具体用法?VB.NET Encoding.UTF8怎么用?VB.NET Encoding.UTF8使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.Text.Encoding的用法示例。



在下文中一共展示了Encoding.UTF8属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。

示例1: Example

' 导入命名空间
Imports System.Text

Public Module Example
   Public Sub Main()
      ' Create a character array.
      Dim gkNumber As String = Char.ConvertFromUtf32(&h10154)
      Dim chars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), 
                             ChrW(&H03B2), gkNumber(0), gkNumber(1) }
 
      ' Get UTF-8 and UTF-16 encoders.
      Dim utf8 As Encoding = Encoding.UTF8
      Dim utf16 As Encoding = Encoding.Unicode

      ' Display the original characters' code units.
      Console.WriteLine("Original UTF-16 code units:")
      Dim utf16Bytes() As Byte = utf16.GetBytes(chars)
      For Each utf16Byte In utf16Bytes
         Console.Write("{0:X2} ", utf16Byte)
      Next
      Console.WriteLine()

      Console.WriteLine()
      ' Display the number of bytes required to encode the array.
      Dim reqBytes As Integer = utf8.GetByteCount(chars)
      Console.WriteLine("Exact number of bytes required: {0}", 
                        reqBytes)

      ' Display the maximum byte count.
      Dim maxBytes As Integer = utf8.GetMaxByteCount(chars.Length)
      Console.WriteLine("Maximum number of bytes required: {0}", 
                        maxBytes)
      Console.WriteLine()
      
      ' Encode the array of characters.
      Dim utf8Bytes() As Byte = utf8.GetBytes(chars)

      ' Display all the UTF-8-encoded bytes.
      Console.WriteLine("UTF-8-encoded code units:")
      For Each utf8Byte In utf8Bytes
         Console.Write("{0:X2} ", utf8Byte)
      Next
      Console.WriteLine()
   End Sub 
End Module
开发者ID:VB.NET开发者,项目名称:System.Text,代码行数:45,代码来源:Encoding.UTF8

输出:

Original UTF-16 code units:
7A 00 61 00 06 03 FD 01 B2 03 00 D8 54 DD

Exact number of bytes required: 12
Maximum number of bytes required: 24

UTF-8-encoded code units:
7A 61 CC 86 C7 BD CE B2 F0 90 85 94


示例2: Example

' 导入命名空间
Imports System.Text

Module Example
   Public Sub Main()
      Dim enc As Encoding = New UTF8Encoding(True, True)
      Dim value As String = String.Format("{0} {1}{2} {3}", 
                            ChrW(&h00C4), ChrW(&hD802), ChrW(&h0033), ChrW(&h00AE))
      
      Try
         Dim bytes() As Byte = enc.GetBytes(value)
         For Each byt As Byte In bytes
            Console.Write("{0:X2} ", byt)
         Next       
         Console.WriteLine()
         Dim value2 As String = enc.GetString(bytes)
         Console.WriteLine(value2)
      Catch e As EncoderFallbackException
         Console.WriteLine("Unable to encode {0} at index {1}", 
                           If(e.IsUnknownSurrogate(), 
                              String.Format("U+{0:X4} U+{1:X4}", 
                                            Convert.ToUInt16(e.CharUnknownHigh),
                                            Convert.ToUInt16(e.CharUnknownLow)),
                              String.Format("U+{0:X4}", 
                                            Convert.ToUInt16(e.CharUnknown))),
                           e.Index)
      End Try
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System.Text,代码行数:29,代码来源:Encoding.UTF8

输出:

Unable to encode U+D802 at index 2



注:本文中的System.Text.Encoding.UTF8属性示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VB.NET TimeSpan.TotalDays属性代码示例发布时间:2022-05-24
下一篇:
VB.NET Encoding.UTF7属性代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap