Guidance Forums / Reginald Rexx / Hex constant

Search 搜索
Home Home

Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Hex constant
Posted by: guidance 2002-11-01 13:57:35
From the online REXX book (, it notes that we have to use X2D() function to specify a hex constant in the program. Does this cause extra machine time?

For example, if I write
many times, or in a loop, to specify a hex constant 11H, does Reginald convert it every time? Is it necessary to convert to decimal internally?
In C, if I write 0x11, the compiler will convert that constant into an internal binary number when compiled.

If I try:
SAY n + 1
then REXX reports:
Line #2: Bad arithmatic conversion

The value of n is the ascii character 'J'. REXX doesn't allow a character in an arithmatic calculation, unlike C. So how can I easily express (in REXX) the following C instruction?
f = 0x11 * a + 12/b - 'a'
Would it be (in REXX):
f = X2D('11') * a + 12/b - C2D('a')
Message2. Re: Hex constant
Posted by: 2002-11-02 05:36:28
We have to use X2D() function to write hex constant.


And yes, a call to X2D() may incur some extra overhead.

But as long as you know exactly what hex value you're putting in your script (because after all, it is a constant and therefore never changes), why not convert it to decimal when you write your script? Do the same with any ascii character that is expressed as a constant -- convert it to its decimal value when you write the script.

So for example:
f = 0x11 * a + 12/b - 'a'
... becomes...
f = 17 * a + 12/b - 97  /* 0x11 = 17, and 'a' = 97 */
This eliminates the calls to X2D('11') and C2D('a').

In conclusion, express all of your constants in decimal when you write your script if you want to avoid extra calls to X2D() or C2D() (or maybe even B2X() too if you try to express a constant in binary).

The conversion functions such as X2D(), C2D(), etc, are really intended for situations where you have some arbitrary (not a constant) hex value, or arbitrary ascii character that needs to be converted for use in a math expression. For example, maybe you ask the user to enter any hex value, which you stuff into some variable. (ie, You use PULL to get his hex value). Now you want to use that hex value in a math expression. You don't really know what exact hex value he'll enter, so you need to convert it on-the-fly. That's where X2D() comes in. You throw the hex value at X2D() and it spits out the equivalent decimal value for use in any math expression.

If I try:
say n + 1

It reports
Line #2: Bad arithmatic conversion

Yes. This is because '4A'x is not the same as X2D('4A').

Appending an X to a REXX literal string is not the same thing as putting a 0x in front of number in C. They are entirely different things (although I suppose you may think they look somewhat similiar). On the other hand, X2D('4A') is the same thing as 0x4A in C. The C compiler converts 0x4A to an appropriate format for use in math expressions. The REXX interpreter uses X2D() for the same purpose.

Appending an X to a REXX literal string means that you want the REXX interpreter to embed a special character (or more than one character) right into your REXX script when the script runs. You want to express that character in hexadecimal. (Normally, all the characters you enter in your text editor are ascii characters. But sometimes, you may want a special character inserted such as a formfeed, or newline, or tab, etc when your script runs).

So '4A'X means that you want REXX to embed a character in your script that has the hex value of 4A. That character just so happens to be a 'J' character. That is what REXX is really inserting into your script in place of '4A'X. So you're trying to do the math expression 'J' + 1 which is illegal in REXX because REXX supports math only with decimal values (ie, values that contain the numeric digits '0' to '9', and perhaps one decimal point '.', and an exponent 'E', and a leading minus '-' or plus '+' sign).

There are some useful special characters that you can embed in your script by appending an X to a literal string. For example, '0A'x is a newline character. Let's say that you want to print two lines to the screen. You can do this:
SAY "Line 1"
SAY "Line 2"
REXX's SAY will automatically put a newline at the end of whatever it SAYs. So above, you see "Line 1" and "Line 2" upon separate lines.

But let's assume you want to print both of those lines to the screen using just one SAY command. You try this:
SAY "Line 1" "Line 2"
That prints "Line 1" and "Line 2" on the same line, with just one space inbetween.

What you need to do is tell REXX to insert a newline character inbetween "Line 1" and "Line 2". You can do this by putting '0A'x there as so (in place of the space inbetween):
SAY "Line 1" || '0A'x || "Line 2"
Now "Line 1" and "Line 2" are printed upon separate lines.

Sometimes, the Windows operating system requires both a newline and a linefeed character together. This is accomplished by putting both special characters there (where '0D'x is the linefeed):
SAY "Line 1" || '0D'x || '0A'x || "Line 2"
If you have two hex literal strings side by side, you can combine them into one string as so:
SAY "Line 1" || '0D0A'x || "Line 2"
But remember, appending an X simply means that REXX is embedding a special character at some point in your script, and you're expressing it in hex. This does not mean that you're expressing a value that is useable in a REXX math expression. Hex values are not useable in REXX math expressions. They must be converted to decimal.
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
© Sat 2023-12-9  Guidance Laboratory Inc. Hits:0