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 »

Ah, well thank you for explaining this to me guys.
So 8-bit unsigned can be 0 to 255, and
8-bit signed by be -128 to +127.
I understand now.

So if I wanted to display 255 (11111111), I'd have to specify that it is unsigned; but I couldn't display 255 as a signed integer, right?

So as signed, I can display up to 127 (01111111), and down to -128 (10000000), right?
creaothceann
Seen it all
Posts: 2302
Joined: Mon Jan 03, 2005 5:04 pm
Location: Germany
Contact:

Post by creaothceann »

Franky wrote:So if I wanted to display 255 (11111111), I'd have to specify that it is unsigned; but I couldn't display 255 as a signed integer, right?
"signed integer" doesn't say anything about the size of this integer! If this integer is larger than a byte then you can store 255 in it.
Franky wrote:So for signed [bytes], I can [store] up to 127 (01111111), and down to -128 (10000000), right?
Yes. See also "signed number representations" (link).
Last edited by creaothceann on Mon Oct 20, 2008 7:36 pm, edited 1 time in total.
vSNES | Delphi 10 BPLs
bsnes launcher with recent files list
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

That's why most 'modern' C types mention sign and amount of bit, like uint32_t, int16_t, and so on.
No mistake possible.
皆黙って俺について来い!!

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 »

Well, that's certainly cleared up more confusion. Thanks guys. I really do appreciate your advice.

EDIT:
Oh, I think I understand now. When you're working with signed bytes, you can store up to 127 (01111111), and down to -128 (10000000).
So, basically my question: since in the positive domain you can't go over 01111111, if there is a 1 where the 0 is, you know it's a two's compliment?
, so
blah blah blah
127... 01111111
10000000... -128
10000001... -127
10000010... -126
10000011... -125
10000100... -124
10000101... -123
10000110... -122
10000111... -121
10001000... -120
etc...
so basically, in signed bytes a "negative" byte is considered to be "higher" than a positive byte, that is --> you could up to 127, and then come to -128, counting down to 1... so basically you count "upwards" (bitwise) while count down from -128 to 0?
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

you display negative integer in binary the same essential way you do any other numeric base.

you put a negative sign in front of it.

For the sake of this conversation we will use the largest bit for the negative sign(the first one on the left).

So 7 is 0111 and negative 7 is 1111.

Ponder the dilemma this gives us.
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
AamirM
Regen Developer
Regen Developer
Posts: 533
Joined: Sun Feb 17, 2008 8:01 am
Contact:

Post by AamirM »

Hi,
funkyass wrote: So 7 is 0111 and negative 7 is 1111.

Ponder the dilemma this gives us.
IIRC, this is called the sign bit convention for negative numbers. This has an obvious problem that 0 is 0000 and 1000 is -0. Thats why someone came up with two's complement. 0 is 0000 and thats it. 1000 would be -8 (in 2' complement).

stay safe,

AamirM
creaothceann
Seen it all
Posts: 2302
Joined: Mon Jan 03, 2005 5:04 pm
Location: Germany
Contact:

Post by creaothceann »

creaothceann wrote:See also "signed number representations" (link).
vSNES | Delphi 10 BPLs
bsnes launcher with recent files list
ZH/Franky

Post by ZH/Franky »

ok, so my l33tness level went up today (during a 2 hour free period at college):
(I've added the ability to accurately find the signed (two's compliment) negative binary value of a negative decimal number. It works for any address space, too (the user specifies the maximum amount of bits, and then specifies whether s/he wants convert to a signed or unsigned binary value).

For example:
the users specifies a maximum amount of bits: 8
if they specify signed, they can enter an integer between a range of -128 and +127. If they specify unsigned, they can enter an integer between a range of 0 and 255. If they enter a number outside of the bit range they specified, the program will tell them so, and not convert anything.

Woot!

Code: Select all

