ZH's software thread

Place to talk about all that new hardware and decaying software you have.

Moderator: General Mods

Post Reply
ZH/Franky

Post by ZH/Franky »

Alright, I've been trying to do hex2bin and hex2dec today. Also tried to do hex input validation. None of these work, but I thought I might post what I have at the moment:

Code: Select all

Module Module1

    Private BinaryString, binr, choice, signed_or_unsigned, HexString, first_hex, hexr, pie As String
    Private digit, remain, quit, negative, oor, bits, disable_welcome_text, min_num, max_num, confirm As Double
    'Private pos As Double
    'uncomment the above if you are using Microsoft Visual Basic .NET 2005 Express

    Sub Main()
        display_options()
        program()
    End Sub

    Private Sub program()
        Console.WriteLine("(for a list of options, type n)")
        Console.WriteLine("What do you want to do?:")
        disable_welcome_text = 1
        choice = Console.ReadLine()
        Select Case choice
            Case "q"
                quit = 1
            Case "n"
                display_options()
            Case "bin2dec"
                validate_bits()
                Console.WriteLine("Signed or unsigned? If signed, use two's compliment")
                validate_sign()
                determine_range()
                validate_BinaryString()
                bin2dec()
                Console.WriteLine(digit)
                BinaryString = ""
                binr = ""
                digit = 0
            Case "dec2bin"
                validate_bits()
                Console.WriteLine("Signed or unsigned? (signed will be in two's compiment)")
                validate_sign()
                determine_range()
                validate_digit()
                BinaryString = ""
                binr = ""
                dec2bin()
                Console.WriteLine(BinaryString)
                BinaryString = ""
                binr = ""
                digit = 0
            Case "dec2hex"
                validate_bits()
                Console.WriteLine("Signed or unsigned? Signed will be in two's compliment")
                validate_sign()
                determine_range()
                validate_digit()
                BinaryString = ""
                binr = ""
                dec2hex()
                Console.WriteLine(HexString)
                BinaryString = ""
                binr = ""
                digit = 0
            Case "bin2hex"
                validate_bits()
                Console.WriteLine("Signed or unsigned? Signed will be in two's compliment")
                validate_sign()
                validate_BinaryString()
                bin2hex()
                Console.WriteLine(HexString)
            Case "hex2bin"
                validate_bits()
                Console.WriteLine("Signed or unsigned? Signed will be in two's compliment")
                validate_sign()
                'validate_hex()
                hex2bin()
                Console.WriteLine(BinaryString)
            Case "hex2dec"
                validate_bits()
                Console.WriteLine("Signed or unsigned? Signed will be in two's compliment")
                validate_sign()
                'validate_hex()
                hex2dec()
                Console.WriteLine(digit)
        End Select
        program()
    End Sub

    Private Sub determine_range()
        Select Case signed_or_unsigned
            Case "signed"
                min_num = (-(2 ^ (bits - 1)))
                max_num = ((2 ^ (bits - 1)) - 1)
            Case "unsigned"
                min_num = 0
                max_num = ((2 ^ bits) - 1)
        End Select
    End Sub

    Private Sub validate_sign()
        Do Until confirm = 1
            signed_or_unsigned = Console.ReadLine()
            Select Case signed_or_unsigned
                Case "signed"
                    confirm = 1
                Case "unsigned"
                    confirm = 1
            End Select
        Loop
        confirm = 0
    End Sub

    Private Sub validate_digit()
        Console.WriteLine("Enter decimal number: ")
        Do Until confirm = 1
            digit = Console.ReadLine()
            If digit < min_num Or digit > max_num Then
                Console.WriteLine("Invalid data! Not within range!")
            End If
            If (digit - Int(digit)) <> 0 Then
                Console.WriteLine("Invalid data! Digit must be an integer, not a float!")
            End If
            If digit < min_num Or digit > max_num Or (digit - Int(digit)) <> 0 Then
                Console.WriteLine("Try again:")
            End If
            If signed_or_unsigned = "signed" Then
                If digit < (max_num + 1) And digit > (min_num - 1) Then
                    If digit < 0 Then
                        oor = (digit + (2 ^ (bits - 1)))
                        digit = ((2 ^ (bits - 1)) + oor)
                        oor = 0
                    End If
                    confirm = 1
                End If
            End If
            If signed_or_unsigned = "unsigned" Then
                If digit < (max_num + 1) And digit > -1 Then
                    confirm = 1
                End If
            End If
        Loop
        confirm = 0
    End Sub

    Private Sub validate_BinaryString()
        Console.WriteLine("Enter binary string: ")
        Do Until confirm = 1
            BinaryString = Console.ReadLine()
            For pos = BinaryString.Length To 1 Step -1
                If Mid(BinaryString, pos, 1) <> "1" And Mid(BinaryString, pos, 1) <> "0" Then
                    oor = 1
                End If
            Next
            If BinaryString.Length > bits Or BinaryString = "" Or oor = 1 Then
                If BinaryString.Length > bits Then
                    Console.WriteLine("Invalid data! Bit length out of range!")
                End If
                If BinaryString = "" Then
                    Console.WriteLine("Invalid data! BinaryString cannot be empty!")
                End If
                If oor = 1 Then
                    Console.WriteLine("Invalid data! Some of the characters are not 1's and 0's!")
                End If
            End If
            If confirm <> 1 And oor <> 1 Then
                Console.WriteLine("Try again:")
            End If
            If BinaryString.Length < (bits + 1) And oor <> 1 And BinaryString <> "" Then
                confirm = 1
            End If
            oor = 0
        Loop
        confirm = 0
    End Sub

    Private Sub validate_bits()
        Console.WriteLine("Address space (i.e. max amount of bits):")
        Do Until confirm = 1
            bits = Console.ReadLine()
            If bits < 1 Or (digit - Int(digit)) <> 0 Then
                If bits < 1 Then
                    Console.WriteLine("Invalid data! Bit length cannot be 0 or negative!")
                End If
                If (digit - Int(digit)) <> 0 Then
                    Console.WriteLine("Invalid data! Bit length cannot be a float! It *must* be an integer!")
                End If
                Console.WriteLine("Try again:")
            End If
            If bits > 0 Then
                confirm = 1
            End If
        Loop
        confirm = 0
    End Sub

    Private Sub validate_hex()
        Console.WriteLine("Enter hexadecimal string:")
        Do Until confirm = 1
            hex2bin()
            bin2dec()
            If digit < min_num Or digit > max_num Then
                Console.WriteLine("Invalid data! Not within range!")
            Else
                confirm = 1
            End If
        Loop
        confirm = 0
    End Sub

    Private Sub bin2dec()
        For pos = BinaryString.Length To 1 Step -1
            If Mid(BinaryString, pos, 1) = "1" Then
                digit = digit + (2 ^ (BinaryString.Length - pos))
            End If
        Next
        If signed_or_unsigned = "signed" And BinaryString.Length = bits Then
            If Mid(BinaryString, 1, 1) = "1" Then
                digit = digit - (2 ^ bits)
            End If
        End If
    End Sub

    Private Sub dec2bin()
        Do Until digit = 0
            remain = (digit / 2) - Int(digit / 2)
            If remain > 0 Then
                binr = binr + "1"
            Else
                binr = binr + "0"
            End If
            digit = (digit / 2) - remain
        Loop
        For pos = binr.Length To 1 Step -1
            BinaryString = BinaryString + Mid(binr, pos, 1)
        Next
    End Sub

    Private Sub dec2hex()
        dec2bin()
        binr = ""
        oor = 0
        bin2hex()
    End Sub

    Private Sub bin2hex()
        If BinaryString.Length <> bits Then
            oor = bits - BinaryString.Length
            For pos = 1 To oor Step 1
                binr = binr + "0"
            Next
            oor = 0
            BinaryString = binr + BinaryString
        End If
        oor = 0
        binr = ""
        For pos = 1 To BinaryString.Length Step 4
            Select Case Mid(BinaryString, pos, 4)
                Case "0000"
                    HexString = HexString + "0"
                Case "0001"
                    HexString = HexString + "1"
                Case "0010"
                    HexString = HexString + "2"
                Case "0011"
                    HexString = HexString + "3"
                Case "0100"
                    HexString = HexString + "4"
                Case "0101"
                    HexString = HexString + "5"
                Case "0110"
                    HexString = HexString + "6"
                Case "0111"
                    HexString = HexString + "7"
                Case "1000"
                    HexString = HexString + "8"
                Case "1001"
                    HexString = HexString + "9"
                Case "1010"
                    HexString = HexString + "A"
                Case "1011"
                    HexString = HexString + "B"
                Case "1100"
                    HexString = HexString + "C"
                Case "1101"
                    HexString = HexString + "D"
                Case "1110"
                    HexString = HexString + "E"
                Case "1111"
                    HexString = HexString + "F"
            End Select
        Next
    End Sub

    Private Sub hex2bin()
        For pos = 1 To HexString.Length Step 1
            Select Case Mid(HexString, pos, 1)
                Case "0"
                    BinaryString = BinaryString + "0000"
                Case "1"
                    BinaryString = BinaryString + "0001"
                Case "2"
                    BinaryString = BinaryString + "0010"
                Case "3"
                    BinaryString = BinaryString + "0011"
                Case "4"
                    BinaryString = BinaryString + "0100"
                Case "5"
                    BinaryString = BinaryString + "0101"
                Case "6"
                    BinaryString = BinaryString + "0110"
                Case "7"
                    BinaryString = BinaryString + "0111"
                Case "8"
                    BinaryString = BinaryString + "1000"
                Case "9"
                    BinaryString = BinaryString + "1001"
                Case "A" Or "a"
                    BinaryString = BinaryString + "1010"
                Case "B" Or "b"
                    BinaryString = BinaryString + "1011"
                Case "C" Or "c"
                    BinaryString = BinaryString + "1100"
                Case "D" Or "d"
                    BinaryString = BinaryString + "1101"
                Case "E" Or "e"
                    BinaryString = BinaryString + "1110"
                Case "F" Or "f"
                    BinaryString = BinaryString + "1111"
            End Select
        Next
    End Sub

    Private Sub hex2dec()
        hex2bin()
        bin2dec()
    End Sub

    Private Sub display_options()
        Console.WriteLine("dec2bin - Decimal to Binary Conversion")
        Console.WriteLine("dec2hex - Decimal to hexadecimal conversion (finished but untested)")
        Console.WriteLine("bin2dec - Binary to Decimal conversion")
        Console.WriteLine("bin2hex - Binary to Hexadecimal conversion (finished but untested)")
        Console.WriteLine("hex2dec - hexadecimal to decimal conversion (currently not started)")
        Console.WriteLine("hex2bin - hexadecimal to binary conversion (currently not started)")
        Console.WriteLine("q - exit this program")
    End Sub

