• Not Answered

convert decimal to hex

Hello,

Can someone tell what expression should I put in to a calculation block to convert from 16 unsigned integer / 32 unsigned integer to hex value?

Thank you

1 Reply

  • What is it with HEX values today? second question on these.

    DeltaV does not have a Hexidecimal parameter type. There are bitwise operations that can be done on integers, but to show a value as a HEX number you have to use a String parameter. Since it is not clear what you are trying to accomplish, I'm not sure this will help you. Also note that once you get the data into a string, you cannot convert back to decimal/integer.

    I used a CALC block and some string parameters to extract the four HEX digits of a 16 bit integer. You would have to extend this for 32 bit, but the logic is the same. The hardest thing is to convert values greater than 9 to "A, B, C, D, E and F".

    '^/HEX_VAL.CV' := ""; rem Clear string each scan to be recreated.

    '^/TEMP1.CV' := shr('^/INTEGER.CV',12) & 15; rem Shift the 16 bits for the most significant digit and use bitwise AND with value of 15 to leave value of 0 - 15.
    if '^/TEMP1.CV' > 9 then
    '^/TEMP1.CV' := '^/TEMP1.CV' - 9;
    if '^/TEMP1.CV' > 5 then
    '^/TEMP1.CV' := "F";
    else;
    '^/TEMP1.CV' := SELSTR('^/TEMP1.CV',"A","B","C","D","E"); Rem SELSTR can only do selection of 5 strings. So F is checked explicitly.
    endif;
    endif;
    '^/HEX_VAL.CV' := '^/TEMP1'; rem Add this string first as most significant.

    '^/TEMP2.CV' := shr('^/INTEGER.CV',8) & 15; rem note this shifts 8 bits leaving 8 bits. AND with 15 to negate the higher order bits leaving the second most significant HEX digit in binary form.
    if '^/TEMP2.CV' > 9 then
    '^/TEMP2.CV' := '^/TEMP2.CV' - 9;
    if '^/TEMP2.CV' > 5 then
    '^/TEMP2.CV' := "F";
    else;
    '^/TEMP2.CV' := SELSTR('^/TEMP2.CV',"A","B","C","D","E");
    endif;
    endif;
    '^/HEX_VAL.CV' := ( '^/HEX_VAL.CV' + '^/TEMP2');

    '^/TEMP3.CV' := shr('^/INTEGER.CV',4) & 15;
    if '^/TEMP3.CV' > 9 then
    '^/TEMP3.CV' := '^/TEMP3.CV' - 9;
    if '^/TEMP3.CV' > 5 then
    '^/TEMP3.CV' := "F";
    else;
    '^/TEMP3.CV' := SELSTR('^/TEMP3.CV',"A","B","C","D","E");
    endif;
    endif;
    '^/HEX_VAL.CV' := '^/HEX_VAL.CV' + '^/TEMP3';


    '^/TEMP4.CV' := '^/INTEGER.CV' & 15;
    if '^/TEMP4.CV' > 9 then
    '^/TEMP4.CV' := '^/TEMP4.CV' - 9;
    if '^/TEMP4.CV' > 5 then
    '^/TEMP4.CV' := "F";
    else;
    '^/TEMP4.CV' := SELSTR('^/TEMP4.CV',"A","B","C","D","E");
    endif;
    endif;
    '^/HEX_VAL.CV' := ( '^/HEX_VAL.CV' + '^/TEMP4.CV');

    Andre Dicaire