How to convert hex to decimal values in RM Studio ?

reedevtaneja
reedevtaneja New Altair Community Member
edited November 5 in Community Q&A

I have machine sensors data which I want to convert on the fly from hex to decimal so that I can use it further processing. Is there is a direct way or Reg Ex or function to do that in RM Studio ? I could see hex() and unhex() funcion in Radoop ( Generate Attributes) but not in Studio. Any help is appreciated.

 

Thanks

Reed

Tagged:

Best Answers

  • bhupendra_patil
    bhupendra_patil New Altair Community Member
    Answer ✓

    Hello @reedevtaneja

     

    There doesnot seem to be an operator to do that, however we can use the execute script operator to do this.and the script is very easy

    Here is the script you will need and it is also in the attached example rmp process.

    Please not that it changes all columns from hex to dec, so you will need to prefix it with  "select attributes" to pass only the desired ones.

    Or depending on your use case, you cna hard code few things..

    Hope this is helpful

     

     

    ExampleSet exampleSet = operator.getInput(ExampleSet.class);

    for (Attribute attribute : exampleSet.getAttributes()) {
    String name = attribute.getName();
    for (Example example : exampleSet) {
    //example[name] = example[name].toUpperCase();
    example[name] = Integer.parseInt(example[name].trim(), 16 );

    }
    }

    return exampleSet;
  • reedevtaneja
    reedevtaneja New Altair Community Member
    Answer ✓

    Thanks BP, It was quick and helpful. 

  • bhupendra_patil
    bhupendra_patil New Altair Community Member
    Answer ✓

    For similar use casez: This is example of Binary to Integers

     

    ExampleSet exampleSet = operator.getInput(ExampleSet.class);

    for (Attribute attribute : exampleSet.getAttributes()) {
    String name = attribute.getName();
    for (Example example : exampleSet) {
    //example[name] = example[name].toUpperCase();
    example[name] = Integer.parseInt(example[name].trim(), 2 );

    }
    }

    return exampleSet;

  • bhupendra_patil
    bhupendra_patil New Altair Community Member
    Answer ✓

    This is example of Hex to Binary

     

     

    ExampleSet exampleSet = operator.getInput(ExampleSet.class);

    for (Attribute attribute : exampleSet.getAttributes()) {
    String name = attribute.getName();
    for (Example example : exampleSet) {
    //example[name] = example[name].toUpperCase();
    int myint = Integer.parseInt(example[name].trim(), 16 );
    example[name] = Integer.toBinaryString(myint)

    }
    }

    return exampleSet;

  • keildvol2
    keildvol2 New Altair Community Member
    Answer ✓

    Ran into the same problem... any help is highly appreciated. Thank you!

    EDIT: solved it in my case, maybe the following change to the script is helpful for others:

     

    ExampleSet exampleSet = operator.getInput(ExampleSet.class);

    for (Attribute attribute : exampleSet.getAttributes()) {
    String name = attribute.getName();
    for (Example example : exampleSet) {
    //example[name] = example[name].toUpperCase();
    example[name] = new BigInteger(example[name].trim(), 16);


    }
    }

    return exampleSet;

    You could also try Long type first (instead of int) if your hex values are not that big.

     

    Best regards

Answers

  • bhupendra_patil
    bhupendra_patil New Altair Community Member
    Answer ✓

    Hello @reedevtaneja

     

    There doesnot seem to be an operator to do that, however we can use the execute script operator to do this.and the script is very easy

    Here is the script you will need and it is also in the attached example rmp process.

    Please not that it changes all columns from hex to dec, so you will need to prefix it with  "select attributes" to pass only the desired ones.

    Or depending on your use case, you cna hard code few things..

    Hope this is helpful

     

     

    ExampleSet exampleSet = operator.getInput(ExampleSet.class);

    for (Attribute attribute : exampleSet.getAttributes()) {
    String name = attribute.getName();
    for (Example example : exampleSet) {
    //example[name] = example[name].toUpperCase();
    example[name] = Integer.parseInt(example[name].trim(), 16 );

    }
    }

    return exampleSet;
  • reedevtaneja
    reedevtaneja New Altair Community Member
    Answer ✓

    Thanks BP, It was quick and helpful. 

  • bhupendra_patil
    bhupendra_patil New Altair Community Member
    Answer ✓

    For similar use casez: This is example of Binary to Integers

     

    ExampleSet exampleSet = operator.getInput(ExampleSet.class);

    for (Attribute attribute : exampleSet.getAttributes()) {
    String name = attribute.getName();
    for (Example example : exampleSet) {
    //example[name] = example[name].toUpperCase();
    example[name] = Integer.parseInt(example[name].trim(), 2 );

    }
    }

    return exampleSet;

  • bhupendra_patil
    bhupendra_patil New Altair Community Member
    Answer ✓

    This is example of Hex to Binary

     

     

    ExampleSet exampleSet = operator.getInput(ExampleSet.class);

    for (Attribute attribute : exampleSet.getAttributes()) {
    String name = attribute.getName();
    for (Example example : exampleSet) {
    //example[name] = example[name].toUpperCase();
    int myint = Integer.parseInt(example[name].trim(), 16 );
    example[name] = Integer.toBinaryString(myint)

    }
    }

    return exampleSet;

  • w_f_vandervegte
    w_f_vandervegte New Altair Community Member

    bhupendra_patil, I had the same question and I found your script here - thanks. I tried it in my similar process, after "select attributes" but it produced an error message: see attached screeshots. Any idea what may have gone wrong here?

  • keildvol2
    keildvol2 New Altair Community Member
    Answer ✓

    Ran into the same problem... any help is highly appreciated. Thank you!

    EDIT: solved it in my case, maybe the following change to the script is helpful for others:

     

    ExampleSet exampleSet = operator.getInput(ExampleSet.class);

    for (Attribute attribute : exampleSet.getAttributes()) {
    String name = attribute.getName();
    for (Example example : exampleSet) {
    //example[name] = example[name].toUpperCase();
    example[name] = new BigInteger(example[name].trim(), 16);


    }
    }

    return exampleSet;

    You could also try Long type first (instead of int) if your hex values are not that big.

     

    Best regards