本文整理汇总了VB.NET中System.Convert.ToSByte方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Convert.ToSByte方法的具体用法?VB.NET Convert.ToSByte怎么用?VB.NET Convert.ToSByte使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Convert
的用法示例。
在下文中一共展示了Convert.ToSByte方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
Module Example
Public Sub Main()
Dim bases() As Integer = { 2, 8, 16}
Dim values() As String = { "FF", "81", "03", "11", "8F", "01", "1C", _
"111", "123", "18A" }
' Convert to each supported base.
For Each base As Integer In bases
Console.WriteLine("Converting strings in base {0}:", base)
For Each value As String In values
Console.Write(" '{0,-5} --> ", value + "'")
Try
Console.WriteLine(Convert.ToSByte(value, base))
Catch e As FormatException
Console.WriteLine("Bad Format")
Catch e As OverflowException
Console.WriteLine("Out of Range")
End Try
Next
Console.WriteLine()
Next
End Sub
End Module
输出:
Converting strings in base 2: FF' --> Bad Format 81' --> Bad Format 03' --> Bad Format 11' --> 3 8F' --> Bad Format 01' --> 1 1C' --> Bad Format 111' --> 7 123' --> Bad Format 18A' --> Bad Format Converting strings in base 8: FF' --> Bad Format 81' --> Bad Format 03' --> 3 11' --> 9 8F' --> Bad Format 01' --> 1 1C' --> Bad Format 111' --> 73 123' --> 83 18A' --> Bad Format Converting strings in base 16: FF' --> -1 81' --> -127 03' --> 3 11' --> 17 8F' --> -113 01' --> 1 1C' --> 28 111' --> Out of Range 123' --> Out of Range 18A' --> Out of Range
示例2:
' Create a hexadecimal value out of range of the SByte type.
Dim value As String = Convert.ToString(Byte.MaxValue, 16)
' Convert it back to a number.
Try
Dim number As SByte = Convert.ToSByte(value, 16)
Console.WriteLine("0x{0} converts to {1}.", value, number)
Catch e As OverflowException
Console.WriteLine("Unable to convert '0x{0}' to a signed byte.", value)
End Try
示例3: And
' Create a negative hexadecimal value out of range of the Long type.
Dim sourceNumber As Byte = Byte.MaxValue
Dim isSigned As Boolean = Math.Sign(sourceNumber.MinValue) = -1
Dim value As String = Convert.ToString(sourceNumber, 16)
Dim targetNumber As SByte
Try
targetNumber = Convert.ToSByte(value, 16)
If Not isSigned And ((targetNumber And &H80) <> 0) Then
Throw New OverflowException()
Else
Console.WriteLine("0x{0} converts to {1}.", value, targetNumber)
End If
Catch e As OverflowException
Console.WriteLine("Unable to convert '0x{0}' to a signed byte.", value)
End Try
' Displays the following to the console:
' Unable to convert '0xff' to a signed byte.
示例4: ToSByteProviderDemo
' Example of the Convert.ToSByte( String ) and
' Convert.ToSByte( String, IFormatProvider ) methods.
Imports System.Globalization
Module ToSByteProviderDemo
Dim format As String = "{0,-20}{1,-20}{2}"
' Get the exception type name; remove the namespace prefix.
Function GetExceptionType( ex As Exception ) As String
Dim exceptionType As String = ex.GetType( ).ToString( )
Return exceptionType.Substring( _
exceptionType.LastIndexOf( "."c ) + 1 )
End Function
Sub ConvertToSByte( numericStr As String, _
provider As IFormatProvider )
Dim defaultValue As Object
Dim providerValue As Object
' Convert numericStr to SByte without a format provider.
Try
defaultValue = Convert.ToSByte( numericStr )
Catch ex As Exception
defaultValue = GetExceptionType( ex )
End Try
' Convert numericStr to SByte with a format provider.
Try
providerValue = Convert.ToSByte( numericStr, provider )
Catch ex As Exception
providerValue = GetExceptionType( ex )
End Try
Console.WriteLine( format, numericStr, _
defaultValue, providerValue )
End Sub
Sub Main( )
' Create a NumberFormatInfo object and set several of its
' properties that apply to numbers.
Dim provider As NumberFormatInfo = new NumberFormatInfo( )
' These properties affect the conversion.
provider.NegativeSign = "neg "
provider.PositiveSign = "pos "
' These properties do not affect the conversion.
' The input string cannot have decimal and group separators.
provider.NumberDecimalSeparator = "."
provider.NumberNegativePattern = 0
Console.WriteLine( "This example of" & vbCrLf & _
" Convert.ToSByte( String ) and " & vbCrLf & _
" Convert.ToSByte( String, IFormatProvider ) " & _
vbCrLf & "generates the following output. It " & _
"converts several strings to " & vbCrLf & "SByte " & _
"values, using default formatting " & _
"or a NumberFormatInfo object." & vbCrLf )
Console.WriteLine( format, "String to convert", _
"Default/exception", "Provider/exception" )
Console.WriteLine( format, "-----------------", _
"-----------------", "------------------" )
' Convert strings, with and without an IFormatProvider.
ConvertToSByte( "123", provider )
ConvertToSByte( "+123", provider )
ConvertToSByte( "pos 123", provider )
ConvertToSByte( "-123", provider )
ConvertToSByte( "neg 123", provider )
ConvertToSByte( "123.", provider )
ConvertToSByte( "(123)", provider )
ConvertToSByte( "128", provider )
ConvertToSByte( "-129", provider )
End Sub
End Module
' This example of
' Convert.ToSByte( String ) and
' Convert.ToSByte( String, IFormatProvider )
' generates the following output. It converts several strings to
' SByte values, using default formatting or a NumberFormatInfo object.
'
' String to convert Default/exception Provider/exception
' ----------------- ----------------- ------------------
' 123 123 123
' +123 123 FormatException
' pos 123 FormatException 123
' -123 -123 FormatException
' neg 123 FormatException -123
' 123. FormatException FormatException
' (123) FormatException FormatException
' 128 OverflowException OverflowException
' -129 OverflowException FormatException
示例5: ArgumentException
' 导入命名空间
Imports System.Globalization
Public Enum SignBit As Integer
Positive = 1
Zero = 0
Negative = -1
End Enum
Public Structure ByteString : Implements IConvertible
Private signBit As SignBit
Private byteString As String
Public Property Sign As SignBit
Set
signBit = value
End Set
Get
Return signBit
End Get
End Property
Public Property Value As String
Set
If value.Trim().Length > 2 Then
Throw New ArgumentException("The string representation of a byte cannot have more than two characters.")
Else
byteString = value
End If
End Set
Get
Return byteString
End Get
End Property
' IConvertible implementations.
Public Function GetTypeCode() As TypeCode _
Implements IConvertible.GetTypeCode
Return TypeCode.Object
End Function
Public Function ToBoolean(provider As IFormatProvider) As Boolean _
Implements IConvertible.ToBoolean
If signBit = SignBit.Zero Then
Return False
Else
Return True
End If
End Function
Public Function ToByte(provider As IFormatProvider) As Byte _
Implements IConvertible.ToByte
If signBit = signBit.Negative Then
Throw New OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToSByte(byteString, 16)))
Else
Return Byte.Parse(byteString, NumberStyles.HexNumber)
End If
End Function
Public Function ToChar(provider As IFormatProvider) As Char _
Implements IConvertible.ToChar
If signBit = signBit.Negative Then
Throw New OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToSByte(byteString, 16)))
Else
Dim byteValue As Byte = Byte.Parse(Me.byteString, NumberStyles.HexNumber)
Return Convert.ToChar(byteValue)
End If
End Function
Public Function ToDateTime(provider As IFormatProvider) As Date _
Implements IConvertible.ToDateTime
Throw New InvalidCastException("ByteString to DateTime conversion is not supported.")
End Function
Public Function ToDecimal(provider As IFormatProvider) As Decimal _
Implements IConvertible.ToDecimal
If signBit = signBit.Negative Then
Dim byteValue As SByte = SByte.Parse(byteString, NumberStyles.HexNumber)
Return Convert.ToDecimal(byteValue)
Else
Dim byteValue As Byte = Byte.Parse(byteString, NumberStyles.HexNumber)
Return Convert.ToDecimal(byteValue)
End If
End Function
Public Function ToDouble(provider As IFormatProvider) As Double _
Implements IConvertible.ToDouble
If signBit = signBit.Negative Then
Return Convert.ToDouble(SByte.Parse(byteString, NumberStyles.HexNumber))
Else
Return Convert.ToDouble(Byte.Parse(byteString, NumberStyles.HexNumber))
End If
End Function
Public Function ToInt16(provider As IFormatProvider) As Int16 _
Implements IConvertible.ToInt16
If signBit = signBit.Negative Then
Return Convert.ToInt16(SByte.Parse(byteString, NumberStyles.HexNumber))
Else
Return Convert.ToInt16(Byte.Parse(byteString, NumberStyles.HexNumber))
End If
End Function
Public Function ToInt32(provider As IFormatProvider) As Int32 _
Implements IConvertible.ToInt32
If signBit = signBit.Negative Then
Return Convert.ToInt32(SByte.Parse(byteString, NumberStyles.HexNumber))
Else
Return Convert.ToInt32(Byte.Parse(byteString, NumberStyles.HexNumber))
End If
End Function
Public Function ToInt64(provider As IFormatProvider) As Int64 _
Implements IConvertible.ToInt64
If signBit = signBit.Negative Then
Return Convert.ToInt64(SByte.Parse(byteString, NumberStyles.HexNumber))
Else
Return Convert.ToInt64(Byte.Parse(byteString, NumberStyles.HexNumber))
End If
End Function
Public Function ToSByte(provider As IFormatProvider) As SByte _
Implements IConvertible.ToSByte
Try
Return SByte.Parse(byteString, NumberStyles.HexNumber)
Catch e As OverflowException
Throw New OverflowException(String.Format("{0} is outside the range of the SByte type.", _
Byte.Parse(byteString, NumberStyles.HexNumber)))
End Try
End Function
Public Function ToSingle(provider As IFormatProvider) As Single _
Implements IConvertible.ToSingle
If signBit = signBit.Negative Then
Return Convert.ToSingle(SByte.Parse(byteString, NumberStyles.HexNumber))
Else
Return Convert.ToSingle(Byte.Parse(byteString, NumberStyles.HexNumber))
End If
End Function
Public Overloads Function ToString(provider As IFormatProvider) As String _
Implements IConvertible.ToString
Return Me.byteString
End Function
Public Function ToType(conversionType As Type, provider As IFormatProvider) As Object _
Implements IConvertible.ToType
Select Case Type.GetTypeCode(conversionType)
Case TypeCode.Boolean
Return Me.ToBoolean(Nothing)
Case TypeCode.Byte
Return Me.ToByte(Nothing)
Case TypeCode.Char
Return Me.ToChar(Nothing)
Case TypeCode.DateTime
Return Me.ToDateTime(Nothing)
Case TypeCode.Decimal
Return Me.ToDecimal(Nothing)
Case TypeCode.Double
Return Me.ToDouble(Nothing)
Case TypeCode.Int16
Return Me.ToInt16(Nothing)
Case TypeCode.Int32
Return Me.ToInt32(Nothing)
Case TypeCode.Int64
Return Me.ToInt64(Nothing)
Case TypeCode.Object
If GetType(ByteString).Equals(conversionType) Then
Return Me
Else
Throw New InvalidCastException(String.Format("Conversion to a {0} is not supported.", conversionType.Name))
End If
Case TypeCode.SByte
Return Me.ToSByte(Nothing)
Case TypeCode.Single
Return Me.ToSingle(Nothing)
Case TypeCode.String
Return Me.ToString(Nothing)
Case TypeCode.UInt16
Return Me.ToUInt16(Nothing)
Case TypeCode.UInt32
Return Me.ToUInt32(Nothing)
Case TypeCode.UInt64
Return Me.ToUInt64(Nothing)
Case Else
Throw New InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name))
End Select
End Function
Public Function ToUInt16(provider As IFormatProvider) As UInt16 _
Implements IConvertible.ToUInt16
If signBit = signBit.Negative Then
Throw New OverflowException(String.Format("{0} is outside the range of the UInt16 type.", _
SByte.Parse(byteString, NumberStyles.HexNumber)))
Else
Return Convert.ToUInt16(Byte.Parse(byteString, NumberStyles.HexNumber))
End If
End Function
Public Function ToUInt32(provider As IFormatProvider) As UInt32 _
Implements IConvertible.ToUInt32
If signBit = signBit.Negative Then
Throw New OverflowException(String.Format("{0} is outside the range of the UInt32 type.", _
SByte.Parse(byteString, NumberStyles.HexNumber)))
Else
Return Convert.ToUInt32(Byte.Parse(byteString, NumberStyles.HexNumber))
End If
End Function
Public Function ToUInt64(provider As IFormatProvider) As UInt64 _
Implements IConvertible.ToUInt64
If signBit = signBit.Negative Then
Throw New OverflowException(String.Format("{0} is outside the range of the UInt64 type.", _
SByte.Parse(byteString, NumberStyles.HexNumber)))
Else
Return Convert.ToUInt64(Byte.Parse(byteString, NumberStyles.HexNumber))
End If
End Function
End Structure
示例6: modMain
Module modMain
Public Sub Main()
Dim positiveByte As SByte = 120
Dim negativeByte As SByte = -101
Dim positiveString As New ByteString()
positiveString.Sign = CType(Math.Sign(positiveByte), SignBit)
positiveString.Value = positiveByte.ToString("X2")
Dim negativeString As New ByteString()
negativeString.Sign = CType(Math.Sign(negativeByte), SignBit)
negativeString.Value = negativeByte.ToString("X2")
Try
Console.WriteLine("'{0}' converts to {1}.", positiveString.Value, Convert.ToSByte(positiveString))
Catch e As OverflowException
Console.WriteLine("0x{0} is outside the range of the Byte type.", positiveString.Value)
End Try
Try
Console.WriteLine("'{0}' converts to {1}.", negativeString.Value, Convert.ToSByte(negativeString))
Catch e As OverflowException
Console.WriteLine("0x{0} is outside the range of the Byte type.", negativeString.Value)
End Try
End Sub
End Module
输出:
78' converts to 120. 9B' converts to -101.
示例7: numbers
Dim numbers() As ULong = { UInt64.MinValue, 121, 340, UInt64.MaxValue }
Dim result As SByte
For Each number As ULong In numbers
Try
result = Convert.ToSByte(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 SByte type.", _
number.GetType().Name, number)
End Try
Next
输出:
Converted the UInt64 value 0 to the SByte value 0. Converted the UInt64 value 121 to the SByte value 121. The UInt64 value 340 is outside the range of the SByte type. The UInt64 value 18446744073709551615 is outside the range of the SByte type.
示例8: numbers
Dim numbers() As UInteger = { UInt32.MinValue, 121, 340, UInt32.MaxValue }
Dim result As SByte
For Each number As UInteger In numbers
Try
result = Convert.ToSByte(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 SByte type.", _
number.GetType().Name, number)
End Try
Next
输出:
Converted the UInt32 value 0 to the SByte value 0. Converted the UInt32 value 121 to the SByte value 121. The UInt32 value 340 is outside the range of the SByte type. The UInt32 value 4294967295 is outside the range of the SByte type.
示例9: values
Dim values() As Object = { True, -12, 163, 935, "x"c, "104", "103.0", "-1", _
"1.00e2", "One", 1.00e2}
Dim result As SByte
For Each value As Object In values
Try
result = Convert.ToSByte(value)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
value.GetType().Name, value, _
result.GetType().Name, result)
Catch e As OverflowException
Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", _
value.GetType().Name, value)
Catch e As FormatException
Console.WriteLine("The {0} value {1} is not in a recognizable format.", _
value.GetType().Name, value)
Catch e As InvalidCastException
Console.WriteLine("No conversion to a Byte exists for the {0} value {1}.", _
value.GetType().Name, value)
End Try
Next
输出:
Converted the Boolean value True to the SByte value 1. Converted the Int32 value -12 to the SByte value -12. The Int32 value 163 is outside the range of the SByte type. The Int32 value 935 is outside the range of the SByte type. Converted the Char value x to the SByte value 120. Converted the String value 104 to the SByte value 104. The String value 103.0 is not in a recognizable format. Converted the String value -1 to the SByte value -1. The String value 1.00e2 is not in a recognizable format. The String value One is not in a recognizable format. Converted the Double value 100 to the SByte value 100.
示例10: numbers
Dim numbers() As UShort = { UInt16.MinValue, 121, 340, UInt16.MaxValue }
Dim result As SByte
For Each number As UShort In numbers
Try
result = Convert.ToSByte(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 SByte type.", _
number.GetType().Name, number)
End Try
Next
输出:
Converted the UInt16 value 0 to the SByte value 0. Converted the UInt16 value 121 to the SByte value 121. The UInt16 value 340 is outside the range of the SByte type. The UInt16 value 65535 is outside the range of the SByte type.
示例11: numbers
Dim numbers() As Integer = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue }
Dim result As SByte
For Each number As Integer In numbers
Try
result = Convert.ToSByte(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 SByte type.", _
number.GetType().Name, number)
End Try
Next
输出:
The Int32 value -2147483648 is outside the range of the SByte type. Converted the Int32 value -1 to the SByte value -1. Converted the Int32 value 0 to the SByte value 0. Converted the Int32 value 121 to the SByte value 121. The Int32 value 340 is outside the range of the SByte type. The Int32 value 2147483647 is outside the range of the SByte type.
示例12: numbers
Dim numbers() As Long = { Int64.MinValue, -1, 0, 121, 340, Int64.MaxValue }
Dim result As SByte
For Each number As Long In numbers
Try
result = Convert.ToSByte(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 SByte type.", _
number.GetType().Name, number)
End Try
Next
输出:
The Int64 value -9223372036854775808 is outside the range of the SByte type. Converted the Int64 value -1 to the SByte value -1. Converted the Int64 value 0 to the SByte value 0. Converted the Int64 value 121 to the SByte value 121. The Int64 value 340 is outside the range of the SByte type. The Int64 value 9223372036854775807 is outside the range of the SByte type.
示例13: numbers
Dim numbers() As Byte = { Byte.MinValue, 10, 100, Byte.MaxValue }
Dim result As SByte
For Each number As Byte In numbers
Try
result = Convert.ToSByte(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 SByte type.", _
number.GetType().Name, number)
End Try
Next
输出:
Converted the Byte value 0 to the SByte value 0. Converted the Byte value 10 to the SByte value 10. Converted the Byte value 100 to the SByte value 100. The Byte value 255 is outside the range of the SByte type.
示例14: chars
Dim chars() As Char = { "a"c, "z"c, ChrW(7), ChrW(200), ChrW(1023) }
For Each ch As Char in chars
Try
Dim result As SByte = Convert.ToSByte(ch)
Console.WriteLine("{0} is converted to {1}.", ch, result)
Catch e As OverflowException
Console.WriteLine("Unable to convert u+{0} to a byte.", _
AscW(ch).ToString("X4"))
End Try
Next
输出:
a is converted to 97. z is converted to 122. is converted to 7. Unable to convert u+00C8 to a byte. Unable to convert u+03FF to a byte.
示例15:
Dim falseFlag As Boolean = False
Dim trueFlag As Boolean = True
Console.WriteLine("{0} converts to {1}.", falseFlag, _
Convert.ToSByte(falseFlag))
Console.WriteLine("{0} converts to {1}.", trueFlag, _
Convert.ToSByte(trueFlag))
输出:
False converts to 0. True converts to 1.