Module Module1

    Private BinaryString, binr, choice, signed_or_unsigned 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()
        disable_welcome_text = 0
        Do Until quit = 1
            oor = 0
            quit = 0
            If disable_welcome_text = 0 Then
                display_options()
            End If
            Console.WriteLine("What do you want to do?:")
            If disable_welcome_text = 1 Then
                Console.WriteLine("(for a list of options, type n)")
            End If
            disable_welcome_text = 1
            choice = Console.ReadLine()
            If choice = "q" Then
                quit = 1
            End If
            If choice = "n" Then
                display_options()
            End If
            If choice = "int_function" Then
                Console.WriteLine("Enter number to display int of:")
                digit = Console.ReadLine()
                int_function()
                Console.WriteLine(digit)
            End If
            If choice = "factorials" Then
                Console.WriteLine("Enter number to find factorial of:")
                digit = Console.ReadLine()
                factorials()
                Console.WriteLine(digit)
            End If
            If choice = "bin2dec" Then
                Console.WriteLine("Enter a binary number (within an 8-bit space) to convert to decimal: ")
                BinaryString = Console.ReadLine()
                If BinaryString.Length > 8 Then
                    Console.WriteLine("Not within range!")
                    oor = 1
                End If
                digit = 0
                bin2dec()
                Console.WriteLine(digit)
            End If
            If choice = "dec2bin" Then
                Console.WriteLine("Maximum amount of bits:")
                Do Until confirm = 1
                    bits = Console.ReadLine()
                    If bits < 1 Then
                        Console.WriteLine("Invalid amount of bits! Try again:")
                    End If
                    If bits > 0 Then
                        confirm = 1
                    End If
                Loop
                confirm = 0
                Console.WriteLine("Signed or unsigned? (signed will be in two's compiment)")
                signed_or_unsigned = Console.ReadLine()
                If signed_or_unsigned = "signed" Then
                    min_num = (-(2 ^ (bits - 1)))
                    max_num = ((2 ^ (bits - 1)) - 1)
                End If
                If signed_or_unsigned = "unsigned" Then
                    max_num = ((2 ^ bits) - 1)
                End If
                Console.WriteLine("Enter decimal number: ")
                digit = Console.ReadLine()
                If signed_or_unsigned = "signed" Then
                    If digit > max_num Then
                        Console.WriteLine("Not within range!")
                        oor = 1
                    End If
                    If digit < min_num Then
                        Console.WriteLine("Not within range!")
                        oor = 1
                    End If
                    If digit < 0 Then
                        oor = (digit + (2 ^ (bits - 1)))
                        digit = ((2 ^ (bits - 1)) + oor)
                        oor = 0
                    End If
                End If
                If signed_or_unsigned = "unsigned" Then
                    If digit > ((2 ^ bits) - 1) Then
                        Console.WriteLine("Not within range!")
                        oor = 1
                    End If
                    If digit < 0 Then
                        Console.WriteLine("Not within range!")
                        oor = 1
                    End If
                End If
                If oor = 0 Then
                    BinaryString = ""
                    binr = ""
                    dec2bin()
                    Console.WriteLine(BinaryString)
                End If
                oor = 0
            End If
        Loop
    End Sub

    Private Sub dec2bin()
        Do Until digit = 0
            remain = (digit / 2) - Int(digit / 2)
            If remain > 0 Then
                binr = binr + "1"
            End If
            If remain = 0 Then
                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 int_function()
        oor = digit
        If digit > 0 Then
            Do Until digit < 1
                digit = digit - 1
            Loop
        End If
        If digit < 0 Then
            Do Until digit > -1
                digit = digit + 1
            Loop
        End If
        digit = oor - digit
        oor = 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
    End Sub

    Private Sub factorials()
        If digit > 0 Then
            For pos = digit To 2 Step -1
                digit = digit * (pos - 1)
            Next
        End If
        If digit < 0 Then
            For pos = digit To -2 Step 1
                digit = digit * (pos + 1)
            Next
            If digit > 0 Then
                digit = digit * (-1)
            End If
        End If
    End Sub

    Private Sub display_options()
        Console.WriteLine("bin2dec - Binary to Decimal conversion")
        Console.WriteLine("dec2bin - Decimal to Binary Conversion")
        Console.WriteLine("factorials - Find the factorial of a number")
        Console.WriteLine("int_function - VB has a built-in int function, but (purely for fun) I decided to write my own")
        Console.WriteLine("q - exit this program")
    End Sub