End Module
I've been playing around a little bit with recursion, and I realize that I can actually use recursion to clean up my code quit a lot (especially on the "until confirm = 1" loops in my validation checks).
gllt
NO VOWELS >:[
Posts: 753
Joined: Sun Aug 31, 2008 12:59 pm
Location: ALABAMA
Contact:

Post by gllt »

You're smarter than me.
Stop it.
adventure_of_link
Locksmith of Hyrule
Posts: 3634
Joined: Sun Aug 08, 2004 7:49 am
Location: 255.255.255.255
Contact:

Post by adventure_of_link »

gllt wrote:You're smarter than me.
Stop it.
don't listen to him you're doing a good job man

and btw, the thread title.. Frankies... shouldn't it be Franky's? oh well, fixed.
<Nach> so why don't the two of you get your own room and leave us alone with this stupidity of yours?
NSRT here.
ZH/Franky

Post by ZH/Franky »

adventure_of_link wrote:
gllt wrote:You're smarter than me.
Stop it.
don't listen to him you're doing a good job man

and btw, the thread title.. Frankies... shouldn't it be Franky's? oh well, fixed.
Heh, thanks. Well, I've gotten rid of the hex2dec and hex2bin parts of zbinc for now (they didn't work, and even if I made them work, the code would be a mess; there'd be hackery gallore), and focused on tidying up everything else:

Code: Select all


Module Module1

    Private BinaryString, binr, choice, signed_or_unsigned, HexString As String
    Private digit, remain, negative, oor, bits, disable_welcome_text, min_num, max_num As Double

    Sub Main()
        display_options()
        reload()
    End Sub

    Private Sub reload()
        Console.WriteLine("(for a list of options, type n)")
        Console.WriteLine("What do you want to do?:")
        disable_welcome_text = 1
        choice = Console.ReadLine()
        Select Case choice
            Case "q"
                End
            Case "n"
                display_options()
            Case "dec2bin"
                validate()
                dec2bin()
                Console.WriteLine(BinaryString)
                blank()
            Case "dec2hex"
                validate()
                dec2hex()
                Console.WriteLine(HexString)
                blank()
            Case "bin2dec"
                validate()
                bin2dec()
                Console.WriteLine(digit)
                blank()
            Case "bin2hex"
                validate()
                bin2hex()
                Console.WriteLine(HexString)
                blank()
        End Select
        reload()
    End Sub

    Private Sub blank()
        HexString = ""
        BinaryString = ""
        binr = ""
        digit = 0
        oor = 0
    End Sub

    Private Sub validate()
        validate_bits()
        validate_sign()
        determine_range()
        If choice = "dec2bin" Or choice = "dec2hex" Then
            validate_digit()
        Else
            validate_BinaryString()
        End If
    End Sub

    Private Sub determine_range()
        If signed_or_unsigned = "signed" Then
            min_num = (-(2 ^ (bits - 1)))
            max_num = ((2 ^ (bits - 1)) - 1)
        ElseIf signed_or_unsigned = "unsigned" Then
            min_num = 0
            max_num = ((2 ^ bits) - 1)
        End If
    End Sub

    Private Sub validate_sign()
        Console.WriteLine("Signed or unsigned? (Signed will be two's compliment)")
        signed_or_unsigned = Console.ReadLine()
        If signed_or_unsigned <> "signed" And signed_or_unsigned <> "unsigned" Then
            Console.WriteLine("Invalid input!")
            validate_sign()
        End If
    End Sub

    Private Sub validate_digit()
        Console.WriteLine("Enter decimal number: ")
        digit = Console.ReadLine()
        If digit < min_num Or digit > max_num Or (digit - Int(digit)) <> 0 Then
            Console.WriteLine("Invalid input!")
            validate_digit()
        End If
        If signed_or_unsigned = "signed" And digit < 0 Then
            oor = (digit + (2 ^ (bits - 1)))
            digit = ((2 ^ (bits - 1)) + oor)
        End If
    End Sub

    Private Sub validate_BinaryString()
        Console.WriteLine("Enter binary string: ")
        BinaryString = Console.ReadLine()
        For pos = BinaryString.Length To 1 Step -1
            If Mid(BinaryString, pos, 1) <> "1" And Mid(BinaryString, pos, 1) <> "0" Then
                oor = 1
            End If
        Next
        If BinaryString.Length > bits Or BinaryString = "" Or oor = 1 Then
            If BinaryString.Length > bits Then
                Console.WriteLine("Invalid data! Bit length out of range!")
            End If
            If BinaryString = "" Then
                Console.WriteLine("Invalid data! BinaryString cannot be empty!")
            ElseIf oor = 1 Then
                Console.WriteLine("Invalid data! Some of the characters are not 1's and 0's!")
                oor = 0
            End If
            validate_BinaryString()
        End If
    End Sub

    Private Sub validate_bits()
        Console.WriteLine("Address space (i.e. max amount of bits):")
        bits = Console.ReadLine()
        If bits < 1 Then
            Console.WriteLine("Invalid data! Bit length cannot be 0 or negative!")
            validate_bits()
        ElseIf (digit - Int(digit)) <> 0 Then
            Console.WriteLine("Invalid data! Bit length cannot be a float!")
            validate_bits()
        End If
    End Sub

    Private Sub dec2bin()
        Do Until digit = 0
            remain = (digit / 2) - Int(digit / 2)
            If remain > 0 Then
                binr = binr + "1"
            Else
                binr = binr + "0"
            End If
            digit = (digit / 2) - remain
        Loop
        For pos = binr.Length To 1 Step -1
            BinaryString = BinaryString + Mid(binr, pos, 1)
        Next
    End Sub

    Private Sub dec2hex()
        dec2bin()
        bin2hex()
    End Sub

    Private Sub bin2dec()
        For pos = BinaryString.Length To 1 Step -1
            If Mid(BinaryString, pos, 1) = "1" Then
                digit = digit + (2 ^ (BinaryString.Length - pos))
            End If
        Next
        If signed_or_unsigned = "signed" And BinaryString.Length = bits Then
            If Mid(BinaryString, 1, 1) = "1" Then
                digit = digit - (2 ^ bits)
            End If
        End If
    End Sub

    Private Sub bin2hex()
        If BinaryString.Length <> bits Then
            oor = bits - BinaryString.Length
            For pos = 1 To oor Step 1
                binr = binr + "0"
            Next
            BinaryString = binr + BinaryString
        End If
        For pos = 1 To BinaryString.Length Step 4
            Select Case Mid(BinaryString, pos, 4)
                Case "0000"
                    HexString = HexString + "0"
                Case "0001"
                    HexString = HexString + "1"
                Case "0010"
                    HexString = HexString + "2"
                Case "0011"
                    HexString = HexString + "3"
                Case "0100"
                    HexString = HexString + "4"
                Case "0101"
                    HexString = HexString + "5"
                Case "0110"
                    HexString = HexString + "6"
                Case "0111"
                    HexString = HexString + "7"
                Case "1000"
                    HexString = HexString + "8"
                Case "1001"
                    HexString = HexString + "9"
                Case "1010"
                    HexString = HexString + "A"
                Case "1011"
                    HexString = HexString + "B"
                Case "1100"
                    HexString = HexString + "C"
                Case "1101"
                    HexString = HexString + "D"
                Case "1110"
                    HexString = HexString + "E"
                Case "1111"
                    HexString = HexString + "F"
            End Select
        Next
    End Sub

    Private Sub display_options()
        Console.WriteLine("dec2bin - Decimal to Binary Conversion")
        Console.WriteLine("dec2hex - Decimal to Hexadecimal conversion")
        Console.WriteLine("bin2dec - Binary to Decimal conversion")
        Console.WriteLine("bin2hex - Binary to Hexadecimal conversion")
        Console.WriteLine("q - exit this program")
    End Sub

End Module
Today, I have learned one important thing: recursion is the way to go.

I'm going to try again at implementing hex2dec and hex2bin now.
Last edited by ZH/Franky on Fri Oct 24, 2008 7:04 pm, edited 1 time in total.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

Franky wrote:recursion is the way to go.
When it's done properly, where applicable.

Sometimes there's no way to properly do it, and sometimes even if there is a very good way, the straight mapped design is faster and lighter on resources.

Remember kids,

Code: Select all

brain::use();
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
ZH/Franky

Post by ZH/Franky »

grinvader wrote:
Franky wrote:recursion is the way to go.
When it's done properly, where applicable.

Sometimes there's no way to properly do it, and sometimes even if there is a very good way, the straight mapped design is faster and lighter on resources.

Remember kids,

Code: Select all

brain::use();
Oh, ok then. So have I used recursion properly?

Well, so what you're saying is that sometimes, doing certain things to make the source code "cleaner" makes the actual compiled program run slower at times? (for example, when as you say, recursion is used incorrectly).
gllt
NO VOWELS >:[
Posts: 753
Joined: Sun Aug 31, 2008 12:59 pm
Location: ALABAMA
Contact:

Post by gllt »

adventure_of_link wrote:
gllt wrote:You're smarter than me.
Stop it.
don't listen to him you're doing a good job man

and btw, the thread title.. Frankies... shouldn't it be Franky's? oh well, fixed.
IT WASN'T MEANT IN A BAD WAY

I respect Franky <3
ZH/Franky

Post by ZH/Franky »

gllt wrote:
adventure_of_link wrote:
gllt wrote:You're smarter than me.
Stop it.
don't listen to him you're doing a good job man

and btw, the thread title.. Frankies... shouldn't it be Franky's? oh well, fixed.
IT WASN'T MEANT IN A BAD WAY

I respect Franky <3
Hey don't worry about it. I had a feeling you were joking around ( and even if you were being serious, I wouldn't mind anyway; I'd just carry on as usual ;) ).
Last edited by ZH/Franky on Fri Oct 24, 2008 10:45 pm, edited 1 time in total.
ZH/Franky

Post by ZH/Franky »

Image
So I've just been messing around. I implemented some password protection just for fun; pretty easy, just a few lines.

Anyway, I was just wondering if anyone knows how to hide what the user types in. So for example, if the user tried the password "monkey", as they're typing it it would only show "******".

Here's the code I have so far:

Code: Select all


Module Module1

    Private BinaryString, binr, choice, signed_or_unsigned, HexString, PasswordString As String
    Private digit, remain, negative, oor, bits, min_num, max_num As Double

    Sub Main()
        password()
        Console.WriteLine("Correct!")
        Console.WriteLine()
        display_options()
        reload()
    End Sub

    Private Sub reload()
        Console.WriteLine("(for a list of options, type n)")
        Console.WriteLine("What do you want to do?:")
        choice = Console.ReadLine()
        Select Case choice
            Case "q"
                End
            Case "n"
                display_options()
            Case "r"
                Main()
            Case "dec2bin"
                validate()
                dec2bin()
                Console.WriteLine(BinaryString)
                blank()
            Case "dec2hex"
                validate()
                dec2hex()
                Console.WriteLine(HexString)
                blank()
            Case "bin2dec"
                validate()
                bin2dec()
                Console.WriteLine(digit)
                blank()
            Case "bin2hex"
                validate()
                bin2hex()
                Console.WriteLine(HexString)
                blank()
        End Select
        reload()
    End Sub

    Private Sub password()
        Console.WriteLine("Enter password:")
        PasswordString = Console.ReadLine()
        If PasswordString <> "And she's fuck ugly" Then
            Console.WriteLine("Incorrect!")
            password()
        End If
    End Sub

    Private Sub blank()
        HexString = ""
        BinaryString = ""
        binr = ""
        digit = 0
        oor = 0
    End Sub

    Private Sub validate()
        validate_bits()
        validate_sign()
        determine_range()
        If choice = "dec2bin" Or choice = "dec2hex" Then
            validate_digit()
        Else
            validate_BinaryString()
        End If
    End Sub

    Private Sub determine_range()
        If signed_or_unsigned = "signed" Then
            min_num = (-(2 ^ (bits - 1)))
            max_num = ((2 ^ (bits - 1)) - 1)
        ElseIf signed_or_unsigned = "unsigned" Then
            min_num = 0
            max_num = ((2 ^ bits) - 1)
        End If
    End Sub

    Private Sub validate_sign()
        Console.WriteLine("Signed or unsigned? (Signed will be two's compliment)")
        signed_or_unsigned = Console.ReadLine()
        If signed_or_unsigned <> "signed" And signed_or_unsigned <> "unsigned" Then
            Console.WriteLine("Invalid input!")
            validate_sign()
        End If
    End Sub

    Private Sub validate_digit()
        Console.WriteLine("Enter decimal number: ")
        digit = Console.ReadLine()
        If digit < min_num Or digit > max_num Or (digit - Int(digit)) <> 0 Then
            Console.WriteLine("Invalid input!")
            validate_digit()
        End If
        If signed_or_unsigned = "signed" And digit < 0 Then
            oor = (digit + (2 ^ (bits - 1)))
            digit = ((2 ^ (bits - 1)) + oor)
        End If
    End Sub

    Private Sub validate_BinaryString()
        Console.WriteLine("Enter binary string: ")
        BinaryString = Console.ReadLine()
        For pos = BinaryString.Length To 1 Step -1
            If Mid(BinaryString, pos, 1) <> "1" And Mid(BinaryString, pos, 1) <> "0" Then
                oor = 1
            End If
        Next
        If BinaryString.Length > bits Or BinaryString = "" Or oor = 1 Then
            If BinaryString.Length > bits Then
                Console.WriteLine("Invalid data! Bit length out of range!")
            End If
            If BinaryString = "" Then
                Console.WriteLine("Invalid data! BinaryString cannot be empty!")
            ElseIf oor = 1 Then
                Console.WriteLine("Invalid data! Some of the characters are not 1's and 0's!")
                oor = 0
            End If
            validate_BinaryString()
        End If
    End Sub

    Private Sub validate_bits()
        Console.WriteLine("Address space (i.e. max amount of bits):")
        bits = Console.ReadLine()
        If bits = Nothing Then
            Console.WriteLine("Invalid input! Must be an integer, not a string!")
        End If
        If bits < 1 Then
            Console.WriteLine("Invalid data! Bit length cannot be 0 or negative!")
            validate_bits()
        ElseIf (digit - Int(digit)) <> 0 Then
            Console.WriteLine("Invalid data! Bit length cannot be a float!")
            validate_bits()
        End If
    End Sub

    Private Sub dec2bin()
        Do Until digit = 0
            remain = (digit / 2) - Int(digit / 2)
            If remain > 0 Then
                binr = binr + "1"
            Else
                binr = binr + "0"
            End If
            digit = (digit / 2) - remain
        Loop
        For pos = binr.Length To 1 Step -1
            BinaryString = BinaryString + Mid(binr, pos, 1)
        Next
    End Sub

    Private Sub dec2hex()
        dec2bin()
        bin2hex()
    End Sub

    Private Sub bin2dec()
        For pos = BinaryString.Length To 1 Step -1
            If Mid(BinaryString, pos, 1) = "1" Then
                digit = digit + (2 ^ (BinaryString.Length - pos))
            End If
        Next
        If signed_or_unsigned = "signed" And BinaryString.Length = bits Then
            If Mid(BinaryString, 1, 1) = "1" Then
                digit = digit - (2 ^ bits)
            End If
        End If
    End Sub

    Private Sub bin2hex()
        If BinaryString.Length <> bits Then
            oor = bits - BinaryString.Length
            For pos = 1 To oor Step 1
                binr = binr + "0"
            Next
            BinaryString = binr + BinaryString
        End If
        For pos = 1 To BinaryString.Length Step 4
            Select Case Mid(BinaryString, pos, 4)
                Case "0000"
                    HexString = HexString + "0"
                Case "0001"
                    HexString = HexString + "1"
                Case "0010"
                    HexString = HexString + "2"
                Case "0011"
                    HexString = HexString + "3"
                Case "0100"
                    HexString = HexString + "4"
                Case "0101"
                    HexString = HexString + "5"
                Case "0110"
                    HexString = HexString + "6"
                Case "0111"
                    HexString = HexString + "7"
                Case "1000"
                    HexString = HexString + "8"
                Case "1001"
                    HexString = HexString + "9"
                Case "1010"
                    HexString = HexString + "A"
                Case "1011"
                    HexString = HexString + "B"
                Case "1100"
                    HexString = HexString + "C"
                Case "1101"
                    HexString = HexString + "D"
                Case "1110"
                    HexString = HexString + "E"
                Case "1111"
                    HexString = HexString + "F"
            End Select
        Next
    End Sub

    Private Sub display_options()
        Console.WriteLine("dec2bin - Decimal to Binary Conversion")
        Console.WriteLine("dec2hex - Decimal to Hexadecimal conversion")
        Console.WriteLine("bin2dec - Binary to Decimal conversion")
        Console.WriteLine("bin2hex - Binary to Hexadecimal conversion")
        Console.WriteLine("r - return to password screen")
        Console.WriteLine("q - exit this program")
    End Sub

End Module
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

you do realize there are functions that convert strings to numbers and vice versa right?
Last edited by funkyass on Fri Oct 24, 2008 11:25 pm, edited 2 times in total.
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
ZH/Franky

Post by ZH/Franky »

funkyass wrote:you do realize there are functions that convert strings to numbers and vice versa right?
What do you mean?
creaothceann
Seen it all
Posts: 2302
Joined: Mon Jan 03, 2005 5:04 pm
Location: Germany
Contact:

Post by creaothceann »

Most languages already have functions for converting between number systems.
Franky wrote:Anyway, I was just wondering if anyone knows how to hide what the user types in. So for example, if the user tried the password "monkey", as they're typing it it would only show "******".
Most languages have functions that return a key from the keyboard buffer.
Use that to build your own string input function.

Code: Select all

function Input : String;
var     c               :       Char;
begin
Result := '';
while true do begin
        c := ReadKey;
        if (c = VK_Return) then break;
        Result := Result + c;
        Write('*');
end;
end;
ReadKey would be that function in Pascal; VK_Return is just some constant assigned to the character code of the Enter/Return key, and break leaves the while loop.
Last edited by creaothceann on Sat Oct 25, 2008 8:37 am, edited 1 time in total.
vSNES | Delphi 10 BPLs
bsnes launcher with recent files list
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

Franky wrote:
funkyass wrote:you do realize there are functions that convert strings to numbers and vice versa right?
What do you mean?
exactly what I said.

Also, the methodology of converting from binary to decimal is the same for binary to hexadecimal.
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
ZH/Franky

Post by ZH/Franky »

Well creaothceann, thanks for your tips. I'll look into this a bit more then (all I really needed was to know what I'm looking for, so thank you for your help).
"Keyboard buffer". I'll have to read up about that then.

But >_<:
If there're built-in vb functions for converting between the numeral systems, then I've wasted my time. >_<
Still, it's always good to see if you can do things yourself. I'm also extremely stubborn, so regardless, I'll probably still try to finish my program (that is, add hex2dec and hex2bin).
Also, the methodology of converting from binary to decimal is the same for binary to hexadecimal.
Really? Could you explain this a bit more for me?
When I go bin to hex, I look from right to left, grouping all the bits in fours, and convert all those groups of fours (i.e. 4-bit values) to hex).
So like, 11101010
1110 1010
E A
EA
obviously, I do the reverse of this when going from hex to binary; when doing hex to decimal, I do this and then my binary to decimal method.

