Use a for() loop from position n to start of field until a non space is found.


Field read is 120 bytes with blanks added to end of field.
For(i = 119; i > 0; i--) {
New_string[i] = (read_field[i] != ' ') ? '\0' : read_field[i];
If(read_field[i] != ' ')

If the field is NULL terminated when written then this would not be necessary, just use strcpy(new_field,read_field);

There are no API's or anything else to look for the length of a character array to a string automatically, you can using the above come up with one of your own.
Another option would be to have a separate field that stores the string length when written.


-----Original Message-----
From: C400-L <c400-l-bounces@xxxxxxxxxxxxxxxxxx> On Behalf Of Patrik Schindler
Sent: June 4, 2021 8:02 AM
To: Bare Metal Programming IBM i (AS/400 and iSeries) <c400-l@xxxxxxxxxxxxxxxxxx>
Subject: [C400-L] Fixed-Length strings vs. NUL-terminated strings


reading from a database table (declared with fixed length strings for easier handling in RPG for green screen use) with the _R API calls returns fixed length strings, filled with spaces until the space is exhausted.

Do I need to come up with a hand crafted function to "convert" these to C-style strings, or does IBM i provide an API for achieving this? By "conversion" I want to point out that I don't care if that function just places a NUL on the proper position's original buffer, or the string gets just copied and the copy is treated accordingly.

My Google searches yield a lot of unrelated information, because the question seems rather generic. The IBM API finder list yielded no usable results by searches for string, fixed or NUL.

Thanks for your hints!

:wq! PoC

This is the Bare Metal Programming IBM i (AS/400 and iSeries) (C400-L) mailing list To post a message email: C400-L@xxxxxxxxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
or email: C400-L-request@xxxxxxxxxxxxxxxxxx Before posting, please take a moment to review the archives at

Help support by shopping at with our affiliate

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2021 by and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.