Question

How does packed decimal translate to ASCII?

Asked by: xenium

Hi,
Simple follow-up question.

If i take the decimal number 98 and pack it into 1 byte i understand that:

9 becomes the firstpart of the byte (1st nibble)
8 becomes the second part of the byte (2nd nibble)

What will the ascii value of this byte be? How is this determined based on the codepage? (i know nothing of codepage)

Can you give me 2 examples of how this number would be translated?

If nothing else the complexity might make me drop the topic, but i'm stubborn.

Cheers

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-02-14 at 10:56:30ID21736456
Tags

packed

,

decimal

Topic

Programming for iSeries / AS400

Participating Experts
2
Points
125
Comments
7

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. ASCII character number in decimal form
    How do I get the ascii character code number for a character e.g. what is the code for QString "hello" decimal number for h is 104 decimal number for e is 103 ...... regards, Gordon.
  2. ASCII foramt
    I need someone to translate the following into English: DCC SEND filename.zip 3640377013 5107 The address and port should be sent as ascii representations of the decimal integer formed by converting the values to host byte order and treating them as an unsigned long and uns...
  3. convert character to ASCII equivalent decimal
    Greetings, How do i convert a character to its ascii equivalent decimal value in VB.net. Pls Help. Thank you.

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: xeniumPosted on 2006-02-14 at 10:58:50ID: 15953637

Q follows from: http://www.experts-exchange.com/Programming/Programming_Platforms/AS400/Q_21735798.html

Note: question is for mainframe packed data, but i think the logic is generic, or is it?
Anyone suggest a better topic area for this?

 

by: tliottaPosted on 2006-02-14 at 12:19:46ID: 15954364

xenium:

Packed data is a form of BCD (Binary Coded Decimal). While a more pure form of BCD can 'pack' the value 98 into a single byte, the packed-decimal form would usually require two bytes. This is because the last nibble will contain the sign of the value and therefore move each decimal digit one nibble to the left. The high-order nibble would be set to a leading '0'. The result would be:

  08
  9F

The first byte is x'09' and the second byte is x'8F'. Together they're x'098F'. I used a x'F' as the sign nibble because there is no indication of positive/negative. The x'F' essentially indicates an absolute value; effectively it's a positive number. More precisely, a x'C' is designated as positive and x'D' is negative.

The value 98 is two digits and takes up two packed bytes. Not particularly an efficient use of the space. But the two packed bytes can hold a range of values from -999 to +999. If 16 bytes were used instead of two bytes, the range of values would be -9(31) through +9(31) where the (31) represents the number of digits. Sixteen bytes is 32 nibbles; minus a single nibble for the sign gives 31 digits. Each digit holds a value from 0 through 9.

Now, if you took those same 16 bytes and used them to store binary integers, you could have a larger range of values; but you run into a problem. There is no precise way to represent decimal _fractions_ in fields formatted as binary integers. Packed-decimal becomes particularly useful when _exact_ representations of decimal fractions is necessary. Most common in business is the representation of fractions of a dollar (cents).

The field definition of packed-decimal then has three general parts: (1) number of total digits, (2) signed/unsigned, and (3) the number of assumed decimal fraction digits. For a monetary field of dollars/cents, the assumed number of fractional digits would usually be '2'.

CPUs that handle packed-decimal have often had basic BCD processing modes. That tends to make them more efficient for business programming where money calculations are prevalent.

Okay, now 'conversion to ASCII'.

Well, usually there is no conversion to ASCII of packed-decimal data. Conversion to ASCII is generally for text data and a packed-decimal field would be considered binary/non-text data. In order to convert to ASCII, you would want to convert the packed data to characters first.

So, assuming a packed-decimal field of three digits and a single assumed decimal position and holding the value x'098D', then the character representation would be [9.8-], i.e. a (negative 9.8)... without the square brackets of course.

An ASCII representation of text [9.8-] is the same as it always is.

In OS/400, one way of handling this conversion more or less automatically is through the use of the CPYTOIMPF (Copy to Import File) command. This command can take a DB2 database file and do the conversions needed to create an ASCII text version. Various command parameters describe the characteristics of the resulting, e.g., whether or not a .CSV format should be created. That version would commonly then be FTPd or otherwise copied to an ASCII system.

Tom

 

by: xeniumPosted on 2006-02-15 at 10:26:07ID: 15963097

Hi tliotta,

Wow thanks for the explanation. I'm missing just one step:

Imagine I download this file to a PC without unpacking. I read the file byte by byte  - what 2 ascii values will I see for the example [9.8-].  For sure I do not see [9.8-].  In understand it depends on the code page or something that presumably effects a translation during the download.

Cheers

 

by: tliottaPosted on 2006-02-15 at 13:51:21ID: 15965364

xenium:

It will depend on what tool is used to do the 'download'. For example, iSeriesAcces File Transfer can do automatic conversion. But FTP is a cross-platform standard that has no concept of formatted records that consist of multiple fields; FTP is line oriented. If the FTP transfer is done as text rather than binary, then each EBCDIC character code will be converted to the corresponding ASCII code.

What you need is an EBCDIC/ASCII translation table. They _used_ to be simple; but then Microsoft and Unicode, as well as IBM and internationalization and CCSIDs and every other vendor, really started confusing us all. Maybe you're lucky, maybe this is an example of a table that will help:

http://publib.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/QB3AUT02/17.1?DT=19980511095346

It's from before when all the changes started to affect programming, but it is still accurate for its scope.

Starting from the packed x'098D' which is the numeric value [98-], we get two bytes -- x'09' and x'8D'. Looking down the table, we see that x'09' translates to an ASCII HT character (Horizontal Tab, I think). Going down farther, we see that x'8D' in EBCDIC converts to... well, there is no defined 7-bit ASCII character that matches x'8D'.

That automatically complicates matters. Now it depends on what conversion tables you have defined for FTP to use. Under OS/400, this can be seen by looking at the CCSID() or TBLFTPOUT() parameters of the FTP or STRTCPFTP commands. By default, OS/400 will use the conversion table that is appropriate to convert between the CCSID of the database file and CCSID 00819 which is ISO 8859-1 8-bit ASCII. Without knowing the CCSID of the database file, there's no way to know the conversion.

Most likely, your system is defined for either CCSID 65535 or CCSID 37, assuming the USA. If 65535, then your job default CCSID will be 37 (if USA English).

So, the conversion table will be according to the defined conversion between CCSID and CCSID 819. One example seems to be:

http://www.rpgiv.com/cgi-bin/showascii

That shows that x'09' will become x'8D' and x'8D' will become x'FD'. (Interesting coincidence for x'09'->x'8D'.)

I'm not sure how much farther I can go. A bunch of things start depending on system configuration, user setup, characteristics of the file being transferred, etc.

Tom

 

by: xeniumPosted on 2006-02-16 at 07:56:39ID: 15971951

Well you've impressed the hell out of me - i'll keep this for reference when I'm less employed or have some side-resource, but for now I'll put it on hold...as i said:

"If nothing else the complexity might make me drop the topic"

Many thanks

 

by: tliottaPosted on 2006-02-16 at 11:42:31ID: 15974379

xenium:

I probably know a few percent more than the average developer who is involved in multiple file transfers. But I am far from certain ground.

The whole MS/Unicode and IBM/CCSID (and both are very much the same if you use them the same) deal is all because of the rapid rise of the internet. Rapid info transmission across language boundaries forces many of us to grapple with it without an education background for it. (It sure wasn't any college course that I had.)

For the most part, you do it just like you did. Find someone who will answer questions.

Where possible, you find tools such as the OS/400 CPYTOIMPF command to do it for you. Under many circumstances, you might create an SQL VIEW that will provide numeric<->character translation and then FTP the view rather than the physical table. In short, you find whatever works and use it.

Tom

 

by: SRG041808Posted on 2008-04-24 at 07:10:58ID: 21431010

I just want to say that I was very impressed with Tom's explanation too.  I've been all over the internet (including Experts pages) and could barely understand some of the explanations for the Packed field.  I need to write some code to unpack these fields - and because I didn't truly understand how they were created.. I couldn't quite understand the explanations of how to unpack them.. But Tom's 'solution' was amazing!!  (You don't have to be an expert to understand him :-)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...