fhg
Volume 6, Number 39 -- October 25, 2006

Stuffing Five Digits Into Four, Take 2

Published: October 25, 2006

Hey, Ted:

Your recent article, Stuffing Five Digits into Four, is a trip in the way, way back machine for me. I had to do this very thing on System/3 in RPG II! I'm writing to let you know that there is a way to stuff those five digits into four using native I/O.

As you've discovered, RPG's 'B' data type limits you to 4 digits; it's in the manual, but that's where data types 'B' and 'I' differ. 'B' is intended to preserve x digits, from 0000 to 9999 (including sign) but the newer 'I' is intended to emulate the C world's maximum value based on number of bits used for storage.

You may be scratching your head as to why the heck I'm babbling about 'I' data type. Well, using RPG IV's H specification EXTBININT(*YES), we can make RPG read and write those binary fields as though they were integers.

Note that we still can't read/write a full 5 (or 10) digits; integer storage is based on the number of bits available, so a B4 can only hold 15 bits (minus one) of information, along with the sign (16th bit) or 32767. A normal RPG packed-decimal, five-digit number could hold a number as large as 99999 (with sign.) So there's still a limitation on the range of numbers that can be stored in a DDS B type field.

Here's a little example that may help. First, consider the DDS for a physical file BINARY, which contains binary fields.

A          R SAMPLER             
A            KEY            5S 0 
A            B4             4B 0 
A            B9             9B 0 
A            TEXT          50A

Now, let's put some data in the file. First, we use the SQL method you wrote about.

INSERT INTO "BINARY" VALUES(4, 32767, 2147483647, 'Inserted via sql')

Now, let's use native I/O.

h extbinint(*yes) debug                                
fqad1000pf uf a e             disk                     
                                                       
c/free                                                 
      *inlr = *on;                                     
      read sampler;                                    
      dump;                                            
      key = 1;                                         
      text = %char(%len(b4)) + ', ' + %char(%len(b9)); 
      dsply text;                                      
      b4 = (2**15)-1;                                  
      b9 = (2**31)-1;                                  
      text = 'test with extbinint(*yes)';              
      write sampler;                                   
      return;

Note that the dump will show the full values.

Use SQL to take a look at the data that was inserted and you'll see that both methods do the trick.

select * from binary

KEY       B4              B9   TEXT                     
===  =======   =============   =========================
  4   32,767   2,147,483,647   Inserted via sql         
  1   32,767   2,147,483,647   test with extbinint(*yes)

Nice tip--it might be a great lead-up to an explanation of binary versus integer, and why we should never use the B data type in RPG IV (APIs want/supply integer data, and B will truncate.)

--Buck Calabro


I want to thank Buck for this information and for his excellent example. My thanks also to Barbara Morris (of IBM), Birgitta Hauser, Larry Hegler, " rgad", and Kevin Wright for giving me the same information. I am thrilled to learn I was mistaken, because I happen to need to use native I/O to stuff five digits into a four-digit binary field in a project I'm currently working on.

I often feel that I need to know more than I am capable of knowing. There is so much information to keep up with. Am I alone?

Also, the article that Buck recommends I write was actually written more than three years ago in this newsletter. You can see it here: When a 10-Digit Variable Won't Hold a 10-Digit Number.

--Ted



Sponsored By
PROFOUND LOGIC SOFTWARE

RPGsp - The Best of Both Worlds

The most comprehensive iSeries Web Development Environment for both building new Web apps and converting green-screens.

That's right! Use RPGsp to instantly convert any existing RPG/CL programs to a great-looking CGI application.

Or point RPGsp to your database files and instantly build new Inquiries, Drill-downs, File Maintenances, and other types of new Web applications.

The most comprehensive iSeries Web Development Environment for both building new Web apps and converting green-screens.

· WYSIWYG HTML Designer
· Integrated Visual Debugger
· Built-in Source Control
· Customizable Themes and Templates
· Easy to use Wizards
· Automated Server Configuration

Click here to download RPGsp.
Click to see videos of RPGsp in action!

Call 1-877-224-7768 or visit www.RPGsp.com



Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

nuBridges:  Leading provider of secure FTP on the iSeries
Magic Software Enterprises:  Design IT, Control IT, Monitor IT with iBOLT
COMMON:  Join us at the Spring 2007 conference, April 29 – May 3, in Anaheim, California

 


 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement