Select to view content in your preferred language

calculation for turn HEX to decimal in survey123 connect?

2049
6
Jump to solution
10-11-2023 01:58 PM
anonymous55
Frequent Contributor

Hello

I need to turn Hex number to decimal values in survey123 connect for one question?

do we have any formula or work around to achieve this?

Best

0 Kudos
2 Solutions

Accepted Solutions
Raul
by
Regular Contributor

You could use the parseInt() javascript function to convert hex numbers to integers.

function hexToInt(hex) {
    return parseInt(hex, 16);
}

 

Then use the survey123 calculation:

pulldata("@javascript", "functions.js", "hexToInt", ${your_hex_field})

View solution in original post

abureaux
MVP Frequent Contributor

While java can do this with less steps, not everyone can use Java in their environment. To do this inside of S123 natively, you can use the attached (at least as a starting point).

input_txt
Your HEX input (only built it for 4 digits, but didn't add a bind::esri:fieldLength).

input_stuff
These calculates isolate the positions of the characters. E.g., substr(${input_txt}, 2, 3) will grab the third character of the string within input_txt. It starts from 0, and reads left to right.

convert_stuff
Will convert the alpha character (A to F) into a decimal number (10 to 15), or return the decimal number (0 to 9) without changing it.

calculate_stuff
multiplies the number by 16^x, where x is the position (This will explain better than me)

output_txt
Your decimal output

S123:
abureaux_1-1697223359728.png

Verification:
abureaux_3-1697223382348.png

 

Two things:

  1. You can simplify what I have done. I just have all the steps broken down and visible like that so you can see what it is doing. I also stopped at 4 digits because the pattern should be visible from that point.
  2. I kind of messed up my original workflow. The 4 calculates under the input_stuff group were originally in reverse order. Rather than redo everything, I choose to swap their order (see below), which fixed everything. I am telling you this because, unfortunately, this adds an extra step for anyone that wants to expand this beyond 4 digits. Sorry!
    abureaux_4-1697223587107.png

     

View solution in original post

6 Replies
MobiusSnake
MVP Regular Contributor

Is everyone using the form in your organization?  If so, you could use a JavaScript function, JS can convert hex to decimal pretty easily

0 Kudos
anonymous55
Frequent Contributor

@MobiusSnake Hey unfortunately I don't know JS. 😞

0 Kudos
abureaux
MVP Frequent Contributor

Should be doable via calculates.

From https://www.binaryhexconverter.com/hex-to-decimal-converter

abureaux_0-1697122008780.png

 

0 Kudos
anonymous55
Frequent Contributor

Thanks @abureaux do you know how can I use this calculation in survey123 Calculate field.

0 Kudos
Raul
by
Regular Contributor

You could use the parseInt() javascript function to convert hex numbers to integers.

function hexToInt(hex) {
    return parseInt(hex, 16);
}

 

Then use the survey123 calculation:

pulldata("@javascript", "functions.js", "hexToInt", ${your_hex_field})

abureaux
MVP Frequent Contributor

While java can do this with less steps, not everyone can use Java in their environment. To do this inside of S123 natively, you can use the attached (at least as a starting point).

input_txt
Your HEX input (only built it for 4 digits, but didn't add a bind::esri:fieldLength).

input_stuff
These calculates isolate the positions of the characters. E.g., substr(${input_txt}, 2, 3) will grab the third character of the string within input_txt. It starts from 0, and reads left to right.

convert_stuff
Will convert the alpha character (A to F) into a decimal number (10 to 15), or return the decimal number (0 to 9) without changing it.

calculate_stuff
multiplies the number by 16^x, where x is the position (This will explain better than me)

output_txt
Your decimal output

S123:
abureaux_1-1697223359728.png

Verification:
abureaux_3-1697223382348.png

 

Two things:

  1. You can simplify what I have done. I just have all the steps broken down and visible like that so you can see what it is doing. I also stopped at 4 digits because the pattern should be visible from that point.
  2. I kind of messed up my original workflow. The 4 calculates under the input_stuff group were originally in reverse order. Rather than redo everything, I choose to swap their order (see below), which fixed everything. I am telling you this because, unfortunately, this adds an extra step for anyone that wants to expand this beyond 4 digits. Sorry!
    abureaux_4-1697223587107.png