(CP5) How to convert the text in Texfield to a string

edited December 2016 in Library Questions

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);
}

Answers

  • There are numerous syntax errors in your code.

    • Without a void draw(), ControlP5 can't update the GUI elements!

    • Hit ctrl+t in the editor to make your code readable

    • This line (22) gives an error: String hex = (cp5.get(Textfield.class, "Hexadecimal").getText(), 360,130);, what's the purpose of the 360 and 130?

    • Another error here (line 95): String bin = cp5.get(Textfield.class, "Binary");, the Get returns a ControllerInterface while you want a String, why not use the getText() method as in line 22?

    Also:

    https://processing.org/reference/binary_.html

    https://processing.org/reference/hex_.html

    https://processing.org/reference/unbinary_.html

    https://processing.org/reference/unhex_.html

  • The errors are mostly just different attempts to obtain a string from the input, the getText() method didn't give me a String variable to use that I could see? The 360 and 130 decide where the input is printed to on screen. Also, should I encompass all the code after the setup in the void draw()?

  • You should consider running the following code to understand the behavior of ControlP5:

    http://www.sojamo.de/libraries/controlP5/examples/controllers/ControlP5textfield/ControlP5textfield.pde

    Kf

  • An empty draw() should do it if you're not drawing anything else. ControlP5 plugs into the draw method and draws its GUI elements directly after draw() has finished by default, which is why you need to implement draw().

  • edited December 2016

    Okay, I now have running code, but I still have to find a way to use the inputted numbers in my code. I've removed both text errors until I learn how to actually use them.

    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);
    }
    void draw(){
    fill(0);
    }
    
    //Hexadecimal button
    //Hexadecimal to Decimal conversion
    public void HexadecimalS()
    {
    int num = 0, power = 0, res = 0;
    String hex = new String("34A");
    
    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()
    {
    int binary = 1000110;
    int power1 = 0, res2 = 0;
    while(binary > 0)
    {
      res2 = res2 + ((binary%2)*(int)(Math.pow(2,power1)));
      power1++;
      binary = binary/10;
    }
    println(res2);
    }
    
  • @kfrajer I understand what it does, I simply can't find a way to actually use the text that I input into the Textfield.

  • You can use the callbacks for Textfields the same way you used callbacks for Bangs:

    void Decimal(String input)
    {
        decimalString = input;
    }
    
    void Binary(String input)
    {
        bin = input;
    }
    
    void Hexadecimal(String input)
    {
        hex = input;
    }
    
  • This is now throwing up another error,

    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);
    }
    void draw(){
    fill(0);
    }
    
    //Hexadecimal button
    //Hexadecimal to Decimal conversion
    public void HexadecimalS(String Hexadecimal)
    {
    String hex = Hexadecimal;
    int num = 0, power = 0, res = 0;
    
    
    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(String Decimal)
    {
    String de = Decimal;
    int deci = Integer.parseInt(de), 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 Binary)
    {
    String bi = Binary;
    int binary = Integer.parseInt(bi);
    int power1 = 0, res2 = 0;
    while(binary > 0)
    {
      res2 = res2 + ((binary%2)*(int)(Math.pow(2,power1)));
      power1++;
      binary = binary/10;
    }
    println(res2);
    }
    
  • What does the error say?

    Kf

  • Look carefully: your Textfields are called Binary, Decimal and Hexadecimal, not BinaryS, DecimalS and HexadecimalS!

  • But I need to use the Bangs to actually run the code don't I? Otherwise all that lets me do is use the text fields, I can't actually submit an answer.

  • edited December 2016 Answer ✓

    You can do both (and have hitting enter in a textfield act as a click on a bang).

    Pay attention to how ControlP5 callbacks work: if you use a bang, the callback method shouldn't have any input arguments. If you use a textfield, it should have a String as input argument.

    Having a bang with name "DecimalS" will not call the method void DecimalS(String Decimal). Instead you get some red letters in the console.

    So implement the two callbacks:

    //gets executed when you hit enter in the textfield
    void Decimal(String input)
    {
        Decimal = input;
    }
    
    //gets executed when you click on a bang
    void DecimalS()
    {
        //do your thing
    }
    
  • Thank you very much, that works now, sorry for the confusion :)

Sign In or Register to comment.