End Module
All I need to do now is add the ability to convert a negative two's compliment binary number into a negative decimal number.
ZH/Franky

Post by ZH/Franky »

Alright, might aswell post an untested (and possibly buggy) WIP that I'm working on. I'm seeing if I can implement some validation checks. I'm also getting round to making bin2dec capable of converting two's compliment binary strings to negative decimal numbers.

Code: Select all

Module Module1

    Private BinaryString, binr, choice, signed_or_unsigned As String
    Private digit, remain, quit, negative, oor, bits, disable_welcome_text, min_num, max_num, confirm, redo As Double
    'Private pos as double
    'uncomment the above if you are using Microsoft Visual Studio .NET 2005 Express

    Sub Main()
        disable_welcome_text = 0
        Do Until quit = 1
            oor = 0
            quit = 0
            If disable_welcome_text = 0 Then
                display_options()
            End If
            Console.WriteLine("What do you want to do?:")
            If disable_welcome_text = 1 Then
                Console.WriteLine("(for a list of options, type n)")
            End If
            disable_welcome_text = 1
            choice = Console.ReadLine()
            If choice = "q" Then
                quit = 1
            End If
            If choice = "n" Then
                display_options()
            End If
            If choice = "int_function" Then
                Console.WriteLine("Enter number to display int of:")
                digit = Console.ReadLine()
                int_function()
                Console.WriteLine(digit)
            End If
            If choice = "factorials" Then
                Console.WriteLine("Enter number to find factorial of:")
                digit = Console.ReadLine()
                factorials()
                Console.WriteLine(digit)
            End If
            If choice = "bin2dec" Then
                validate_bits()
                Console.WriteLine("Signed or unsigned? (when entering an unsigned binary string, do it as two's compliment)")
                validate_sign()
                validate_BinaryString()
                bin2dec()
                If signed_or_unsigned = "signed" Then
                    min_num = (-(2 ^ (bits - 1)))
                    max_num = ((2 ^ (bits - 1)) - 1)
                End If
                If signed_or_unsigned = "unsigned" Then
                    max_num = ((2 ^ bits) - 1)
                End If
            End If
            If choice = "dec2bin" Then
                validate_bits()
                Console.WriteLine("Signed or unsigned? (signed will be in two's compliment)")
                validate_sign()
                If signed_or_unsigned = "signed" Then
                    min_num = (-(2 ^ (bits - 1)))
                    max_num = ((2 ^ (bits - 1)) - 1)
                End If
                If signed_or_unsigned = "unsigned" Then
                    max_num = ((2 ^ bits) - 1)
                End If
                validate_digit()
                If oor = 0 Then
                    BinaryString = ""
                    binr = ""
                    dec2bin()
                    Console.WriteLine(BinaryString)
                End If
                oor = 0
            End If
        Loop
    End Sub

    Private Sub dec2bin()
        Do Until digit = 0
            remain = (digit / 2) - Int(digit / 2)
            If remain > 0 Then
                binr = binr + "1"
            End If
            If remain = 0 Then
                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 int_function()
        oor = digit
        If digit > 0 Then
            Do Until digit < 1
                digit = digit - 1
            Loop
        End If
        If digit < 0 Then
            Do Until digit > -1
                digit = digit + 1
            Loop
        End If
        digit = oor - digit
        oor = 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
    End Sub

    Private Sub factorials()
        If digit > 0 Then
            For pos = digit To 2 Step -1
                digit = digit * (pos - 1)
            Next
        End If
        If digit < 0 Then
            For pos = digit To -2 Step 1
                digit = digit * (pos + 1)
            Next
            If digit > 0 Then
                digit = digit * (-1)
            End If
        End If
    End Sub

    Private Sub validate_BinaryString()
        Console.WriteLine("Enter binary string:")
        Do Until confirm = 1
            BinaryString = Console.ReadLine()
            If BinaryString.Length > bits Or BinaryString.Length < 1 Then
                Console.WriteLine("Invalid! Try again:")
            End If
            If BinaryString.length < (bits + 1) And BinaryString.length > 0 Then
                confirm = 1
            End If
        Loop
        confirm = 0
        Do Until confirm = 1
            For pos = BinaryString.Length To 1 Step -1
                If Mid(BinaryString, pos, 1) <> "0" And Mid(BinaryString, pos, 1) <> "1" Then
                    oor = 1
                End If
            Next
            If oor = 1 Then
                Console.WriteLine("Invalid data! Some characters in this string are not either a 1 or a 0!")
                Console.WriteLine("Try again:")
                oor = 0
                validate_BinaryString()
            End If
            If oor <> 1 Then
                confirm = 1
            End If
        Loop
        confirm = 0
    End Sub

    Private Sub validate_digit()
        Console.WriteLine("Enter decimal number: ")
        Do Until confirm = 1
            digit = Console.ReadLine()
            If signed_or_unsigned = "signed" Then
                If digit > max_num Or digit < min_num Then
                    Console.WriteLine("Not within range! Try again:")
                End If
                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)
                    End If
                    confirm = 1
                End If
            End If
            If signed_or_unsigned = "unsigned" Then
                If digit > ((2 ^ bits) - 1) Or digit < 0 Then
                    Console.WriteLine("Not within range! Try again")
                End If
                If digit < (2 ^ bits) And digit > 0 Then
                    confirm = 1
                End If
            End If
        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 Then
                Console.WriteLine("Invalid address space! Try again:")
            End If
            If bits > 0 Then
                confirm = 1
            End If
        Loop
        confirm = 0
    End Sub

    Private Sub validate_sign()
        redo = 1
        Do Until confirm = 1
            signed_or_unsigned = Console.ReadLine()
            If signed_or_unsigned = "signed" Or signed_or_unsigned = "signed" Then
                confirm = 1
                redo = 0
            End If
            If redo = 1 Then
                Console.WriteLine("Invalid selection! Try again:")
            End If
        Loop
        confirm = 0
    End Sub

    Private Sub display_options()
        Console.WriteLine("bin2dec - Binary to Decimal conversion")
        Console.WriteLine("dec2bin - Decimal to Binary Conversion")
        Console.WriteLine("factorials - Find the factorial of a number")
        Console.WriteLine("int_function - VB has a built-in int function, but (purely for fun) I decided to write my own")
        Console.WriteLine("q - exit this program")
    End Sub

