Well it is not a terribly good example out of context like this but ...
Let's look at the purpose of %STR. It is normally used when (say) a C function has retuned to you a pointer to a null terminated string. %STR will go through the memory referenced by that pointer and copy the data if finds a byte at a time until it reaches a byte containing hex 00. i.e. the null terminator for the string. At that point it stops copying and (in the case of a fixed length target) will blank pad any remaining characters in the target string.
In the example they have somehow contrived to have tempIn contain a null terminate string. The example doesn't make a lot of sense since if tempIn is defined as const its content should not have been changeable. You are obviously not showing all the relevant code so it is hard to comment further.
But to talk to the code as it is shown... Let's say 'ABCDE' + X'00". The result in tempOut will be 'ABCD '. i.e. the four characters before the null padded with 6 blanks.
If the target had been a varying string then the length would have just been set to 4 and that would be that.
It could have been done as a one-liner - but not the way you show it. It would have to be:
tempI = %dec(%str(%addr(tempIn)): 7: 2); // Not tested and ugly ...
Even if %Dec ignored the null terminator your code would still not work because chances are that there will be data beyond the terminator left over n memory from a previous usage.
Using 'x' as meaning X'00' - Say the first time the routines was called the result was '001234567x' even if that worked for %Dec (it wouldn't because of the X'00') the next time you did the call you might get '123' followed by X'00'. But in reality that would be in memory '123x34567x' and that certainly would not work with %Dec even if it were bodied to ignore X'00" in the string.
Hope this helps. It would be nice to know what example you are working from. IBM's attempts to re-code in RPG programs originally written in C often contain truly idiotic translations - just came upon several in the XMLTOOLKIT examples.
Jon Paris
www.partner400.com
www.SystemiDeveloper.com
On Nov 22, 2018, at 11:53 AM, Booth Martin <booth@xxxxxxxxxxxx> wrote:
What is the purpose served by the field "value" (50A) and %STR(%ADDR()) as used in this IBM example? Why not just:
tempI = %dec(tempIn: 7: 2); ?
---------------------------------------
...
d tempIn 10 const
d tempOut 10
*****************************************
d tempI s 8P 2
d tempO s 8P 2
d value S 50A
/free
value = %STR(%ADDR(tempIn));
tempI=%DEC(value:7:2);
...
----------------------------------------------------
--
This is the RPG programming on the IBM i (AS/400 and iSeries) (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/rpg400-l.
Please contact support@xxxxxxxxxxxx for any subscription related questions.
Help support midrange.com by shopping at amazon.com with our affiliate link: https://amazon.midrange.com
As an Amazon Associate we earn from qualifying purchases.