But when I go bin to dec, I look from right to left, in powers of 2 (starting from 0), adding those power's of two if their bits are 1, else if they're 0 I add nothing. E.g.
1110101011010
2 + 8 + 16 + 64 + 256 + 1024 + 2048 + 4096 = 7514
so binary 1110101011010 = 7514


... when I do decimal to binary, I keep dividing by two, then store and subtract the remainder, until I get to 0. E.g.
28 / 2 = 14 r 0
14 / 2 = 7 r 0
7 / 2 = 3 r 1
3 / 2 = 1 r 1
1 / 2 = 0 r 1
(the remainder will always be 0.5, but you count it as 1 (quite funny when you think about it; I think I remember hearing that transistors, in their high state, operate at a voltage level of 0.5))
and then I write down each 1 or 0 from bottom to top, in this case that would be 11100, so decimal 28 = 11100
(when going decimal to hexadecimal, I do my decimal to binary method first, and then use my binary to hexadecimal method.)


All these methods are what I've been basing my program on.
h4tred

Post by h4tred »

don't listen to him you're doing a good job man
Indeed, gilt is fine. Shader programming == UBER L33T :D
So gilt can keep coding shaders.

VB: icks
Pascal: rox
C/C++/ASM: l33t
python: cool
gllt
NO VOWELS >:[
Posts: 753
Joined: Sun Aug 31, 2008 12:59 pm
Location: ALABAMA
Contact:

Post by gllt »

h4tred wrote:
don't listen to him you're doing a good job man
Indeed, gilt is fine. Shader programming == UBER L33T :D
So gilt can keep coding shaders.

VB: icks
Pascal: rox
C/C++/ASM: l33t
python: cool
GILTY AS CHARG-ED
Rashidi
Trooper
Posts: 515
Joined: Fri Aug 18, 2006 2:45 pm

Post by Rashidi »

hex2dec?

anyway this was an (old) tricks from ancient basic

Code: Select all

A$ = "1EE7"
? VAL("&H"+A$)
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

That's already better.
Franky wrote:If there're built-in vb functions for converting between the numeral systems, then I've wasted my time. >_<
You have, you have.
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
ZH/Franky

Post by ZH/Franky »

Ok dudes, finished it ages ago now (but never posted it). Here it is anyway:
(it's a bit cleaner this time. As clean as I could make it that is)

Code: Select all

Module Module1

    Private BinaryString, choice, sign, HexString, hexr As String
    Private digit, bits, min_num, max_num, pos As Double

    Sub Main()
        display_options()
        reload()
    End Sub

    Private Sub reload()
        Do
            Console.WriteLine("(for a list of options, type n)")
            Console.Write("What do you want to do?: ")
            choice = Console.ReadLine()
            Select Case choice
                Case "q"
                    confirm()
                Case "n"
                    display_options()
                Case "dec2bin"
                    validate()
                    dec2bin()
                    Console.WriteLine(BinaryString)
                    blank()
                Case "dec2hex"
                    validate()
                    dec2hex()
                    Console.WriteLine(HexString)
                    blank()
                Case "bin2dec"
                    validate()
                    bin2dec()
                    Console.WriteLine(digit)
                    blank()
                Case "bin2hex"
                    validate()
                    bin2hex()
                    Console.WriteLine(HexString)
                    blank()
                Case "hex2dec"
                    validate()
                    hex2dec()
                    console.writeline(digit)
                    blank()
                Case "hex2bin"
                    validate()
                    hex2bin()
                    console.writeline(BinaryString)
                    blank()
            End Select
        Loop
    End Sub

    Private Sub confirm()
        Console.Write("Are you sure you'd like to quit? y/n: ")
        choice = Console.ReadLine()
        If choice <> "y" And choice <> "n" Then
            console.writeline("Invalid selection!")
        ElseIf choice = "y" Then
            End
        End If
    End Sub

    Private Sub fix_bits()
        If BinaryString.Length <> bits Then
            For pos = 1 To (bits - BinaryString.Length) Step 1
                BinaryString = "0" + BinaryString
            Next
        End If
    End Sub

    Private Sub validate()
        validate_bits()
        validate_sign()
        determine_range()
        If choice = "dec2bin" Or choice = "dec2hex" Then
            validate_digit()
        ElseIf choice = "bin2dec" Or choice = "bin2hex" Then
            validate_BinaryString()
        Else
            validate_HexString()
        End If
    End Sub

    Private Sub blank()
        HexString = ""
        hexr = ""
        BinaryString = ""
        digit = 0
    End Sub

    Private Sub determine_range()
        If sign = "signed" Then
            min_num = (-(2 ^ (bits - 1)))
            max_num = ((2 ^ (bits - 1)) - 1)
        Else
            min_num = 0
            max_num = ((2 ^ bits) - 1)
        End If
    End Sub

    Private Sub validate_sign()
        Console.Write("Signed or unsigned? (Signed will be two's compliment): ")
        sign = Console.ReadLine()
        If sign <> "signed" And sign <> "unsigned" Then
            Console.WriteLine("Invalid input!")
            validate_sign()
        End If
    End Sub

    Private Sub validate_HexString()
        Console.Write("Enter hexadecimal string (letters A-F *must* be uppercase): ")
        HexString = Console.ReadLine()
        For pos = 1 To HexString.Length Step 1
            hexr = Mid(HexString, pos, 1)
            If hexr <> "0" And hexr <> "1" And hexr <> "2" And hexr <> "3" And hexr <> "4" And hexr <> "5" And hexr <> "6" And hexr <> "7" And hexr <> "8" And hexr <> "9" And hexr <> "A" And hexr <> "B" And hexr <> "C" And hexr <> "D" And hexr <> "E" And hexr <> "F" Then
                Console.WriteLine("Invalid input! Funky hex!")
                validate_HexString()
            ElseIf (HexString.Length * 4) > bits Then
                hex2bin()
                If BinaryString.Length > bits Then
                    Console.WriteLine("Invalid input! HexString *must* be in range!")
                    BinaryString = ""
                    validate_HexString()
                End If
                BinaryString = ""
            End If
        Next
    End Sub

    Private Sub validate_digit()
        Console.Write("Enter decimal number: ")
        digit = Console.ReadLine()
        If digit < min_num Or digit > max_num Or (digit - Int(digit)) <> 0 Then
            Console.WriteLine("Invalid input! Must be an integer within range!")
            validate_digit()
        End If
        If sign = "signed" And digit < 0 Then
            digit = ((2 * (2 ^ (bits - 1))) + digit)
        End If
    End Sub

    Private Sub validate_BinaryString()
        Console.Write("Enter binary string: ")
        BinaryString = Console.ReadLine()
        For pos = BinaryString.Length To 1 Step -1
            If Mid(BinaryString, pos, 1) <> "1" And Mid(BinaryString, pos, 1) <> "0" Then
                Console.WriteLine("Invalid input! Funky bits!")
                validate_BinaryString()
            ElseIf BinaryString.Length > bits Then
                Console.WriteLine("Invalid data! Bit length out of range!")
                validate_BinaryString()
            ElseIf BinaryString = "" Then
                Console.WriteLine("Invalid data! BinaryString cannot be empty!")
                validate_BinaryString()
            End If
        Next
    End Sub

    Private Sub validate_bits()
        Console.Write("Address space (i.e. max amount of bits): ")
        bits = Console.ReadLine()
        If bits < 1 Then
            Console.WriteLine("Invalid data! Bit length cannot be 0 or negative!")
            validate_bits()
        ElseIf (digit - (Int(digit))) <> 0 Then
            Console.WriteLine("Invalid data! Bit length cannot be a float!")
            validate_bits()
        End If
    End Sub

    Private Sub dec2bin()
        Do While digit <> 0
            If ((digit / 2) - (Int(digit / 2))) <> 0 Then
                BinaryString = "1" + BinaryString
            Else
                BinaryString = "0" + BinaryString
            End If
            digit = (Int(digit / 2))
        Loop
        fix_bits()
    End Sub

    Private Sub dec2hex()
        dec2bin()
        bin2hex()
    End Sub

    Private Sub bin2dec()
        For pos = BinaryString.Length To 1 Step -1
            If Mid(BinaryString, pos, 1) = "1" Then
                digit = ((2 ^ (BinaryString.Length - pos)) + digit)
            End If
        Next
        If sign = "signed" And BinaryString.Length = bits And (Mid(BinaryString, 1, 1)) = "1" Then
            digit = (digit - (2 ^ bits))
        End If
    End Sub

    Private Sub bin2hex()
        fix_bits()
        For pos = 1 To BinaryString.Length Step 4
            Select Case Mid(BinaryString, pos, 4)
                Case "0000"
                    HexString = HexString + "0"
                Case "0001"
                    HexString = HexString + "1"
                Case "0010"
                    HexString = HexString + "2"
                Case "0011"
                    HexString = HexString + "3"
                Case "0100"
                    HexString = HexString + "4"
                Case "0101"
                    HexString = HexString + "5"
                Case "0110"
                    HexString = HexString + "6"
                Case "0111"
                    HexString = HexString + "7"
                Case "1000"
                    HexString = HexString + "8"
                Case "1001"
                    HexString = HexString + "9"
                Case "1010"
                    HexString = HexString + "A"
                Case "1011"
                    HexString = HexString + "B"
                Case "1100"
                    HexString = HexString + "C"
                Case "1101"
                    HexString = HexString + "D"
                Case "1110"
                    HexString = HexString + "E"
                Case "1111"
                    HexString = HexString + "F"
            End Select
        Next
    End Sub

    Private Sub hex2dec()
        hex2bin()
        bin2dec()
    End Sub

    Private Sub hex2bin()
        For pos = 1 To HexString.Length Step 1
            Select Case Mid(HexString, pos, 1)
                Case "0"
                    BinaryString = BinaryString + "0000"
                Case "1"
                    BinaryString = BinaryString + "0001"
                Case "2"
                    BinaryString = BinaryString + "0010"
                Case "3"
                    BinaryString = BinaryString + "0011"
                Case "4"
                    BinaryString = BinaryString + "0100"
                Case "5"
                    BinaryString = BinaryString + "0101"
                Case "6"
                    BinaryString = BinaryString + "0110"
                Case "7"
                    BinaryString = BinaryString + "0111"
                Case "8"
                    BinaryString = BinaryString + "1000"
                Case "9"
                    BinaryString = BinaryString + "1001"
                Case "A"
                    BinaryString = BinaryString + "1010"
                Case "B"
                    BinaryString = BinaryString + "1011"
                Case "C"
                    BinaryString = BinaryString + "1100"
                Case "D"
                    BinaryString = BinaryString + "1101"
                Case "E"
                    BinaryString = BinaryString + "1110"
                Case "F"
                    BinaryString = BinaryString + "1111"
            End Select
        Next
    End Sub

    Private Sub display_options()
        Console.WriteLine("dec2bin - Decimal to Binary Conversion")
        Console.WriteLine("dec2hex - Decimal to Hexadecimal conversion")
        Console.WriteLine("bin2dec - Binary to Decimal conversion")
        Console.WriteLine("bin2hex - Binary to Hexadecimal conversion")
        Console.WriteLine("hex2dec - Hexadecimal to decimal conversion")
        Console.WriteLine("hex2bin - Hexadecimal to binary conversion")
        Console.WriteLine("q - exit this program")
    End Sub

End Module
I've been doing file input/output. Wrote a program that searches for a particular string of text in a file, and writes all lines containing that text to one file, and all lines without that peice of text to another file. Will post it soon. EDIT: Here it is:

Code: Select all

Imports System.IO

Module Module1

    private TextString,inputFile,outputFile1,outputFile2,SearchItem as string
    private FileWriter1,FileWriter2 as StreamWriter
    private fileReader As StreamReader

    Sub Main()
            search_in_stuff()
    End Sub

    Private Sub search_in_file()
        console.writeline("Give me a text file (full path included) for me to search in, dude:")
        inputFile = console.readline()
        console.writeline("Alright, what do you want to look for in this file?")
        SearchItem = console.readline()
        console.writeline("I will write any line with your SearchItem to a file. Specify this file (full path, too):")
        outputFile1 = console.readline()
        console.writeline("Just for kicks, I'll write all lines without SearchItem into another file. SPECIFY!! ")
        outputFile2 = console.readline()
        fileReader = New StreamReader(inputFile)
        FileWriter1 = New StreamWriter(outputFile1)
        FileWriter2 = New StreamWriter(outputFile2)
        Do Until fileReader.EndOfStream
            textstring = fileReader.ReadLine()
            If InStr(textstring, SearchItem) > 0 Then
                FileWriter1.WriteLine(textstring)
            Else
                FileWriter2.WriteLine(textstring)
            End If
        Loop
        fileReader.Close()
        FileWriter1.Close()
        FileWriter2.Close()
        Console.WriteLine("Ok, look in the files you specified to see what I found.")
        Console.Write("Press enter to exit this program...")
        Console.ReadLine()
        End
    End Sub

End Module
I need to get some validation checks in there, but it's fine for now.

I want to do a file searching program too. Will write it soon.
h4tred

Post by h4tred »

Why not write a module that searches and replaces bytes in files? I know some people who will find such code very handy.......
SquareHead
Veteran
Posts: 970
Joined: Fri Jan 21, 2005 11:15 am
Location: Montana, United States

Post by SquareHead »

h4tred wrote:Why not write a module that searches and replaces bytes in files? I know some people who will find such code very handy.......
Did that in qbasic with a hex to dec and dec to hex routine. Evil, very evil.

As I have halted my learning in cpp for a short while, I am at a total loss, but does c or cpp have an equivalent to the seek parameter in qbasic's binary file access?
ZH/Franky

Post by ZH/Franky »

h4tred wrote:Why not write a module that searches and replaces bytes in files? I know some people who will find such code very handy.......
I don't know how to do that the moment.
Nach
ZSNES Developer
ZSNES Developer
Posts: 3904
Joined: Tue Jul 27, 2004 10:54 pm
Location: Solar powered park bench
Contact:

Post by Nach »

SquareHead wrote: As I have halted my learning in cpp for a short while, I am at a total loss, but does c or cpp have an equivalent to the seek parameter in qbasic's binary file access?
C: fseek()
C++: ifstream::seekg()/ofstream::seekp()
UNIX: lseek() (_lseek() in MS's dumb lets _ prefix everything implementation)

Modern UNIX also has pread() and pwrite() for atomic seeking and then reading or writing (and return of the file pointer to where it was). Microsoft however lacks these as they're not in the minimally required section of the POSIX standard.

Now if you wanted to seek to a character in a file, not at all hard to make your own function to do it.

Just load up a chunk of a file, call memchr(), if found, good, otherwise load up the next chunk and do it again.

Easy to turn that into a search and replace too.

BTW Mudlord, most good hex editors contain search and replace of bytes.
May 9 2007 - NSRT 3.4, now with lots of hashing and even more accurate information! Go download it.
_____________
Insane Coding
h4tred

Post by h4tred »

BTW Mudlord, most good hex editors contain search and replace of bytes.
I know, but I was referring for a way to do it pragmatically in C#/Visual Basic.NET. As in, you open a executable for binary read/update, scan for a particular byte pattern, and replace it with a different one. Should be quite obvious why such code is useful to some people....... :)
Easy to turn that into a search and replace too.
Yep, there is also code around to scan memory for bytes and replacing in real time. Also I have some assembly code for binary search and replace. Really neat stuff :).
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

Err... yeah. 10 minutes and less than 50 lines do that.
Double it for idiot-proofing, if needed.

Really neat ? At one point it gets too easy to be worth that praise, no ? ¬_¬
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
Post Reply