EvilZone

Programming and Scripting => C - C++ => : gh0st April 03, 2011, 06:56:55 AM

: [C++] binary -> decimal decoder by me
: gh0st April 03, 2011, 06:56:55 AM
:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int x;
    string a =  "Binary -> Decimal by gh0st Evilzone.org\n";cout<< a;
    cout << "enter a binary number of 4 figures: \n";
    cin >> x;
    if (x == 1)
    {
    cout << "the number in decimal is 1\n";
    system("pause");
    return 0;
    }
    if (x == 11)
    {
    cout << "the number in decimal is 3\n";
    system("pause");
    return 0;
    }
    if (x == 1011)
    {
    cout << "the number in decimal is 11\n";
    system("pause");
    return 0;
    }
    if (x == 1110)
    {
    cout << "the number in decimal is 14\n";
    system("pause");
    return 0;
    }
    if (x == 111 )
    {
    cout << "the number in decimal is 7\n";
    system("pause");
    return 0;
    }
    if (x == 1101)
    {
    cout << "the number in decimal is 13\n";
    system("pause");
    return 0;
    }
    if (x == 1000 )
    {
    cout << "the number in decimal is 8\n";
    system("pause");
    return 0;
    }
    if (x == 100 )
    {
    cout << "the number in decimal is 4\n";
    system("pause");
    return 0;
    }
    if (x == 10 )
    {
    cout << "the number in decimal is 2\n";
    system("pause");
    return 0;
    }
    if (x == 1111 )
    {
    cout << "the number in decimal is 15\n";
    system("pause");
    return 0;
    }
    if (x == 0000 )
    {
    cout << "the number in decimal is 0\n";
    system("pause");
    return 0;
    }
    if (x == 1010 )
    {
    cout << "the number in decimal is 10\n";
    system("pause");
    return 0;
    }
    if (x == 101)
    {
    cout << "the number in decimal is 5\n";
    system("pause");
    return 0;
    }
    if (x == 110 )
    {
    cout << "the number in decimal is 6\n";
    system("pause");
    return 0;
    }
    if (x == 1100)
    {
    cout << "the number in decimal is 12\n";
    system("pause");
    return 0;
    }
    if (x == 1001 )
    {
    cout << "the number in decimal is 9\n";
    system("pause");
    return 0;
    }
   
    else
    {
    cout << "error invalid input\n";
    system("pause");
    return 0;
    }
}

what the program does is to decode binary numbers (for example 0010 to 2) in decimal but just numbers of 4 characters no more.

well I wanted to add an ASCII art of EVILZONE but I cant
can someone help me plz?  :(

award: +2 of karma
: Re: C++ binary -> decimal decoder by me
: Huntondoom April 03, 2011, 10:58:46 AM
I dont Know how to do it in C++
but In VB.net it should look like this:

: (vb.net)
Dim Bytes() as byte
Dim ByteString as string = ""
For Each Bit as byte in Bytes
Bytestring &= byte.tostring
next
this will already be in decimal numbers.

but for the ascii encoding stuff:
:
Dim Bytes() as byte = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello world!")and for reverse of the code Above:
:
Dim Bytes() as byte = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello world!")
DIm StringLine as string = System.text.ASCIIEncoding.ASCII.getstring(Bytes)

Ps: Please use the code block instead of Quotes:
[ code] [ /code]
: Re: C++ binary -> decimal decoder by me
: gh0st April 03, 2011, 04:58:24 PM
:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int x;
     string a = "                    \n"; cout << a;
    string b = "      EVILZONE        \n"; cout << b;
    string c = "                     \n"; cout << c;
    string d = "                      \n"; cout << d;
    string e = "         &&&          \n"; cout << e;
    string f = "        <o.o>         \n"; cout << f;
    string g = "         \\-/         \n"; cout << g;
    string h = "          W           \n"; cout << h;
    string i = "         /|\\         \n"; cout << i;
    string j = "        / | \\        \n"; cout << j;
    string k = "       O  |  O        \n"; cout << k;
    string l = "         / \\         \n"; cout << l;
    string m = "        /   \\        \n"; cout << m;
    string n = "       /     \\       \n"; cout << n;
    string o = "      ()     ()       \n"; cout << o; 
                                                                                 
    cout << "enter a binary number of 4 figures: \n";
    cin >> x;
    if (x == 1)
    {
    cout << "the number in decimal is 1\n";
    system("pause");
    return 0;
    }
    if (x == 11)
    {
    cout << "the number in decimal is 3\n";
    system("pause");
    return 0;
    }
    if (x == 1011)
    {
    cout << "the number in decimal is 11\n";
    system("pause");
    return 0;
    }
    if (x == 1110)
    {
    cout << "the number in decimal is 14\n";
    system("pause");
    return 0;
    }
    if (x == 111 )
    {
    cout << "the number in decimal is 7\n";
    system("pause");
    return 0;
    }
    if (x == 1101)
    {
    cout << "the number in decimal is 13\n";
    system("pause");
    return 0;
    }
    if (x == 1000 )
    {
    cout << "the number in decimal is 8\n";
    system("pause");
    return 0;
    }
    if (x == 100 )
    {
    cout << "the number in decimal is 4\n";
    system("pause");
    return 0;
    }
    if (x == 10 )
    {
    cout << "the number in decimal is 2\n";
    system("pause");
    return 0;
    }
    if (x == 1111 )
    {
    cout << "the number in decimal is 15\n";
    system("pause");
    return 0;
    }
    if (x == 0000 )
    {
    cout << "the number in decimal is 0\n";
    system("pause");
    return 0;
    }
    if (x == 1010 )
    {
    cout << "the number in decimal is 10\n";
    system("pause");
    return 0;
    }
    if (x == 101)
    {
    cout << "the number in decimal is 5\n";
    system("pause");
    return 0;
    }
    if (x == 110 )
    {
    cout << "the number in decimal is 6\n";
    system("pause");
    return 0;
    }
    if (x == 1100)
    {
    cout << "the number in decimal is 12\n";
    system("pause");
    return 0;
    }
    if (x == 1001 )
    {
    cout << "the number in decimal is 9\n";
    system("pause");
    return 0;
    }
   
    else
    {
    cout << "error invalid input\n";
    system("pause");
    return 0;
    }
}

