× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Bob,
        Thanks for asking. Yes. After all of the great suggestions that I
got from this list I decided to do a "manual type" conversion both ways. (as
is in keeping with this company's attitude of "write so a junior programmer
can figure it out!!")
        Here are my little routines... (for reference... these binary
strings are generated by some sort of a battery tester device.)

Binary string '010101010' to a decimal number....
        MeasCCAd is the decimal number.
        MeasCCA is the binary string. 
        (Yes, CCA stands for Cold Cranking Amps!)
C                   Clear                   MeasCCAd       
C                   Eval      Divsr = 256                  
C     1             Do        9             Idx2           
C                   If        %SubSt(MeasCCA:Idx2:1) = '1' 
C                   Eval      MeasCCAd = MeasCCAd + Divsr  
C                   EndIf                                  
C                   Eval      Divsr = Divsr / 2            
C                   EndDo  

Decimal number to a binary string...
        VinB is the character string to hold the binary number.
        VinD is the decimal number being converted.
        (Yes, VIN stands for Vehicle Identification Number!)
C                   Eval      Divsr = 256               
C                   Clear                   VinB        
C     1             Do        9             Idx2        
C                   If        VinD / Divsr >= 1         
C                   Eval      %SubSt(VinB:Idx2:1) = '1' 
C                   Eval      VinD = VinD - Divsr       
C                   Else                                
C                   Eval      %SubSt(VinB:Idx2:1) = '0' 
C                   EndIf                               
C                   Eval      Divsr = Divsr / 2         
C                   EndDo                                          
                                                           
Thanks to everybody for your input.
Jerry


-----Original Message-----
From: Bob cozzi [mailto:cozzi@xxxxxxxxx]
Sent: Monday, June 09, 2003 9:12 AM
To: 'RPG programming on the AS400 / iSeries'
Subject: RE: Binary Characters to Decimal


Jerry,
As Hans pointed out strtol (or strtoll for larger numbers) converts from
binary notation 101101 to decimal, but did you ever find a way to go
backwards? I have an old RPGIII grunt method technique that I've been using
for 10 years, but I'd rather find a simpler method. Just wondering.

Bob Cozzi
Cozzi Consulting
www.rpgiv.com


-----Original Message-----
From: rpg400-l-bounces@xxxxxxxxxxxx [mailto:rpg400-l-bounces@xxxxxxxxxxxx]
On Behalf Of Hans Boldt
Sent: Friday, June 06, 2003 8:07 AM
To: rpg400-l@xxxxxxxxxxxx
Subject: Re: Binary Characters to Decimal

Feador, Jerry wrote:
> Hi All,
> 
>       I've been tasked with converting a character representation of a
> binary number to it's decimal equivalent and vice versa. For example I
have
> a series of nine characters '001011101' and I'm suppose to get the AS/400
to
> come up with 93. Or I have the decimal number 321 and I need to get to
> '101000001'
> 
>       I've checked the archives and all past information deals with the
> conversion of a true binary number to decimal, not this character
> representation of one....
> 
>       Is it possible to just move this string into a binary field and then
> to a regular numeric field?

For conversion of numbers in base 2 to decimal, try the C run-time 
library function "strtol()". Here's an example:

------------------------------------------------------------------
H dftactgrp(*no) bnddir('QC2LE')
D strtol          pr            10i 0 extproc('strtol')
D   nptr                          *   value options(*string)
D   endptr                        *   value
D   base                        10i 0 value
D ptr             s               *
  /free
     dsply (strtol('1': %addr(ptr): 2));
     dsply (strtol('10': %addr(ptr): 2));
     dsply (strtol('11': %addr(ptr): 2));
     dsply (strtol('100': %addr(ptr): 2));
     dsply (strtol('10010101': %addr(ptr): 2));
     *inlr = *on;
  /end-free
------------------------------------------------------------------

Cheers! Hans


_______________________________________________
This is the RPG programming on the AS400 / iSeries (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.



_______________________________________________
This is the RPG programming on the AS400 / iSeries (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.

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-2024 by midrange.com 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.