|
|
DATA TYPES CONVERSION Programmer's Guide: HP 3000 MPE/iX Computer Systems > Chapter 3 Converting Data TypesConverting from Character: |
|
This section offers suggestions about converting from one character set to another, and from ASCII (character) numbers to numeric data types. Since all characters are 8 bits long and all are byte-aligned, there is no incompatibility between between languages or environments. You may want to convert between different character sets. ASCII and EBCDIC are the supported English character sets; several native language character sets like the Japenese EBCDIK and JISCII are also supported. The CTRANSLATE intrinsic accepts a string of characters in either ASCII or EBCDIC, and returns a string translated into the other. The translation string is returned the same buffer unless you specify another. ASCII and EBCDIC are described in Chapter 2 “Formatting Data Types” of this book, and their code equivalents are in Appendix A The Native Language Subsystem (NLS) supports six character sets containing the following native languages:
Use the intrinsic NLINFO or generate a report from the NLUTIL utility to review information about a character's native language format. For more information, refer to MPE XL Intrinsics Reference Manual (32650-90028). Use the intrinsic CTRANSLATE to translate between EBCDIK and JIS(KANA8). EBCDIK is a Hewlett-Packard's Japanese version of EBCDIC. JIS is a Japanese International Standard code (JISCII is a Japanese version of US ASCII); KANA8 is an 8-bit JIS code. Use the intrinsic NLREPCHAR to replace nondisplayable characters. It accepts a character array containing nondisplayable characters and returns another character array with replacement characters. (NLINFO can tell you if the character is nondisplayable because it is a control code or an undefined graphic character). If you are sending data files to, or receiving data from, a foreign country, you may discover that numbers are represented differently in different native languages. The following intrinsics are used for converting ASCII numbers between native language formats:
For further information about the Native Language Subsystem, refer to Native Language Programmer's Guide (32650-90022). Individual languages may have simple assignment commands to accept characters and interpret the symbol as its value. For example, STRREAD in Pascal or READ in FORTRAN will read an ASCII number and format a variable as the value the character string represents. The intrinsic DBINARY accepts an octal-based, a hexadecimal-based, or a signed decimal-based number in ASCII characters, and returns a 32-bit binary integer in twos complement (signed) form. A character string beginning with a percent sign (%) is treated as octal (base 8) value. A string beginning with a dollar sign ($) is treated as a hexadecimal (base 16) number. A string begining with a plus sign (+), a minus sign (-), or a number (1 through 9, no leading blanks allowed) is treated as a decimal (base 10) number The intrinsic BINARY performs a similar operation; it converts a numeric ASCII string into a 16-bit binary value. A parameter indicates the number of input bytes. The compiler utility procedures, EXTIN ' (CM) and HPEXTIN (NM) can also be used to convert from ASCII to integer. Any fractions in the input number string will be truncated in integer results. As with converting from character to integer (above), the simplest way to convert character to real type may be an assignment statement within the language. The compiler utility procedures EXTIN ' (CM) and HPEXTIN (NM) accept a byte array (a character string of ASCII digits passed by reference) and convert it into an internal representation. Leading blanks in the input string are ignored. A parameter can be set to treat trailing blanks as zeros. Dollar signs and other currency symbols and commas for thousands separators are counted in the input length, but are ignored in the output. In the datatype parameter, you specify which internal representation you want: an integer of 16 bits or 32 bits, or a real of 32 bits or 64 bits. The result parameter is a 32-bit pointer to the first word of result storage, according to the type specified. Other parameters let you specify field width, decimal places, exponents, and fractions. In MPE V, system instructions use packed decimals; for compatibility, MPE XL has compiler library procedures that run in NM and emulate the MPE V instruction set. In MPE XL, the packed decimal type is used only in COBOL or RPG, however. Within COBOL and RPG, use the MOVE command to convert easily between types by assignment. The NM compiler procedure HPPACCVAD accepts ASCII digits and returns the packed decimal digits used by MPE V and in COBOL and RPG. The rightmost source digit indicates the sign; all other digits must be unsigned or be leading blanks. Blanks between digits are illegal. Leading blanks are converted to packed-decimal zeros. An all-blank field converts to an unsigned zero. If the source has more digits than the target, the result is left-truncated; if the target is the larger, the result is padded with zeros on the left. Because this procedure is external and general, it may not be as efficient as code optimized by the NM COBOL compiler. Packed-decimal procedures must be declared as intrinsics to be called from within high-level NM languages. If speed is a primary concern, consider doing packed-decimal operations within HP COBOL II/XL or HP RPG/XL. |
|