well this will be my ASCII art for a while Satan  :P
: Re: C++ binary -> decimal decoder by me
: Satan911 April 04, 2011, 01:30:33 AM
1) You should never use system calls (like system("pause")) when it's not absolutely needed.

2) A switch case might be a better option here. What would be even better is to make your own algorithm to convert binary to decimal instead of using an ton of if or cases (and it's not that hard).

3) As for the ascii you can format cout output pretty much anyway you like. The ascii table might help you a little. http://www.asciitable.com/ (http://www.asciitable.com/)
: Re: C++ binary -> decimal decoder by me
: bluechill April 10, 2011, 06:02:59 PM
1) You should never use system calls (like system("pause")) when it's not absolutely needed.

Considering that he is probably using it to halt the program he could just use sleep(-1)
: Re: [C++] binary -> decimal decoder by me
: Xires April 15, 2011, 07:39:22 AM
I concur with Satan911 on all 3 counts.  That is not to say that you don't deserve some congratulations on creating something yourself, gh0st; by that simple act you have placed yourself above many others and deserve recognition for it.

  ____                            _         _       _   _                 
 / ___|___  _ __   __ _ _ __ __ _| |_ _   _| | __ _| |_(_) ___  _ __  ___
| |   / _ \| '_ \ / _` | '__/ _` | __| | | | |/ _` | __| |/ _ \| '_ \/ __|
| |__| (_) | | | | (_| | | | (_| | |_| |_| | | (_| | |_| | (_) | | | \__ \
 \____\___/|_| |_|\__, |_|  \__,_|\__|\__,_|_|\__,_|\__|_|\___/|_| |_|___/
                  |___/                                                   


Okay, that's over; now go improve your code.  If you need help, don't hesitate to ask.

Good luck && have fun.
: Re: [C++] binary -> decimal decoder by me
: gh0st April 16, 2011, 03:13:34 AM
lol thanx  :P
: Re: [C++] binary -> decimal decoder by me
: Huntondoom April 22, 2011, 06:30:18 PM
something I was working on for a program.
Then I remember you wanted to convert this stuff also so:
(its in Vb though, but easily translatable :P)

: (vb.net)
''' <summary>
    ''' Convert a number to Byte(1010011)
    ''' </summary>
    ''' <param name="Number">the number to convert</param>
    ''' <returns>the byte in a string</returns>
    ''' <remarks></remarks>
    Function toByte(ByVal Number As Integer) As String
        Dim Bytes As New List(Of String)
        While Number <> 0
            Dim I As Integer
            For I = 1 To Number
                If 2 ^ (I + 1) > Number Then Exit For
            Next
            Number -= (2 ^ I)
            Bytes.Add(I)
        End While
        Dim ByteStr As New List(Of String)
        For J As Integer = 0 To Bytes(0)
            ByteStr.Add("0")
        Next
        For J As Integer = 0 To Bytes.Count - 1
            ByteStr(Bytes(J)) = 1
        Next
        Return Join(ByteStr.ToArray.Reverse.ToArray, "")
    End Function

    ''' <summary>
    ''' Converts a Byte (in String) to Decimal
    ''' </summary>
    ''' <param name="Bytes"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function toDecimal(ByVal Bytes As String) As Integer
        Dim R As Integer = 0
        Dim D As Integer = Bytes.Length
        For I As Integer = 1 To Bytes.Length
            Dim Bit As Integer = CInt(Mid(Bytes, I, 1))
            If Bit = 1 Then
                R += 2 ^ (D - I)
            End If
        Next
        Return R
    End Function
: Re: [C++] binary -> decimal decoder by me
: gh0st April 22, 2011, 06:33:55 PM
sweet works with a math algorithm good job +1
: Re: [C++] binary -> decimal decoder by me
: Tsar April 25, 2011, 10:03:35 AM
what the program does is to decode binary numbers (for example 0010 to 2) in decimal but just numbers of 4 characters no more.

well I wanted to add an ASCII art of EVILZONE but I cant
can someone help me plz?  :(

award: +2 of karma

Not a bad start, but generally you want to avoid a bunch of cases if possible.

Here is a better way:
:
#include <bitset>
#include <iostream>
#include <string>

int main ()
{
    cout << "Binary -> Decimal by gh0st Evilzone.org" << endl;
    string x = "";
    cout << "enter a binary number of 4 figures:" << endl;
    cin >> x;
    bitset<4> binary (x); // initialize from string
    cout << "The number in decimal is " <<  binary.to_ulong() << endl;
    return 0;
}

(Note: Not tested may of missed something but that's the basics, also if you declare the size of the bitset dynamically you could technically have no size limit) More on bitsets: http://www.cplusplus.com/reference/stl/bitset/ (http://www.cplusplus.com/reference/stl/bitset/)
: Re: [C++] binary -> decimal decoder by me
: gh0st April 26, 2011, 01:24:52 AM
@Tsar, cool that works damn it this reminds me to Sentinel I may be doing something by a long way I should take another short way however thanks for your post btw +1