End Module
Gil_Hamilton
Buzzkill Gil
Posts: 4295
Joined: Wed Jan 12, 2005 7:14 pm

Post by Gil_Hamilton »

funkyass wrote:you display negative integer in binary the same essential way you do any other numeric base.

you put a negative sign in front of it.

For the sake of this conversation we will use the largest bit for the negative sign(the first one on the left).

So 7 is 0111 and negative 7 is 1111.

Ponder the dilemma this gives us.
I believe he's asking about the two's complement notation, not real math.


In which case, unless I'm off, he's right. All negative numbers WILL have a 1 in the farthest bit.
Though unlike sign-bit notation, 1 followed by zeros won't be a negative 0.


He's also correctly identified the end result of a rollover error in a signed integer.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

It looks like a huge lot of code for what it's supposed to do. Is it bound to the language used ?
皆黙って俺について来い!!

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
DOLLS (J) [!]
ZNES Developer
Posts: 215
Joined: Mon Aug 02, 2004 11:22 pm

Post by DOLLS (J) [!] »

grinvader wrote:It looks like a huge lot of code for what it's supposed to do. Is it bound to the language used ?
Of course not, don't act like you don't know basic D:
ZH/Franky

Post by ZH/Franky »

grinvader wrote:It looks like a huge lot of code for what it's supposed to do. Is it bound to the language used ?
Well, no it's not bound to the language; I could quit happily rewrite this in C++ if I wanted to (though I'd have to learn properly how to create my own functions first (and also how to handle strings). There was a lot of code because of two things:
-it did more than just bin2dec and dec2bin (just a few extra silly things I put there)
- the actual code for the bin2dec and dec2bin programs were severely bloated, and bug-ridden (and not to mention, with hackery gallore)

