Trying to add special character (%) sign to variable with following concatenation sign in ESQL, it gives me the the below error - messagebroker

Trying to add special character (%) sign to variable with following concatenation sign, but it gives me the the error: Invalid characters.
DECLARE Percent CHARACTER CAST ( ' %' AS CHARACTER CCSID 1208);
SET AlocatedAmount = 45
SET InPercent = AlocatedAmount||'%'
Result should be: InPercent = 45%
Error:Invalid characters::45 %
What's going wrong here?

AlocatedAmount seems to be an INTEGER, on what you cannot use the concatenation operator.
You need to cast that to CHARACTER first:
SET InPercent = CAST(AlocatedAmount AS CHARACTER) || '%';

So there is also the option of using FORMAT in your CAST
DECLARE Num INTEGER;
DECLARE FormattedStr CHAR;
SET Num = 45;
SET FormattedStr = CAST(Num AS CHAR FORMAT '#0%');
More information can be found at https://www.ibm.com/support/knowledgecenter/en/SSMKHH_9.0.0/com.ibm.etools.mft.doc/ak05615_.htm

Related

Problem getting the decimal separator setting in Windows

I hope you can help me to solve this.
I am building a Windows VCL application with C++ Builder. I have a Tedit object in which the user should type a temperature value, 25.35 for example. Before processing it, I need to check if the number is written correctly and, in particular, I need to check if the decimal separator is correct comparing it to the Windows 10 settings. In case the separator is wrong, it should be automatically set correctly and re-write the Text of the Tedit object.
I wrote this code:
char separator = std::use_facet< std::numpunct<char> >(std::cout.getloc()).decimal_point();
AnsiString str = refTemperature->Text;
if (separator == ',') {
str = StringReplace( str, ".", separator, TReplaceFlags() <<rfReplaceAll );
}
else
str = StringReplace( str, ",", separator, TReplaceFlags() <<rfReplaceAll );
refTemperature->Text = str;
Here, separator is always ".", even when my Windows Control Panel settings are Decimal symbol = ',' and Digit grouping symbol = '.'. So, in this case, the code does not work. Have got a solution? Thank you.

cl_http_utility not normalizing my url. Why?

Via an enterpreise service consumer I connect to a webservice, which returns me some data, and also url's.
However, I tried all methods of the mentioned class above and NO METHOD seems to convert the unicode-characters inside my url into the proper readable characters.... ( in this case '=' and ';' ) ...
The only method, which runs properly is "is_valid_url", which returns false, when I pass url's like this:
http://not_publish-workflow-dev.hq.not_publish.com/lc/content/forms/af/not_publish/request-datson-internal/v01/request-datson-internal.html?taskId\u003d105862\u0026wcmmode\u003ddisabled
What am I missing?
It seems that this format is for json values. Usually = and & don't need to be written with the \u prefix. To decode all \u characters, you may use this code:
DATA(json_value) = `http://not_publish-workflow-dev.hq.not_publish.com/lc`
&& `/content/forms/af/not_publish/request-datson-internal/v01`
&& `/request-datson-internal.html?taskId\u003d105862\u0026wcmmode\u003ddisabled`.
FIND ALL OCCURRENCES OF REGEX '\\u....' IN json_value RESULTS DATA(matches).
SORT matches BY offset DESCENDING.
LOOP AT matches ASSIGNING FIELD-SYMBOL(<match>).
DATA hex2 TYPE x LENGTH 2.
hex2 = to_upper( substring( val = json_value+<match>-offset(<match>-length) off = 2 ) ).
DATA(uchar) = cl_abap_conv_in_ce=>uccp( hex2 ).
REPLACE SECTION OFFSET <match>-offset LENGTH <match>-length OF json_value WITH uchar.
ENDLOOP.
ASSERT json_value = `http://not_publish-workflow-dev.hq.not_publish.com/lc`
&& `/content/forms/af/not_publish/request-datson-internal/v01`
&& `/request-datson-internal.html?taskId=105862&wcmmode=disabled`.
I hate to answer my own questions, but anyway, I found an own solution, via manually replacing those unicodes. It is similar to Sandra's idea, but able to convert ANY unicode.
I share it here, just in case, any person might also need it.
DATA: lt_res_tab TYPE match_result_tab.
DATA(valid_url) = url.
FIND ALL OCCURRENCES OF REGEX '\\u.{4}' IN valid_url RESULTS lt_res_tab.
WHILE lines( lt_res_tab ) > 0.
DATA(match) = substring( val = valid_url off = lt_res_tab[ 1 ]-offset len = lt_res_tab[ 1 ]-length ).
DATA(hex_unicode) = to_upper( match+2 ).
DATA(char) = cl_abap_conv_in_ce=>uccp( uccp = hex_unicode ).
valid_url = replace( val = valid_url off = lt_res_tab[ 1 ]-offset len = lt_res_tab[ 1 ]-length with = char ).
FIND ALL OCCURRENCES OF REGEX '\\u.{4}' IN valid_url RESULTS lt_res_tab.
ENDWHILE.
WRITE / url.
WRITE / valid_url.

Value can't be assigned to a host variable because it's not in range data type

