× 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.



At 05:03 PM 6/5/03, you 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?

Not really. But it's pretty easy to check each '1' or '0' and calculate it up. See below.



Help...

Thanks,


test.rpgle

h dftactgrp(*no)

D DEBUG c const(1)

     D char2dec        pr            15p 0
     D  binstring                   256    const

     D  test0          c                   '0'
     D  test1          c                   '1'
     D  test2          c                   '11111111'
     D  test4          c                   '010101010101111010101010111101'

D somenum s 15p 0

      /free
          // do the conversion
          somenum = char2dec(test0);
          somenum = char2dec(test1);
          somenum = char2dec(test2);
          somenum = char2dec('1111 1111');
          somenum = char2dec(test4);

          eval *inlr = *on;
      /end-free

      * Convert a bit string into its decimal number
     P char2dec        b
     D char2dec        pi            15p 0
     D  binstring                   256    const

     D  max            c                   const(256)
     D  i              s             10i 0
     D  multiplier     s             15p 0 inz(1)
     D  bindigit       s              1
     D  result         s             15p 0 inz
     D  resultc        s             15

      /free
         for i = max downto 1;
             bindigit = %subst(binstring:i:1);
             select;
                 when bindigit = '1';
                     result = result + multiplier;
                     multiplier = multiplier * 2;
                 when bindigit = '0';
                     multiplier = multiplier * 2;
             endsl;
         endfor;

         // display the results
         if DEBUG = 1;
              resultc = %char(result);
              dsply resultc;
         endif;

return result;

      /end-free
     P char2dec        e


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

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.