Anyway, I've cleaned it up a lot now, and implemented a lot of validation checks.

My feelings about my achievement (explained below, after the code):
MWHAHAHA!!

Code: Select all

Module Module1

    Private BinaryString, binr, choice, signed_or_unsigned 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()
        Do Until quit = 1
            quit = 0
            If disable_welcome_text <> 1 Then
                display_options()
            End If
            Console.WriteLine("What do you want to do?:")
            If disable_welcome_text = 1 Then
                Console.WriteLine("(for a list of options, type n)")
            End If
            disable_welcome_text = 1
            choice = Console.ReadLine()
            If choice = "q" Then
                quit = 1
            End If
            If choice = "n" Then
                display_options()
            End If
            If choice = "bin2dec" Then
                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
            End If
            If choice = "dec2bin" Then
                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
            End If
        Loop
    End Sub

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

    Private Sub validate_sign()
        Do Until confirm = 1
            signed_or_unsigned = Console.ReadLine()
            If signed_or_unsigned <> "signed" And signed_or_unsigned <> "unsigned" Then
                Console.WriteLine("Invalid data! Try again:")
            End If
            If signed_or_unsigned = "signed" Or signed_or_unsigned = "unsigned" Then
                confirm = 1
            End If
        Loop
        confirm = 0
    End Sub

    Private Sub validate_digit()
        Console.WriteLine("Enter decimal number: ")
        Do Until confirm = 1
            If signed_or_unsigned = "signed" Then
                digit = Console.ReadLine()
                If digit < min_num Or digit > max_num Then
                    Console.WriteLine("Invalid data! Not within range! Try again:")
                End If
                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
                digit = Console.ReadLine()
                If digit < 0 Or digit > max_num Then
                    Console.WriteLine("Invalid data! Not within range! Try again:")
                End If
                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 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 Then
                Console.WriteLine("Invalid data! Bit length cannot be 0 or negative! Try again:")
            End If
            If bits > 0 Then
                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"
            End If
            If remain = 0 Then
                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 display_options()
        Console.WriteLine("bin2dec - Binary to Decimal conversion")
        Console.WriteLine("dec2bin - Decimal to Binary Conversion")
        Console.WriteLine("q - exit this program")
    End Sub

