Stuffing Five Digits Into Four, Take 2
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 |