I'm trying to make a decimal/hexadecimal/binary converter for a college project using user input, but can't figure out any sort of way to actually use any of the stuff I input? Bear in mind I am useless at processing, so my code is messy and not complete.
Code:
import controlP5.*;
ControlP5 cp5;
void setup()
{
size(700, 400);
cp5 = new ControlP5(this);
cp5.addTextfield("Decimal").setPosition(20, 100).setSize(200, 40).setAutoClear(false);
cp5.addTextfield("Binary").setPosition(20, 170).setSize(200, 40).setAutoClear(false);
cp5.addTextfield("Hexadecimal").setPosition(20, 240).setSize(200, 40).setAutoClear(false);
cp5.addBang("DecimalS").setPosition(240, 100).setSize(200, 40);
cp5.addBang("BinaryS").setPosition(240,170).setSize(200, 40);
cp5.addBang("HexadecimalS").setPosition(240, 240).setSize(200, 40);
}
//Hexadecimal button
//Hexadecimal to Decimal conversion
public void HexadecimalS()
{
int num = 0, power = 0, res = 0;
String hex = (cp5.get(Textfield.class, "Hexadecimal").getText(), 360,130);
for(int i = hex.length()-1; i >= 0; i--)
{
char dec = hex.charAt(i);
String s1 = hex.substring(i, i+1);
switch(dec)
{
case 'A' : num = 10; break;
case 'B' : num = 11; break;
case 'C' : num = 12; break;
case 'D' : num = 13; break;
case 'E' : num = 14; break;
case 'F' : num = 15; break;
default : num = Integer.parseInt(s1);
}
res = res + ((num)*(int)(Math.pow(16,power)));
power++;
}
println(res);
//Hexadecimal to Binary conversion
String bin = "";
String binFragment = "";
int iHex;
hex = hex.trim();
hex = hex.replaceFirst("0x", "");
for(int i = 0; i < hex.length(); i++){
iHex = Integer.parseInt(""+hex.charAt(i),16);
binFragment = Integer.toBinaryString(iHex);
while(binFragment.length() < 4){
binFragment = "0" + binFragment;
}
bin += binFragment;
}
println(bin);
}
//Decimal button
//Decimal to binary conversion
public void DecimalS()
{
int deci = 142, res1 = 0, decim = deci;
String s2 = new String("");
while(deci > 0)
{
res1 = deci%2;
s2 = s2 + (Integer.toString(res1));
deci = deci/2;
}
for(int i = s2.length()-1; i>=0; i--)
{
print(s2.charAt(i));
}
println("");
//Decimal to hexadecimal conversion
String decHex = Integer.toHexString(decim);
println(decHex);
}
//Binary button
//Binary to decimal conversion
public void BinaryS()
{
String bin = cp5.get(Textfield.class, "Binary");
int binary = Integer.parseInt();
int power1 = 0, res2 = 0;
while(binary > 0)
{
res2 = res2 + ((binary%2)*(int)(Math.pow(2,power1)));
power1++;
binary = binary/10;
}
println(res2);
}