Re: Converting packed decimal to a char array

Tim Bronski <[email protected]>
Newsgroups gmane.comp.lang.as400.c
Message-ID <[email protected]>
It works with all decimal lengths and positions. The move to a (30,0) 
variable is because you need a common length for sprintf (or printf) but 
the decimals can vary.

On 3/11/2015 1:21 PM, Jevgeni Astanovski wrote:
> Hi, Tim.
>
> I have to experiment with it before I believe it works with different
> decimal types.
> Hope it does :-)
> Of course in this case probably decimal(30,0) is not enough in general...
>
> Jevgeni.
>
>
>
> On Sun, Mar 8, 2015 at 3:11 PM, Tim Bronski <[email protected]> wrote:
>> Hi Jevgeni,
>>
>> Sorry, I got too busy last week to look at this. Here's one way to do what
>> you're trying to do:
>>
>>
>> void printdec(void *A, int Len, int Prec)
>> {
>>   decimal(30,0) fulldec;
>>   int offset;
>>    // Move packed number to a 30,0 variable.
>>    offset=16-Len/2;
>>    if (Len & 1)
>>      offset--;
>>    fulldec=0;
>>    memcpy(((char *) &fulldec)+offset, A, Len);
>>    // Now print it.
>>    printf("%D(30,*)\n", Prec, fulldec) ;
>>   }
>>
>>
>>
>> On 3/4/2015 4:46 PM, Jevgeni Astanovski wrote:
>>> Tim,
>>> length and precision are not a problem. The question is how to pass
>>> this decimal value to the function.
>>> The only way that I can imagine is as a void pointer. But you cannot
>>> apply indirection to void pointers AFAIK.
>>> The below program does not compile:
>>>
>>> void printdec(void *A, int Len, int Prec) ;
>>>
>>> int main ( )
>>> {
>>>     decimal (15,9) A ;
>>>
>>>     A = 17.5D ;
>>>     printdec(&A, digitsof(A), precisinof(A)) ;
>>>     return(0) ;
>>> }
>>>
>>> void printdec(void *A, int Len, int Prec)
>>> {
>>>     printf("%D(*,*)\n", Len, Prec, *A) ;
>>> }
>>>
>>>
>>>
>>> On Fri, Jan 9, 2015 at 5:25 PM, Tim Bronski <[email protected]> wrote:
>>>> Well your function needs to know the length and decimal places. So if you
>>>> know those and can pass them to the function then just build the format
>>>> string before using it in sprintf.
>>>>
>>>> On 1/9/2015 10:00 AM, Jevgeni Astanovski wrote:
>>>>> 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.
>>>>
>>>> --
>>>> Need secure FTP? Download your native sFTP solution here:
>>>> www.arpeggiosoftware.com
>>>> --
>>>> 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.
>>>>
>> --
>> Need secure FTP? Download your native sFTP solution here:
>> www.arpeggiosoftware.com
>> --
>> 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.
>>

-- 
Need secure FTP? Download your native sFTP solution here: 
www.arpeggiosoftware.com
-- 
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.