HPlogo DATA TYPES CONVERSION Programmer's Guide: HP 3000 MPE/iX Computer Systems > Chapter 3 Converting Data Types

Converting from Character:

» 

Technical documentation

Complete book in PDF
» Feedback

 » Table of Contents

 » Index

This section offers suggestions about converting from one character set to another, and from ASCII (character) numbers to numeric data types.

To Other Character

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.

Between ASCII and EBCDIC

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

Between Native Languages

The Native Language Subsystem (NLS) supports six character sets containing the following native languages:

  • USASCII supports NATIVE-3000, an artificial language

  • ROMAN8 supports:

    • NATIVE-3000

    • American English

    • Canadian French

    • Danish

    • Dutch

    • English

    • Finnish

    • French

    • German

    • Italian

    • Norwegian

    • Portuguese

    • Spanish

    • Swedish

    • Icelandic

  • KANA8 supports:

    • NATIVE-3000

    • KATAKANA

  • ARABIC8 supports:

    • Arabic

    • Western Arabic

  • Greek8 supports Greek

  • Turkish8 supports Turkish

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

Between Numeric Formats

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:

  • NLCONVNUM takes data in a foreign language (one that is supported in NATIVE-3000) and translates it to the system's native language. It accepts a foreign language number with decimal and thousands separators and returns an ASCII number with NATIVE-3000 decimal and thousands separators. As an option, the decimal and thousands separators can be stripped.

  • NLFMTNUM takes native language data and translates it to a foreign language that is supported by NATIVE-3000. It accepts an ASCII number string, which may include NATIVE-3000 decimal separator, thousands separator, and currency symbol or name. It returns a string with the same number, but formatted with the decimal separator, thousands separator, and currency symbol or name of the native language you specify.

  • NLNUMSPEC accepts a string as a logical byte array (minimum 60 bytes) and a language identification number. It returns the string encoded with information (number specifications) about the characteristics of the language, such as

    • Direction (left-to-right or right-to-left),

    • Digit range.

    • Symbols for digits, currency, mathematical operations, and thousands and decimal separators.

For further information about the Native Language Subsystem, refer to Native Language Programmer's Guide (32650-90022).

To Integer

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.

To Real

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.

To Packed Decimal

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.

Feedback to webmaster