~skaggapa/do-plugins/Convert

« back to all changes in this revision

Viewing changes to Convert/src/ConvertAction.cs

  • Committer: anders
  • Date: 2009-01-03 01:02:07 UTC
  • Revision ID: anders@hefaistos-20090103010207-jo20ch4ws6e3shpz
Convert plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ConvertAction.cs created with MonoDevelop
 
2
// User: anders at 2:37 PM 9/8/2008
 
3
// By: Anders Dånmark
 
4
// Licence: GPLv2
 
5
// Authors note: If you like the software and we meet 
 
6
// by me a beer =)
 
7
// Contact: skaggapa at hotmail dot com
 
8
//
 
9
// USE THIS SOFTWARE AT YOUR OWN RISK
 
10
// THE AUTHOR TAKES NO RESPONSIBILITY
 
11
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
 
12
//
 
13
 
 
14
using System;
 
15
using Do.Universe;
 
16
using System.Text;
 
17
 
 
18
namespace Convert 
 
19
{
 
20
 
 
21
        public class ConvertAction : AbstractAction
 
22
        {
 
23
                public ConvertAction() 
 
24
                {
 
25
                }
 
26
                public override string Name { get { return "Convert"; }  }
 
27
                public override string Description { get { return "Converts number to binary, decimal and hexadecimal"; }  }
 
28
                public override string Icon { get { return "gnome-terminal" ;}  }
 
29
                
 
30
                public override Type[] SupportedItemTypes {
 
31
                        get { return new Type[] { typeof (ITextItem) }; }
 
32
                }
 
33
 
 
34
                public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
 
35
                {
 
36
                        string input = (items[0] as ITextItem).Text;
 
37
                        //first assume binary
 
38
                        try {
 
39
                                int i = (int)System.Convert.ToInt32(input,2);
 
40
                                StringBuilder output = new StringBuilder();
 
41
                                // its binary return dec and hex
 
42
                                output.Append(input);
 
43
                                output.Append(" = ");
 
44
                                output.Append( i.ToString() );
 
45
                                output.Append( " , " );
 
46
                                output.Append("0x");
 
47
                                output.Append( i.ToString("X") );
 
48
                                
 
49
                                return new IItem[] { new TextItem (output.ToString() ) } ;
 
50
                        }
 
51
                        catch (Exception e) {
 
52
                                Console.Error.WriteLine(e.Message);
 
53
                        }
 
54
                        //assume decimal before hex cause small decimal can be intepreted as hex
 
55
                        // hex should have 0x in front but no check yet
 
56
                        try {
 
57
                                
 
58
                                int i = Int32.Parse(input);
 
59
                                StringBuilder output = new StringBuilder();
 
60
                                output.Append(input);
 
61
                                output.Append(" = ");
 
62
                                output.Append("0x");
 
63
                                output.Append( i.ToString("X") );
 
64
                                output.Append( " , " );
 
65
                                output.Append( ToBinary(i) );
 
66
                                return new IItem[] { new TextItem ( output.ToString() ) } ;
 
67
                        }
 
68
                        catch (Exception e) {
 
69
                                Console.Error.WriteLine(e.Message);
 
70
                        }
 
71
 
 
72
                        
 
73
                        // assume hex
 
74
                        try {
 
75
 
 
76
                                int i = (int) System.Convert.ToInt32(input,16);
 
77
                                //its hex return dec and binary
 
78
                                StringBuilder output = new StringBuilder();
 
79
                                output.Append(input);
 
80
                                output.Append(" = ");
 
81
                                output.Append( i.ToString());
 
82
                                output.Append( " , " );
 
83
                                output.Append( ToBinary(i) );
 
84
 
 
85
                                return new IItem[] { new TextItem ( output.ToString() ) } ;
 
86
                        }
 
87
                        catch (Exception e) {
 
88
                                Console.Error.WriteLine(e.Message);
 
89
                        }
 
90
 
 
91
                        // neither or someting mysterious happened, say error
 
92
                        return new IItem[] { new TextItem ("error") } ;
 
93
 
 
94
                }
 
95
                
 
96
                public string ToBinary(int i)
 
97
                {
 
98
                        // Declare a few variables we're going to need
 
99
                        Int64 BinaryHolder;
 
100
                        char[] BinaryArray;
 
101
                        string BinaryResult = "";
 
102
                        
 
103
                        while (i > 0)
 
104
                        {
 
105
                                BinaryHolder = i % 2;
 
106
                                BinaryResult += BinaryHolder;
 
107
                                i = i / 2;
 
108
                        }
 
109
                        
 
110
                        // The algoritm gives us the binary number in reverse order (mirrored)
 
111
                        // We store it in an array so that we can reverse it back to normal
 
112
                        BinaryArray = BinaryResult.ToCharArray();
 
113
                        Array.Reverse(BinaryArray);
 
114
                        BinaryResult = new string(BinaryArray);
 
115
                        // comment
 
116
                        
 
117
                        return BinaryResult;                    
 
118
                }
 
119
                
 
120
        }
 
121
}       
 
 
b'\\ No newline at end of file'