| 
 | 
Jerry - here is a simple program that may help you...it will convert the character representation of a binary value to decimal, or it will convert a decimal value to the equivalent binary value in character form. Some of the other guys here may have a more elegant solution, but you can't complain about the price (free)... HTH, Steve Landess Austin, Texas (512) 423-0935 ----- Original Message ----- From: "Feador, Jerry" <JFeador@xxxxxxxxxx> To: "'RPG programming on the AS400 / iSeries'" <rpg400-l@xxxxxxxxxxxx> Sent: Thursday, June 05, 2003 5:03 PM Subject: Binary Characters to Decimal > > 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? > > Help... > > Thanks, > > > Jerry L. Feador > Sr. Programmer/Analyst > Kia Motors America, Inc. > (949) 595-5810 > _______________________________________________ > 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. > >
      *************************************************************
      *
      *  Program name:  CVTBIN
      *        Author:  Steve Landess
      *          Date:  6/5/2003
      *       Purpose:  This program will convert a Binary value
      *                 (passed as a character value) to Decimal,
      *                 or it will convert a Decimal value to
      *                 binary (as a character representation).
      *
      *  Parms:
      *  1)  BINFLD is a 16-byte alphanumeric field
      *  If you pass the character representation of a binary value
      *  in BINFLD, it will be converted to its decimal equivalent
      *  and returned in DECFLD.
      *
      *  2) DECFLD is a 15.5 numeric field
      *  If you pass a decimal value in DECFLD, the binary
      *  equivalent (in character form) will be returned in BINFLD.
      *
      *  *** NOTE ***
      *  As written, the maximum Decimal value that can be
      *  converted by this program is 65535.
      *
      *************************************************************
     D BINFLD          DS            16
     D BIN                            1    DIM(16)
     D DECFLD          S             15  5
      *
     D TWO             S              3  0 INZ(002)
     D WORK5           S              5  0 INZ(*ZEROS)
     D REMAINDER       S              5  0 INZ(*ZEROS)
     D INDEX           S              3  0 INZ(*ZEROS)
     D EXPONENT        S              3  0 INZ(*ZEROS)
     D DIVISOR         S              5  0 INZ(*ZEROS)
     D RESULT          S              5  0 INZ(*ZEROS)
      *
     C     *ENTRY        PLIST
     C                   PARM                    BINFLD
     C                   PARM                    DECFLD
      *
      *    Convert Binary to Decimal:
      *
     C                   SELECT
     C     BINFLD        WHENNE    *BLANKS
     C                   EVAL      DECFLD = *ZEROS
     C                   EVAL      EXPONENT = 0
     C                   EVAL      INDEX = 16
      *
     C     1             DO        16
     C                   IF        BIN(INDEX) = '1'
     C                   EVAL      DECFLD = (DECFLD + ( TWO ** EXPONENT ))
     C                   ENDIF
     C                   EVAL      INDEX = INDEX - 1
     C                   EVAL      EXPONENT = EXPONENT + 1
     C                   ENDDO
      *
      *    Convert Decimal to Binary:
      *
     C     DECFLD        WHENNE    *ZEROS
     C                   EVAL      BINFLD   = *BLANKS
     C                   EVAL      EXPONENT = 15
     C                   EVAL      WORK5    = DECFLD
      *
     C     1             DO        16
     C                   EVAL      INDEX = INDEX + 1
     C                   EVAL      DIVISOR = (TWO ** EXPONENT)
     C     WORK5         DIV       DIVISOR       RESULT
     C                   MVR                     REMAINDER
     C                   IF        RESULT <> 0
     C                   MOVE      '1'           BIN(INDEX)
     C                   EVAL      WORK5 = REMAINDER
     C                   ELSE
     C                   MOVE      '0'           BIN(INDEX)
     C                   ENDIF
     C                   EVAL      EXPONENT = EXPONENT - 1
     C                   ENDDO
      *
     C                   ENDSL
      *
     C                   EVAL      *INLR = *ON
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 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.