How to convert hex to decimal values in RM Studio ?

reedevtanejareedevtaneja Member Posts: 2 Contributor I
edited November 2018 in Help

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

Best Answers

  • bhupendra_patilbhupendra_patil Administrator, Employee, Member Posts: 168 RM Data Scientist
    Solution Accepted

    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;
  • reedevtanejareedevtaneja Member Posts: 2 Contributor I
    Solution Accepted

    Thanks BP, It was quick and helpful. 

  • bhupendra_patilbhupendra_patil Administrator, Employee, Member Posts: 168 RM Data Scientist
    Solution Accepted

    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_patilbhupendra_patil Administrator, Employee, Member Posts: 168 RM Data Scientist
    Solution Accepted

    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;

  • keildvol2keildvol2 Member Posts: 1 Contributor I
    Solution Accepted

    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

Sign In or Register to comment.