Click here to Skip to main content
15,940,246 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: multi form in the same form Pin
M-Hall10-Feb-07 7:10
M-Hall10-Feb-07 7:10 
QuestionSetting Readonly on columns in Datagrid in the code behind Pin
BitterChick8-Feb-07 11:38
BitterChick8-Feb-07 11:38 
AnswerRe: Setting Readonly on columns in Datagrid in the code behind Pin
gauthee8-Feb-07 18:02
gauthee8-Feb-07 18:02 
QuestionDataGridView formatting breaks validation Pin
penguin50008-Feb-07 11:25
penguin50008-Feb-07 11:25 
AnswerRe: DataGridView formatting breaks validation Pin
penguin500020-Jul-07 10:56
penguin500020-Jul-07 10:56 
QuestionConverting huge numbers for Decimal to Hexadecimal and visa versa Pin
Fu Manchu8-Feb-07 11:19
Fu Manchu8-Feb-07 11:19 
AnswerRe: Converting huge numbers for Decimal to Hexadecimal and visa versa Pin
Johan Hakkesteegt8-Feb-07 22:26
Johan Hakkesteegt8-Feb-07 22:26 
AnswerRe: Converting huge numbers for Decimal to Hexadecimal and visa versa [modified] Pin
TwoFaced9-Feb-07 16:28
TwoFaced9-Feb-07 16:28 
I couldn't find any built in functions to do such large conversions. However, I had some code to convert Hex to Dec and vice versa. I made some modifications to ensure the conversion would work with the largest decimal number allowed. Here is the code which I believe works perfectly. If you discover a problem let me know. Hope this helps.
Public Class Form1
    'Test conversion of the largest allowed decimal value
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim msg As String = "MaxDecimal: " & Decimal.MaxValue.ToString & _
            vbCrLf & "MaxDecimal as Hex: " & DecToHex(Decimal.MaxValue).ToUpper & _
            vbCrLf & "Hex converted back: " & HexToDec(DecToHex(Decimal.MaxValue)).ToString

        MsgBox(msg)
    End Sub

    '---MAIN CONVERSION FUNCTIONS---

    'Converts a decimal type to it's Hex value
    'Does not support fractional numbers, so don't pass them :)
    'Actually they just get rounded so nothing to worry about.
    Private Function DecToHex(ByVal number As Decimal) As String
        number = Decimal.Round(number)
        Dim result As New System.Text.StringBuilder

        Do While number > 0
            Dim remainder As Integer = number Mod 16

            number = number / 16D
            number = Decimal.Truncate(number)
            result.Insert(0, DecToHexChar(remainder))
        Loop
        Return result.ToString
    End Function

    'Converts a string of Hex characters to it's decimal equivalent
    'Does not provide any validation.  String must have nothing but
    'valid Hex values.  Although no errors should occur.
    Private Function HexToDec(ByVal hex As String) As Decimal
        Dim result As Decimal

        For i As Decimal = 0 To hex.Length - 1
            Dim chr As Char = hex.Chars(i)
            result += (HexCharToDec(chr) * Pow(16, hex.Length - 1 - i))
        Next
        Return result
    End Function

    '---HELPER FUNCTIONS---

    'Converts a Decimal value 0 to 15 to it's Hex character equivalent
    'If number is anything else it returns String.Empty
    Private Function DecToHexChar(ByVal number As Integer) As String
        If number < 10 Then Return number
        Select Case number
            Case 10 : Return "A"
            Case 11 : Return "B"
            Case 12 : Return "C"
            Case 13 : Return "D"
            Case 14 : Return "E"
            Case 15 : Return "F"
        End Select
        Return String.Empty
    End Function

    'Converts a Hex character to it's decimal equivalent
    'Valid characters: 0-9, A-F
    'Returns -1 if invalid
    Private Function HexCharToDec(ByVal chr As Char) As Decimal
        If Char.IsDigit(chr) Then Return Decimal.Parse(chr)

        Select Case Char.ToUpper(chr)
            Case "A" : Return 10D
            Case "B" : Return 11D
            Case "C" : Return 12D
            Case "D" : Return 13D
            Case "E" : Return 14D
            Case "F" : Return 15D
        End Select
        Return -1D
    End Function

    'Raises a number to a given power
    'Math.Pow and ^ were not options because they don't handle up to decimal
    Private Function Pow(ByVal number As Decimal, ByVal exponent As Integer) As Decimal
        Dim result As Decimal = 1
        For i As Integer = 1 To exponent
            result *= number
        Next
        Return result
    End Function
End Class



-- modified at 22:38 Friday 9th February, 2007
Questionenumerate objects Pin
RJGCarey8-Feb-07 9:17
RJGCarey8-Feb-07 9:17 
AnswerRe: enumerate objects Pin
Christian Graus8-Feb-07 9:27
protectorChristian Graus8-Feb-07 9:27 
GeneralRe: enumerate objects Pin
RJGCarey9-Feb-07 2:55
RJGCarey9-Feb-07 2:55 
GeneralRe: enumerate objects Pin
Christian Graus9-Feb-07 9:51
protectorChristian Graus9-Feb-07 9:51 
Questionvba script to monitor temperature and workgroup Pin
tomboy788-Feb-07 7:08
tomboy788-Feb-07 7:08 
Questioninstance of form Pin
charchabil038-Feb-07 6:35
charchabil038-Feb-07 6:35 
AnswerRe: instance of form Pin
MatrixCoder8-Feb-07 6:41
MatrixCoder8-Feb-07 6:41 
GeneralRe: instance of form Pin
charchabil038-Feb-07 7:11
charchabil038-Feb-07 7:11 
GeneralRe: instance of form Pin
Old John8-Feb-07 15:23
Old John8-Feb-07 15:23 
AnswerRe: instance of form [modified] Pin
TwoFaced8-Feb-07 7:42
TwoFaced8-Feb-07 7:42 
GeneralRe: instance of form Pin
charchabil0310-Feb-07 5:51
charchabil0310-Feb-07 5:51 
GeneralRe: instance of form Pin
TwoFaced10-Feb-07 7:10
TwoFaced10-Feb-07 7:10 
GeneralRe: instance of form Pin
charchabil0310-Feb-07 21:16
charchabil0310-Feb-07 21:16 
QuestionClosing a form Pin
jady848-Feb-07 5:51
jady848-Feb-07 5:51 
AnswerRe: Closing a form Pin
M-Hall8-Feb-07 6:21
M-Hall8-Feb-07 6:21 
Questionitem with same key has already been adding Pin
charchabil038-Feb-07 5:32
charchabil038-Feb-07 5:32 
AnswerRe: item with same key has already been adding Pin
Christian Graus8-Feb-07 9:04
protectorChristian Graus8-Feb-07 9:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.