Is there any CAPL function for converting decimal value into hexadecimal value? I have already looked in help option of CAPL browser.
-
How do you want to use the converted hexadecimal value? – Om Choudhary Jul 01 '19 at 11:28
2 Answers
Assuming that you want the number to be converted to a string, that you can print out. Either to the write window, into the testreport, etc.
You can use snprintf
like this:
snprintf(buffer,elcount(buffer),"%x",integervariable);
where buffer
is a char array big enough.
This example is taken from the Vector knowledge base and was among the first result on google.

- 3,500
- 2
- 12
- 24
For hexadecimal equivalent value:
You can make use of _pow
function (returns x to the power of y) and while
in the following way, which would return you the hexadecimal equivalent value:
double decToHexEquivalent(int n)
{
int counter,remainder,decimal_number,hexadecimal_number = 0;
while(n!=0)
{
remainder = decimal_number % 16;
hexadecimal_number = hexadecimal_number + remainder * _pow(10, counter);
n=n/16;
++counter;
}
return hexadecimal_number;
}
you can call the above function in the following way:
testfunction xyz(int n)
{
write("Hexadecimal:%d", decToHexa(n));
}
Caution: not tested
For hexadecimal value
declare a global variable char buffer[100]
in the variable section
variables
{
char buffer[100];
}
and then using snprintf
function you can convert an integer variable to a character array, like this:
void dectohexValue(int decimal_number)
{
snprintf(buffer,elcount(buffer),"%02X",decimal_number);
}
then finally you can use the function as follows:
testfunction xyz(int n)
{
dectohexValue(n);
write("Hexadecimal:%s", buffer);
}

- 492
- 1
- 7
- 27
-
Not by executing the code, but by simple thought: What will your code return when you input `10`. I would expect it to return `A`. What does your code return? – MSpiller Jul 04 '19 at 18:56