Re: Converting packed decimal to a char array

Aaron Albertson <albertaa-r/[email protected]>
Newsgroups gmane.comp.lang.as400.c
Message-ID <OF3E657C55.54149FC0-ON85257DC8.00590821-86257DC8.005B4CB2@us.ibm.com>
Hi Jevgeni,

Assuming that you want to avoid a complicated case statement in the
function you will have to use a (ugh) function-like macro.  Here is an
example of what such a macro would look like.  It is very bare bones.
You'd need to be sure first that the size of your intermediate string is as
large as your largest number of potential digits plus three (for the '-'
and '.' characters and the trailing null character), and second that the
size of the character array passed to the macro is as large as the number
of digits in the passed in packed decimal plus two (for the '-' and '.'
characters).  The first part is easy by just setting the size of the
intermediate string to be the maximum possible size of a packed decimal
value plus three.  The example below assumes you are not using 63-digit
packed decimals.  If you want to use 63-digit packed decimals replace
DEC_DIG with DEC63_DIG.  The second part could be verified in the macro.

Note: DEC_DIG, digitsof, and precisionof are all defined in the 'decimal.h'
header file.

----------------------------- Begin Example -----------------------------

#include <decimal.h>
#include <stdio.h>
#include <stdlib.h>

char intermediate_form[DEC_DIG + 3];
#define packedToCharArray(charArray, packedVal) { \
                                                  \
  sprintf(intermediate_form, "%0*D(*,*)",         \
          digitsof(packedVal) + 2,                \
          digitsof(packedVal),                    \
          precisionof(packedVal),                 \
          packedVal);                             \
                                                  \
  memcpy(charArray, intermediate_form,            \
         digitsof(packedVal) + 2);                \
                                                  \
}

int main(void){
  decimal (15,9) A = -123456.123456789D;
  decimal (15,9) B = 123456.123456789D;
  char sField[17];

  packedToCharArray(sField, A);

  printf("%.17s\n", sField);

  packedToCharArray(sField, B);

  printf("%.17s\n", sField);
}

----------------------------- End Example -------------------------------

Hope that helps!

Aaron




From:	Jevgeni Astanovski <[email protected]>
To:	"Bare Metal Programming IBM i (AS/400 and iSeries)"
            <c400-l-Zwy7GipZuJhWk0Htik3J/[email protected]>
Date:	01/09/2015 03:01 AM
Subject:	[C400-L] Converting packed decimal to a char array
Sent by:	"C400-L" <c400-l-bounces-Zwy7GipZuJhWk0Htik3J/[email protected]>



Hi all,

spend a lot of time and feeling like I run into a dead cycle with a
problem, that looks trivial.

I have packed decimals in my C programs that I need to convert to a
char array, representing the number (not zero terminated string).

For example:

decimal (15,9) A ;
char szTemp[32], sField[16] ;

sprintf(szTemp, "%016D(15,9)", A) ;
memcpy(sField, szTemp, 16) ;

What I want to achieve is get a function, that receives A and returns
sField.
Of course it must support any types of decimal - not only decimal
(15,9) but others like decimal(11,7), decimal (15,0), decimal (5,0)
and so on. And, of course, decimal can be positive or negative...


Any ideas?

Jevgeni.
--
This is the Bare Metal Programming IBM i (AS/400 and iSeries) (C400-L)
mailing list
To post a message email: C400-L-Zwy7GipZuJhWk0Htik3J/[email protected]
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/c400-l
or email: C400-L-request-Zwy7GipZuJhWk0Htik3J/[email protected]
Before posting, please take a moment to review the archives
at http://archive.midrange.com/c400-l.

-- 
This is the Bare Metal Programming IBM i (AS/400 and iSeries) (C400-L) mailing list
To post a message email: C400-L-Zwy7GipZuJhWk0Htik3J/[email protected]
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/c400-l
or email: C400-L-request-Zwy7GipZuJhWk0Htik3J/[email protected]
Before posting, please take a moment to review the archives
at http://archive.midrange.com/c400-l.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.