How to convert hex to decimal values in RM Studio ?
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
-
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;1 -
Thanks BP, It was quick and helpful.
0 -
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;1 -
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;1 -
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
1
Answers
-
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;1 -
Thanks BP, It was quick and helpful.
0 -
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;1 -
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;1 -
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?
0 -
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
1