Renuka Choudhari
Renuka Choudhari

Reputation: 21

Decimal to hexadecimal conversion using CAPL

Is there any CAPL function for converting decimal value into hexadecimal value? I have already looked in help option of CAPL browser.

Upvotes: 1

Views: 8827

Answers (2)

Om Choudhary
Om Choudhary

Reputation: 512

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);
}

Upvotes: -1

mspiller
mspiller

Reputation: 3857

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.

Upvotes: 3

Related Questions