This forum is in archive mode. You will not be able to post new content.

Author Topic: [C++] binary -> decimal decoder by me  (Read 5987 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++] binary -> decimal decoder by me
« on: April 03, 2011, 06:56:55 AM »
Code: [Select]
#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
« Last Edit: April 13, 2011, 04:44:21 PM by gh0st »

Offline Huntondoom

  • Baron
  • ****
  • Posts: 856
  • Cookies: 17
  • Visual C# programmer
    • View Profile
Re: C++ binary -> decimal decoder by me
« Reply #1 on: 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:

Code: (vb.net) [Select]
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:
Code: [Select]
Dim Bytes() as byte = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello world!")and for reverse of the code Above:
Code: [Select]
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]
« Last Edit: April 03, 2011, 10:59:01 AM by Huntondoom »
Aslong as you are connected to the internet, you'll have no privacy

Advanced Internet Search
Clean Up!

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: C++ binary -> decimal decoder by me
« Reply #2 on: April 03, 2011, 04:58:24 PM »
Code: [Select]
#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
« Last Edit: April 03, 2011, 05:50:32 PM by gh0st »

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: C++ binary -> decimal decoder by me
« Reply #3 on: 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/
Satan911
Evilzone Network Administrator

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: C++ binary -> decimal decoder by me
« Reply #4 on: 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)
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [C++] binary -> decimal decoder by me
« Reply #5 on: 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.
« Last Edit: April 15, 2011, 07:52:22 AM by Xires »
-Xires

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: [C++] binary -> decimal decoder by me
« Reply #6 on: April 16, 2011, 03:13:34 AM »
lol thanx  :P

Offline Huntondoom

  • Baron
  • ****
  • Posts: 856
  • Cookies: 17
  • Visual C# programmer
    • View Profile
Re: [C++] binary -> decimal decoder by me
« Reply #7 on: 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)

Code: (vb.net) [Select]
''' <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
Aslong as you are connected to the internet, you'll have no privacy

Advanced Internet Search
Clean Up!

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: [C++] binary -> decimal decoder by me
« Reply #8 on: April 22, 2011, 06:33:55 PM »
sweet works with a math algorithm good job +1

Offline Tsar

  • Peasant
  • *
  • Posts: 126
  • Cookies: 10
  • turing-recognizable
    • View Profile
Re: [C++] binary -> decimal decoder by me
« Reply #9 on: April 25, 2011, 10:03:35 AM »
Quote
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:
Code: [Select]
#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/
« Last Edit: April 25, 2011, 10:05:30 AM by Tsar »

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: [C++] binary -> decimal decoder by me
« Reply #10 on: 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
« Last Edit: April 26, 2011, 01:27:37 AM by gh0st »

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.