Decimal to Hex conversion


ricmarti

Recommended Posts

I am reading a date register that is in decimal format that needs to be converted to hexadecimal , then the four digit number needs to be sperated into MSB, LSB. Then converted back to decimal. If done manually it would go like this, register value '1301' : converted to hex value 515: split to 5 & 15: converted to 5 & 21. So 1301 represents May 21. How can I perform this calculation in the DAQ expression box?

Thanks for your assistance.

Link to comment
Share on other sites

decimal to hex:

private string hexval = format("%04x",decimalValue)

then use string functions to pull out the desired values:

private month = strtodouble(left(hexval,2))

private year = strtodouble(right(hexval,2))

note the 04 in format() means prepend 0's to make the result at least 4 characters long. This makes it easier to split into month and year.

Link to comment
Share on other sites

Thanks. This partly works. In the variable value componenet expression(expanded) of page_2, if I enter format("%04x", decimal value) I get correct hex value. But if I enter private string hexval = format("%04x", decimal value) I get 'C1000 Channel or function not found' error. It appears the private string hexval statement is the problem. I am just learning the software, thanks for your help. I also tried combining the formula Right(format("%04x", decimal value),2) which did produce the correct value of the right two hex values. Then I tried converting this back to decimal value by entering format("%d",(Right(format("%04x",decimal value),2))) . There was no result, just a blue screen. I think I maybe trying to many operations in one entry?

Link to comment
Share on other sites

You probably just have a typo. Try just splitting it into two lines:

private string hexval

hexval = format(...)

Also, you don't want to use format() to make it into a number. Format() always returns a string. Use strToDouble() instead.

Link to comment
Share on other sites

  • 1 month later...

Still unable to obtain correct result.

If I enter following formula:

Right(Format("%04x",H[0]),2)

I get correct hex result of 1b

If I enter:

hexval = Right(Format("%04x",H[0]),2)

I get incorrect result of C1000 channel or function not found

I can live with the hex value being displayed, but it would be nice to display in decimal format.

Link to comment
Share on other sites

Where are you entering this? Did you declare hexval as a variable? It has to be a string variable. If you are trying to do this in a component's expression, you have to combine everything I did into one long expression:

strtodouble(left(Right(Format("%04x",H[0]),2),2)) + "/" + strtodouble(right(Right(Format("%04x",H[0]),2),2))

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.