CREATE PROCEDURE ADMINIST.STUDENT_CUSTOM_PROCEDURE1 (
IN p_id INTEGER,
IN p_maths INTEGER,
IN p_science INTEGER,
Out p_obtain_marks INTEGER,
out p_percentage decimal(3,2),
out p_status char(4)
)
P1: BEGIN
DECLARE p_total INTEGER;
SET p_total = 200;
SET p_obtain_marks = p_maths + p_science;
SET p_percentage = ((p_obtain_marks * 100)/p_total);
IF (p_percentage > 35) THEN
SET p_status = 'PASS';
ELSE
SET p_status = 'FAIL';
END IF;
insert into ADMINIST.STUDENT_RESULT values(p_id, p_maths, p_science, p_obtain_marks, p_percentage, p_status);
END P1
I got Error code:
SQLCODE=-304, SQLSTATE=22003
The DEC/DECIMAL data type is different than assumed. The data type information can be found in the DB2 manual under CREATE TABLE:
"DECIMAL(precision-integer, scale-integer) or DEC(precision-integer, scale-integer)
For a decimal number. The first integer is the precision of the number; that is, the total number of digits; it may range from 1 to 31. The second integer is the scale of the number; that is, the number of digits to the right of the decimal point; it may range from 0 to the precision of the number.
If precision and scale are not specified, the default values of 5,0 are used. The words NUMERIC and NUM can be used as synonyms for DECIMAL and DEC."
So in your case, to hold percentages, change the variable declaration:
out p_percentage decimal(5,2),
I got this answer by casting value into dacimal.
SET p_percentage = DECIMAL(((p_obtain_marks * 100)/p_total),3,2);
Thanks Henrik Loeser and Alex.

What standard produced hex-encoded characters with an extra "25" at the front?

I'm trying to integrate with ybp.com, a vendor of proprietary software for managing book ordering workflows in large libraries. It keeps feeding me URLs that contain characters encoded with an extra "25" in them. Like this book title:
VOLATILE KNOWING%253a PARENTS%252c TEACHERS%252c AND THE CENSORED STORY OF ACCOUNTABILITY IN AMERICA%2527S PUBLIC SCHOOLS.
The encoded characters in this sample are as follows:
%253a = %3A = a colon
%252c = %2C = a comma
%2527 = %27 = an apostrophe (non-curly)
I need to convert these encodings to a format my internal apps can recognize, and the extra 25 is throwing things off kilter. The final two digits of the hex encoded characters appear to be identical to standard URL encodings, so a brute force method would be to replace "%25" with "%". But I'm leary of doing that because it would be sure to haunt me later when an actual %25 shows up for some reason.
So, what standard is this? Is there an official algorithm for converting values like this to other encodings?
%25 is actually a % character. My guess is that the external website is URLEncoding their output twice accidentally.
If that's the case, it is safe to replace %25 with % (or just URLDecode twice)
The ASCII code 37 (25 in hexadecimal) is %, so the URL encoding of % is %25.
It looks like your data got URL encoded twice: , -> %2C -> %252C
Substituting every %25 for % should not generate any problems, as an actual %25 would get encoded to %25252525.
Create a counter that increments one by one for next two characters, and if you found modulus, you go back, assign the previous counter the '%' char and proceed again. Something like this.
char *str, *newstr; // Fill up with some memory before proceeding below..
....
int k = 0, j = 0;
short modulus = 0;
char first = 0, second = 0;
short proceed = 0;
for(k=0,j=0; k<some_size; j++,k++) {
if(str[k] == '%') {
++k; first = str[k];
++k; second = str[k];
proceed = 1;
} else if(modulus == 1) {
modulus = 0;
--j; first = str[k];
++k; second = str[k];
newstr[j] = '%';
proceed = 1;
} else proceed = 0; // Do not do decoding..
if(proceed == 1) {
if(first == '2' && second == '5') {
newstr[j] = '%';
modulus = 1;
......

How do I URL-escape a string in Mathematica?

For example,
urlesc["foo.cgi?abc=123"]
should return
foo.cgi%3Fabc%3D123
This is also known as percent-encoding.
Also, for better readability, spaces should encode to pluses.
I believe that's always acceptable for URL escaping.
Another method, using J/Link and java.net.URLEncoder:
In[116]:= Needs["JLink`"]; InstallJava[];
LoadJavaClass["java.net.URLEncoder"];
In[118]:= URLEncoder`encode["foo.cgi?abc=123"]
Out[118]= "foo.cgi%3Fabc%3D123"
There's also java.net.URLDecoder for decoding.
Here's my solution:
cat = StringJoin##(ToString/#{##})&; (* Like sprintf/strout in C/C++. *)
re = RegularExpression;
hex = IntegerString[#,16]&; (* integer to hex, represented as a string *)
up = ToUpperCase;
asc = ToCharacterCode[#][[1]]&; (* character to ascii code *)
subst = StringReplace;
urlesc[s_String] := subst[s, {" "->"+", re#"[^\w\_\:\.]":>"%"<>up#hex#asc#"$0"}]
urlesc[x_] := urlesc#cat#x
unesc[s_String] := subst[s, re#"\\%(..)":>FromCharacterCode#FromDigits["$1",16]]
As a bonus, here's a function to encode a list of rules like {a->2, b->3} into GET parameters like a=2&b=3, with appropriate URL-encoding:
encode[c_] := cat ## Riffle[cat[#1, "=", urlesc[#2]]& ### c, "&"]

Resources