Hex code conversion

How do i convert + to + in the script?

Dynamic vaue I extract has + which is to be converted to +

1
  • Suggested Answer

    0  

    Hi,

    The &#x prefix in an XML or HTML entity stands for a hexadecimal Unicode code point, which means it can represent any valid Unicode character.

    "+" is an XML entity for '+' (ascii value: 2b) sign.

    Knowing that, you can use LR XML api to convert.

    Here is a working example:

    #include "as_web.h"

    char * xml_input =
    "<response>"
        "&#x2b;"
    "</response>";

    Action() {

         int i, NumOfValues;
         char buf[64];

         lr_save_string(xml_input, "XML_Input_Param"); // Save input as parameter

         lr_xml_extract("XML={XML_Input_Param}",
              "XMLFragmentParam=Result",
              "Query=/response/text()", LAST );

         lr_output_message(lr_eval_string("Extracted: {Result}"));

         return 0;
    }

  • 0

    Tedy,

    Looks like the solution you shared is to extract values from xml.

    My query is as follows,

    I am fetching a value from my response through correlation which is abc&#x2b;xyz&#x3d;

    And in the request, i need to pass this value abc+xyz=

    So need to know how to perform this conversion.

  • 0   in reply to 

    Hi Selifa,

    Yes. XML extraction is just a technique because the string you got from the server and saved into a correlation parameter is actually very common in XML.  This is the point.

    In the revised example, you will see a simulated correlation_param which is then wrapped into an XML element for parsing.

    #include "as_web.h"


    Action() {
         lr_save_string("abc&#x2b;xyz&#x3d;", "correlation_param");
         lr_save_string(lr_eval_string("<response>{correlation_param}</response>"), "XML_Input_Param"); // Wrap your encoded value in XML element and save it as a new parameter

         lr_xml_extract("XML={XML_Input_Param}",
              "XMLFragmentParam=Result",
              "Query=/response/text()", LAST ); //this is to extract the parsed string via XPath

         lr_output_message(lr_eval_string("Extracted: {Result}"));

         return 0;
    }

    Output:

    Action.c(9): "lr_xml_extract" succeeded, 1 match processed
    Action.c(13): Extracted: abc+xyz=

    Besides, you can seek for help from GenAI service for future scripting issues.

  • 0   in reply to 

     

    You might have a look to the post Hexadecimal Conversion. It converts strings with hex encoding of \xNN.

    When you have some experience with C programming it is not that difficult to change this function, so it can convert &#xNN;

    But here is my solution for you: a bit cheaper, as all those lr_* functions, but it is restricted to ASCII characters only.

    /*
     * Converts a sting with &#xNN; encoded characters to a string with the encoded
     * characters replaced by their nummerical replacement. The encoding can be in
     * lower- or uppercase.
     * Arguments:
     *   char *hex:  A string that needs conversion. This string is first processed
     *               by lr_eval_string(), so you can also convert a LR parameter.
     *   char *param The name of the parameter that receives the results of the
     *               conversion.
     * An error message might be raised when an incorrect sequence is detected.
     * Note to convert a from-to single parameter use:
     *   convert_hex_string("{param}", "param");
     * You can also convert literal strings like:
     *   convert_hex_string("Hallo&#x2b;World&#x20;&#x32;&#X30;&#x32;&#x34;", "param");
     * vuser_init.c(60): Notify: Saving Parameter "param = Hallo+World 2024"
     */
    void convert_hex_string( char *hex, char *param ) {
        int n, m;
        char *buf, *s, *d, *e;
        // lr_eval_string() gives a memory buffer that we can use during conversion.
        buf = s = d = lr_eval_string( hex );
        e = s + strlen(s) - 6;        // Encoded character is 6 long, so we can stop early. 
        while( s < e ) {
            if ( *s == '&' && *(s+1) == '#' && (*(s+2) == 'X' || *(s+2) == 'x') && *(s+5) == ';' ) {
                unsigned int i, j;
                s++; s++; s++;        // skip ampersand, hash and x or X and look to first hex digit
                i = (*s) - '0';       // Assume 0..9
                if( i > 9 ) {         // Assume A .. F
                    i = (*s) - 'A'; 
                    if( i > 5 ) {     // Assume a .. f
                        i = (*s) - 'a'; 
                        if( i > 5 ) {
                            lr_error_message("Unexpected character %c in &#xNN; encoding at %6.6s", *s, s-3);
                            *d++ = '&'; s -= 2; // Do not convert.
                            continue;
                        }
                    }
                    i += 10;
                }
                s++;                 // Next N
                j = (*s) - '0';      // Assume 0..9
                if( j > 9 ) {        // Assume A .. F
                    j = (*s) - 'A'; 
                    if( j > 5 ) {    // Assume a .. f
                        j = (*s) - 'a'; 
                        if( j > 5 ) {
                            lr_error_message("Unexpected character %c in &#xNN; encoding at %6.6s", *s, s-3);
                            *d++ = '&'; s -= 2; // Do not convert.
                            continue;
                        }
                    }
                    j += 10;
                }
                *d++ = (char) ((i<<4) + j);
                s++; s++;           // Skip ';'
            } else {
                *d++ = *s++;
            }
        }
        while( *s ) *d++ = *s++;    // Copy last 5 bytes.
        *d = *s;                    // Copy null byte
        lr_save_string(buf, param);
    }

    How to ask questions

    Reward contributions via likes or 'verified answers'