本文整理汇总了VB.NET中System.Byte结构体的典型用法代码示例。如果您正苦于以下问题:VB.NET Byte结构体的具体用法?VB.NET Byte怎么用?VB.NET Byte使用的例子?那么, 这里精选的结构体代码示例或许可以为您提供帮助。
在下文中一共展示了Byte结构体的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: CByte
Dim int1 As Integer = 128
Try
Dim value1 As Byte = CByte(int1)
Console.WriteLine(value1)
Catch e As OverflowException
Console.WriteLine("{0} is out of range of a byte.", int1)
End Try
Dim dbl2 As Double = 3.997
Try
Dim value2 As Byte = CByte(dbl2)
Console.WriteLine(value2)
Catch e As OverflowException
Console.WriteLine("{0} is out of range of a byte.", dbl2)
End Try
输出:
128 4
示例2: numbers
Dim numbers() As Integer = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue }
Dim result As Byte
For Each number As Integer In numbers
Try
result = Convert.ToByte(number)
Console.WriteLIne("Converted the {0} value {1} to the {2} value {3}.", _
number.GetType().Name, number, _
result.GetType().Name, result)
Catch e As OverflowException
Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", _
number.GetType().Name, number)
End Try
Next
输出:
The Int32 value -2147483648 is outside the range of the Byte type. The Int32 value -1 is outside the range of the Byte type. Converted the Int32 value 0 to the Byte value 0. Converted the Int32 value 121 to the Byte value 121. The Int32 value 340 is outside the range of the Byte type. The Int32 value 2147483647 is outside the range of the Byte type.
示例3:
Dim string1 As String = "244"
Try
Dim byte1 As Byte = Byte.Parse(string1)
Console.WriteLine(byte1)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of a byte.", string1)
Catch e As FormatException
Console.WriteLine("'{0}' is out of range of a byte.", string1)
End Try
Dim string2 As String = "F9"
Try
Dim byte2 As Byte = Byte.Parse(string2,
System.Globalization.NumberStyles.HexNumber)
Console.WriteLine(byte2)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of a byte.", string2)
Catch e As FormatException
Console.WriteLine("'{0}' is out of range of a byte.", string2)
End Try
输出:
244 249
示例4: numbers
Dim numbers() As Byte = { 0, 16, 104, 213 }
For Each number As Byte In numbers
' Display value using default formatting.
Console.Write("{0,-3} --> ", number.ToString())
' Display value with 3 digits and leading zeros.
Console.Write(number.ToString("D3") + " ")
' Display value with hexadecimal.
Console.Write(number.ToString("X2") + " ")
' Display value with four hexadecimal digits.
Console.WriteLine(number.ToString("X4"))
Next
输出:
0 --> 000 00 0000 16 --> 016 10 0010 104 --> 104 68 0068 213 --> 213 D5 00D5
示例5: numbers
Dim numbers() As Byte = { 0, 16, 104, 213 }
Console.WriteLine("{0} {1,8} {2,5} {3,5}", _
"Value", "Binary", "Octal", "Hex")
For Each number As Byte In numbers
Console.WriteLine("{0,5} {1,8} {2,5} {3,5}", _
number, Convert.ToString(number, 2), _
Convert.ToString(number, 8), _
Convert.ToString(number, 16))
Next
输出:
Value Binary Octal Hex 0 0 0 0 16 10000 20 10 104 1101000 150 68 213 11010101 325 d5
示例6: Example
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim values() As String = { Convert.ToString(12, 16), _
Convert.ToString(123, 16), _
Convert.ToString(245, 16) }
Dim mask As Byte = &hFE
For Each value As String In values
Dim byteValue As Byte = Byte.Parse(value, NumberStyles.AllowHexSpecifier)
Console.WriteLine("{0} And {1} = {2}", byteValue, mask, _
byteValue And mask)
Next
End Sub
End Module
输出:
12 And 254 = 12 123 And 254 = 122 245 And 254 = 244
示例7: Example
' 导入命名空间
Imports System.Collections.Generic
Imports System.Globalization
Public Structure ByteString
Public Value As String
Public Sign As Integer
End Structure
Module Example
Public Sub Main()
Dim values() As ByteString = CreateArray(-15, 123, 245)
Dim mask As Byte = &h14 ' Mask all bits but 2 and 4.
For Each strValue As ByteString In values
Dim byteValue As Byte = Byte.Parse(strValue.Value, NumberStyles.AllowHexSpecifier)
Console.WriteLine("{0} ({1}) And {2} ({3}) = {4} ({5})", _
strValue.Sign * byteValue, _
Convert.ToString(byteValue, 2), _
mask, Convert.ToString(mask, 2), _
(strValue.Sign And Math.Sign(mask)) * (byteValue And mask), _
Convert.ToString(byteValue And mask, 2))
Next
End Sub
Private Function CreateArray(ParamArray values() As Object) As ByteString()
Dim byteStrings As New List(Of ByteString)
For Each value As Object In values
Dim temp As New ByteString()
Dim sign As Integer = Math.Sign(value)
temp.Sign = sign
' Change two's complement to magnitude-only representation.
value = value * sign
temp.Value = Convert.ToString(value, 16)
byteStrings.Add(temp)
Next
Return byteStrings.ToArray()
End Function
End Module
输出:
-15 (1111) And 20 (10100) = 4 (100) 123 (1111011) And 20 (10100) = 16 (10000) 245 (11110101) And 20 (10100) = 20 (10100)