End Module
My program (which I will call "binc" from now on) can now do the following:
bin2dec - both signed (two's compliment) and unsigned.
dec2bin - both signed (two's compliment) and unsigned.
I've also implemented some pretty heavy validation checks. The only thing that does not get validated is numeral input; where if the user enters a non-number, it will crash. Am I correct in saying that if in a numeral input you enter a non-number (such as the string "monkey"), it is counted as null? I really need to fix this, but I also need to know what causes the "why" behind the bug.

Once I perfect this, I will proceed upon trying to implement the following:
decimal to hexadecimal - both signed (two's compliment) and unsigned.
binary to hexadecimal - both signed (two's compliment) and unsigned.
hexadecimal to decimal - both signed (two's compliment) and unsigned.
hexadecimal to binary - both signed (two's compliment) and unsigned.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

DOLLS (J) [!] wrote:
grinvader wrote:It looks like a huge lot of code for what it's supposed to do. Is it bound to the language used ?
Of course not, don't act like you don't know basic D:
Even my BASIC (not visual) programs weren't fat like that for that amount of features. :p
皆黙って俺について来い!!

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
Rashidi
Trooper
Posts: 515
Joined: Fri Aug 18, 2006 2:45 pm

Post by Rashidi »

grinvader wrote:
DOLLS (J) [!] wrote:
grinvader wrote:It looks like a huge lot of code for what it's supposed to do. Is it bound to the language used ?
Of course not, don't act like you don't know basic D:
Even my BASIC (not visual) programs weren't fat like that for that amount of features. :p
yeah i'm still remember the line-number and (implicit) GOTO statement.

being user of ROM-Basic (in XT), BASICA, GW-BASIC & QBasic, myself.
i can vouch for grin statement.
ZH/Franky

Post by ZH/Franky »

Alright, more code:

Code: Select all

Module Module1

    Private BinaryString, binr, choice, signed_or_unsigned, HexString 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()
        Do Until quit = 1
            If disable_welcome_text <> 1 Then
                display_options()
            End If
            Console.WriteLine("What do you want to do?:")
            If disable_welcome_text = 1 Then
                Console.WriteLine("(for a list of options, type n)")
            End If
            disable_welcome_text = 1
            choice = Console.ReadLine()
            If choice = "q" Then
                quit = 1
            End If
            If choice = "n" Then
                display_options()
            End If
            If choice = "bin2dec" Then
                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
            End If
            If choice = "dec2bin" Then
                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
            End If
            If choice = "dec2hex" Then
                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
            End If
            If choice = "bin2hex" Then
                validate_bits()
                Console.WriteLine("Signed or unsigned? Signed will be in two's compliment")
                validate_sign()
                validate_BinaryString()
                bin2hex()
                Console.WriteLine(HexString)
            End If
        Loop
    End Sub

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

    Private Sub validate_sign()
        Do Until confirm = 1
            signed_or_unsigned = Console.ReadLine()
            If signed_or_unsigned <> "signed" And signed_or_unsigned <> "unsigned" Then
                Console.WriteLine("Invalid data! Try again:")
            End If
            If signed_or_unsigned = "signed" Or signed_or_unsigned = "unsigned" Then
                confirm = 1
            End If
        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 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"
            End If
            If remain = 0 Then
                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
            BinaryString = binr + BinaryString
        End If
        oor = 0
        binr = ""
        For pos = 1 To BinaryString.Length Step 4
            If Mid(BinaryString, pos, 4) = "0000" Then
                HexString = HexString + "0"
            End If
            If Mid(BinaryString, pos, 4) = "0001" Then
                HexString = HexString + "1"
            End If
            If Mid(BinaryString, pos, 4) = "0010" Then
                HexString = HexString + "2"
            End If
            If Mid(BinaryString, pos, 4) = "0011" Then
                HexString = HexString + "3"
            End If
            If Mid(BinaryString, pos, 4) = "0100" Then
                HexString = HexString + "4"
            End If
            If Mid(BinaryString, pos, 4) = "0101" Then
                HexString = HexString + "5"
            End If
            If Mid(BinaryString, pos, 4) = "0110" Then
                HexString = HexString + "6"
            End If
            If Mid(BinaryString, pos, 4) = "0111" Then
                HexString = HexString + "7"
            End If
            If Mid(BinaryString, pos, 4) = "1000" Then
                HexString = HexString + "8"
            End If
            If Mid(BinaryString, pos, 4) = "1001" Then
                HexString = HexString + "9"
            End If
            If Mid(BinaryString, pos, 4) = "1010" Then
                HexString = HexString + "A"
            End If
            If Mid(BinaryString, pos, 4) = "1011" Then
                HexString = HexString + "B"
            End If
            If Mid(BinaryString, pos, 4) = "1100" Then
                HexString = HexString + "C"
            End If
            If Mid(BinaryString, pos, 4) = "1101" Then
                HexString = HexString + "D"
            End If
            If Mid(BinaryString, pos, 4) = "1110" Then
                HexString = HexString + "E"
            End If
            If Mid(BinaryString, pos, 4) = "1111" Then
                HexString = HexString + "F"
            End If
        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
zbinc now does dec2hex and bin2hex (in both signed (two's compliment) and unsigned). All I need to do now is perfect those two new things I've added, then I'll try and implement hex2dec and hex2bin.

(PS: If you're wondering why I call my program "zbinc":
I go by the name "Franky" on this board, but I've recently decide to go by the name "Zero-Hero" if I ever sign up anywhere else. So, when I write a program, I call it z<name of program>. So for example, if I wrote a calculator program, I'd call it zcalc. Or if I wrote a text editor, I'd call it zwrite. Or if I wrote a drawing program like inkscape, etc, I'd call it zdraw... unfortunately, I couldn't do this if I ever wrote a snes emulator :D I'd call it Zero-Snes instead.)
odditude
Official tech support dood
Posts: 2122
Joined: Wed Jan 25, 2006 7:57 am

Post by odditude »

Franky wrote:

Code: Select all

            If Mid(BinaryString, pos, 4) = "0000" Then
                HexString = HexString + "0"
            End If
            ...absurd chain of ifs...
            If Mid(BinaryString, pos, 4) = "1111" Then
                HexString = HexString + "F"
            End If
        Next
look up the Select statement for great justice.
Why yes, my shift key *IS* broken.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

or just

Code: Select all

printf("%08x\n", strtoul(binary_string, NULL, 2));
wooha

Would be a little bigger for the other way, oooo

Code: Select all

uint32_t num = strtoul(hexadecimal_string, NULL, 16);
int i = 31;
do { printf("%u", (bool)(num&(1<<i))); } while (i--);
puts("");
ooooooo
皆黙って俺について来い!!

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
odditude
Official tech support dood
Posts: 2122
Joined: Wed Jan 25, 2006 7:57 am

Post by odditude »

grinvader wrote:or just

Code: Select all

printf("%08x\n", strtoul(binary_string, NULL, 2));
wooha

Would be a little bigger for the other way, oooo

Code: Select all

uint32_t num = strtoul(hexadecimal_string, NULL, 16);
int i = 31;
do { printf("%u", (bool)(num&(1<<i))); } while (i--);
puts("");
ooooooo
now vb it and make it legible to those who haven't yet learned to appreciate bitwise goodness, grin ;)
Why yes, my shift key *IS* broken.
ZH/Franky

Post by ZH/Franky »

odditude wrote:
Franky wrote:

Code: Select all

            If Mid(BinaryString, pos, 4) = "0000" Then
                HexString = HexString + "0"
            End If
            ...absurd chain of ifs...
            If Mid(BinaryString, pos, 4) = "1111" Then
                HexString = HexString + "F"
            End If
        Next
look up the Select statement for great justice.
OMG, thanks a bunch man. Looked it up and seriously, it makes my program so much tidier:

Code: Select all

Module Module1

    Private BinaryString, binr, choice, signed_or_unsigned, HexString 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()
        Do Until quit = 1
            If disable_welcome_text <> 1 Then
                display_options()
            End If
            Console.WriteLine("What do you want to do?:")
            If disable_welcome_text = 1 Then
                Console.WriteLine("(for a list of options, type n)")
            End If
            disable_welcome_text = 1
            choice = Console.ReadLine()
            If choice = "q" Then
                quit = 1
            End If
            If choice = "n" Then
                display_options()
            End If
            If choice = "bin2dec" Then
                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
            End If
            If choice = "dec2bin" Then
                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
            End If
            If choice = "dec2hex" Then
                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
            End If
            If choice = "bin2hex" Then
                validate_bits()
                Console.WriteLine("Signed or unsigned? Signed will be in two's compliment")
                validate_sign()
                validate_BinaryString()
                bin2hex()
                Console.WriteLine(HexString)
            End If
        Loop
    End Sub

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

    Private Sub validate_sign()
        Do Until confirm = 1
            signed_or_unsigned = Console.ReadLine()
            If signed_or_unsigned <> "signed" And signed_or_unsigned <> "unsigned" Then
                Console.WriteLine("Invalid data! Try again:")
            End If
            If signed_or_unsigned = "signed" Or signed_or_unsigned = "unsigned" Then
                confirm = 1
            End If
        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 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"
            End If
            If remain = 0 Then
                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 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'm going to see if I can use this in other areas.
odditude
Official tech support dood
Posts: 2122
Joined: Wed Jan 25, 2006 7:57 am

Post by odditude »

Franky wrote:

Code: Select all

    Private Sub dec2bin()
        Do Until digit = 0
            remain = (digit / 2) - Int(digit / 2)
            If remain > 0 Then
                binr = binr + "1"
            End If
            If remain = 0 Then
                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
dump redundant ifs. in this case, you don't need to explicitly check if remain = 0 because you already know it's not > 0.

Code: Select all

if remain > 0 then
    binr = binr + "1"
else
    binr = binr + "0"
end if
also, go back through the thread and look at my comment about the mod operator '%'.
Why yes, my shift key *IS* broken.
ZH/Franky

Post by ZH/Franky »

odditude wrote:
Franky wrote:

Code: Select all

    Private Sub dec2bin()
        Do Until digit = 0
            remain = (digit / 2) - Int(digit / 2)
            If remain > 0 Then
                binr = binr + "1"
            End If
            If remain = 0 Then
                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
dump redundant ifs. in this case, you don't need to explicitly check if remain = 0 because you already know it's not > 0.

Code: Select all

if remain > 0 then
    binr = binr + "1"
else
    binr = binr + "0"
end if
also, go back through the thread and look at my comment about the mod operator '%'.
Ok, well I've cleaned up the code a bit more:

Code: Select all

Module Module1

    Private BinaryString, binr, choice, signed_or_unsigned, HexString 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()
        disable_welcome_text = 0
        Do Until quit = 1
            Select Case disable_welcome_text
                Case 0
                    display_options()
                Case 1
                    Console.WriteLine("(for a list of options, type n)")
            End Select
            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)
            End Select
        Loop
    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 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 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'll look into operators soon.
DOLLS (J) [!]
ZNES Developer
Posts: 215
Joined: Mon Aug 02, 2004 11:22 pm

Post by DOLLS (J) [!] »

Rashidi wrote:
grinvader wrote:
DOLLS (J) [!] wrote:
grinvader wrote:It looks like a huge lot of code for what it's supposed to do. Is it bound to the language used ?
Of course not, don't act like you don't know basic D:
Even my BASIC (not visual) programs weren't fat like that for that amount of features. :p
yeah i'm still remember the line-number and (implicit) GOTO statement.

being user of ROM-Basic (in XT), BASICA, GW-BASIC & QBasic, myself.
i can vouch for grin statement.
Oh, my "of course not" was answering to grinvader's question. So, yes, Franky's code is bloated ATM.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

odditude wrote:now vb it
And that's where you lost me

About as enticing as
odditude wrote:now chop off your dick with a rusty spoon
皆黙って俺について来い!!

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
odditude
Official tech support dood
Posts: 2122
Joined: Wed Jan 25, 2006 7:57 am

Post by odditude »

grinvader wrote:
odditude wrote:now vb it
And that's where you lost me

About as enticing as
odditude wrote:now chop off your dick with a rusty spoon
nah, it's closer to "scrape through your dick using the rusted underside of a spoon after dipping it in salted lemon juice"
Why yes, my shift key *IS* broken.
Post Reply