Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string to numeric

236 views
Skip to first unread message

Dan Samber

unread,
Dec 29, 1995, 3:00:00 AM12/29/95
to
Is there a function that can take a variable declared like

data = 123

and convert it so that the variable's value is actually 123
(that's the number 123, NOT a character string consisting
od ASCII 1 followed by ASCII 2 followed by ASCII 3... which
I think is 31, 32, 33)

I need to pass these variables as integers to a device driver.

In fact, REXX must have this built in, otherwise how could it
do addition with variables saved as strings?

Any help appreciated,


Thanks


Dan

Jeff Hennick

unread,
Dec 29, 1995, 3:00:00 AM12/29/95
to
At 01:46 PM 12/29/95 -0500, d...@camelot.rockefeller.edu wrote an e-mail
to me on this:

>Thanks for your reply, but maybe I should tell you the whole story.
>I've written a physical device driver (with Mastrianni's help) and
>decided to use rexx as the interface. Communication between rexx
>and the PDD is thru IOCTLs (with the help of Dave Boll's rxu api
>wrapper functions).
>
>But the issue is that I would ideally like to pass a "structure"
>(compound variable whatever) to the PDD. The IOCTL api call allows
>you to pass the address of what it presumes to be the array. I naively
>tried passing the stem name but that won't work...then I decided
>to just pass the strings... but it gets a little kludgy, then I
>looked at a bunch of stem to structure functions (also in Boll's
>rxu stuff)... finally I tried a naive approach of just assigning
>a variable like :
>
>data = "1234"x
>and passing its address to an unsigned int in the PDD. This worked OK
>except that there seems to be some byte swapping going on. (It
>arrives at the PDD as 0x3412) this is curious and probably has something
>to do with 32bit to 16:16 but I can switch it around, its not a big deal.
>
>Then I tried :
>
>data1 = "1234"x
>data2 = "5678"x
>data = data1 || data2
>
>and passed data to a structure in the PDD declared as 2 unsigned ints.
>It worked great and I thought I was home free.
>
>BUT
>
>the numbers I really want to concatenate together are not explicitly
>created by me as I did above but are returned from functions and are
>consequently (apparently) stored as strings.
>
>I guess I could have saved both of us alot of time by just asking:
>Is there a string to numeric conversion function. (I have tried
>the set of c2d etc and they dont seem appropriate.
>
>In fact I may post this last question.... but if you know of anything...
>
>HELP.
>
>Thanks
>
>Dan

Dan,
With no experience with your specific applications...

Assume mode on.
(1) the functions are returning a "string" of decimal digits, e.g., "12"
or "1234"
(2) you want these to be hexified, e.g., "C" or "4D2"
A mode off.

Rexx _automatically_ handles the string-to-decimal stuff (and the
reverse) all the time.

So your "problem" really is decimal-to-hex, or
hex_value = D2x(decimal_value)

These kind of things should do it:

dtemp1 = function1(...)
dtemp2 = function2(...)
dtemp3 = temp1 || temp2
xtemp = D2x(dtemp3)
rc = IOCTLfunc(xtemp3)

This "long form" lets you "see" exactly what is happening when run with
Trace ?R and is also easier to write and read correctly!

Or, a little faster in execution and using less memory:

rc = IOCTLfunc(D2x(function1(...) || function2(...)))

Hope this is a little clearer than the previous.


--
Jeff Hennick
JHen...@Delphi.Com (temp:) JHen...@MCI.NewsCorp.Com

Luis Paulo

unread,
Dec 29, 1995, 3:00:00 AM12/29/95
to
Dan Samber (d...@camelot.rockefeller.edu) wrote:
: Is there a function that can take a variable declared like

: data = 123

: and convert it so that the variable's value is actually 123
: (that's the number 123, NOT a character string consisting
: od ASCII 1 followed by ASCII 2 followed by ASCII 3... which
: I think is 31, 32, 33)

Rexx has six functions to convert values between decimal, hexadecimal
and character. They are: c2d(), d2c(), c2x(), x2c(), x2d(), d2x().
For your question, use decimal to character d2c().

Try the following:
data=123
data_char=d2c(data)
and you will have your integer value on the variable data_char.
You can also do directly data_char=d2c(123).
Rexx does not store numbers as integers and it does no mathematics
using integers. It does accounts like we learn in school, digit by
digit. The advantage is you have no limit to the precision.
By default, REXX works with nine digits for the numbers, but you can
override it and do whatever precision you want.
Try this:

numeric digits 999999 /* tells REXX to use 999999 digits for numbers */
aaa = 7652376512736517657865977566876876765 / 3325876876892228726666
say aaa

if you have enogh memory, you will get lots of screens filled with numbers!

Hope this helps.

Luis Paulo


Patrick Herring

unread,
Dec 30, 1995, 3:00:00 AM12/30/95
to
d...@camelot.rockefeller.edu writes in article <DKD30...@rockyd.rockefeller.edu>:

>
> Is there a function that can take a variable declared like
>
> data = 123
>
> and convert it so that the variable's value is actually 123
> (that's the number 123, NOT a character string consisting
> od ASCII 1 followed by ASCII 2 followed by ASCII 3... which
> I think is 31, 32, 33)
>
> I need to pass these variables as integers to a device driver.
>
> In fact, REXX must have this built in, otherwise how could it
> do addition with variables saved as strings?

Rexx's arithmetic is defined and implemented as operations on strings, believe
it or not :-) . This gives a lot of portability at the expense of speed.

I think what you mean is 123 as it would be physically in C ie in binary. The
function d2x( 123 ) gives this (7B).

Yours, Patrick
__________________________________________________________________

Patrick Herring, Primrose Hill, London, UK
I tend to eat my UUCP feed once a day so replies can take two days

Andrew J Armstrong

unread,
Jan 5, 1996, 3:00:00 AM1/5/96
to
d...@camelot.rockefeller.edu (Dan Samber) wrote:

>I need to pass these variables as integers to a device driver.

Dan,

To convert a numeric string to a two-byte integer you should use:

nNumber = '1234' /* = '31323334'x in ASCII */
cInteger2 = d2c(nNumber,2) /* = '04D2'x */

If you are passing this integer to an assembly language routine on a
big-endian machine (eg IBM S/390) then this is fine. If the machine
is little-endian (eg Intel 80x86) you will have to reverse the bytes
first:

cInteger2 = reverse(d2c(nNumber,2)) /* = 'D204'x */

Explicitly specifying the length ensures that a short (80x86) int is
always generated, otherwise REXX will use the mininum number of bytes
to store the result. A long (80x86) int would be generated by:

cInteger4 = reverse(d2c(nNumber,4)) /* = D204'0000'x */

I don't want to labour the point, but using d2x is inappropriate since
it converts a decimal string to a hex STRING:

sHex = d2x(nNumber,4) /* = '30344432'x in ASCII */

...which is not an integer, but a string.

BTW, I have found it useful to use (a variation of) Hungarian notation
to reduce confusion about what is actually stored in REXX variables.
Examples of prefixes I use are:
sString = 'hello'
nNumericString = '1234'
xHexString = '4D2'
cBinaryString = '04D2'x
bBoolean = 1
...but there are exceptions which dont fit in from time to time..

== == ===== ==== Andrew J. Armstrong
== == ====== ====== Managing Director
====== == == == == HPO Computer Services Pty Ltd
====== ====== == == Sydney, Australia
== == ===== ====== Voice: +61 2 041 111 0245
== == == ==== Internet: ajar...@ozemail.com.au


Wim Kok

unread,
Jan 8, 1996, 3:00:00 AM1/8/96
to
In article <820350...@anweald.exnet.co.uk> p...@anweald.exnet.co.uk writes:
>
>d...@camelot.rockefeller.edu writes in article <DKD30...@rockyd.rockefeller.ed>u>:

>>
>> Is there a function that can take a variable declared like
>>
>> data = 123
>>
>> and convert it so that the variable's value is actually 123
>> (that's the number 123, NOT a character string consisting
>> od ASCII 1 followed by ASCII 2 followed by ASCII 3... which
>> I think is 31, 32, 33)
>>
>> I need to pass these variables as integers to a device driver.
>>
>> In fact, REXX must have this built in, otherwise how could it
>> do addition with variables saved as strings?
>
>Rexx's arithmetic is defined and implemented as operations on strings, believe
>it or not :-) . This gives a lot of portability at the expense of speed.
>
>I think what you mean is 123 as it would be physically in C ie in binary. The
>function d2x( 123 ) gives this (7B).

This is the Hex value. You can get binary value with d2c(). If you look at
the output you see "{" .


>
>Yours, Patrick
>__________________________________________________________________
>
>Patrick Herring, Primrose Hill, London, UK
>I tend to eat my UUCP feed once a day so replies can take two days
>

--
Wim Kok 2405 ZJ Alphen ad Rijn The Netherlands
Phone: 0172-424536 email: w...@wims.ow.nl


parent frederick

unread,
Jan 9, 1996, 3:00:00 AM1/9/96
to
number = '1234'
val = VALUE(number)

/* value is a buit-in function provided by REXX */

Patrick Herring

unread,
Jan 10, 1996, 3:00:00 AM1/10/96
to
w...@wims.ow.nl writes in article <8211106...@wims.ow.nl>:> >d...@camelot.rockefeller.edu writes in article <DK> D30E...@rockyd.rockefeller.ed>u>:

> >>
> >> Is there a function that can take a variable declared like
> >>
> >> data = 123
> >>
> >> and convert it so that the variable's value is actually 123
> >> (that's the number 123, NOT a character string consisting
> >> od ASCII 1 followed by ASCII 2 followed by ASCII 3... which
> >> I think is 31, 32, 33)
> >>
> >> I need to pass these variables as integers to a device driver.
> >>
> >> In fact, REXX must have this built in, otherwise how could it
> >> do addition with variables saved as strings?
> >
> >Rexx's arithmetic is defined and implemented as operations on strings, believe>
> >it or not :-) . This gives a lot of portability at the expense of speed.
> >
> >I think what you mean is 123 as it would be physically in C ie in binary. The > >function d2x( 123 ) gives this (7B).
>
> This is the Hex value. You can get binary value with d2c(). If you look at
> the output you see "{" .

Hmm, yes, elementary mistake by me, oh dear. Still, as Dan said, it's a
confusing part of Rexx in that the input to d2c() is a char string and you get
back a binary number. One thing the thread did show up is that there is no one
thing that is 'really' a number in Rexx. Coming from COBOL I'm not surprised.

Yours, Patrick
_____________________________________________________________________________

Peter Hansen

unread,
Jan 11, 1996, 3:00:00 AM1/11/96
to
In <biff-0801962029250001@pool24_1.odyssee.net>, bi...@204.50.80.201 (parent frederick) writes:
>number = '1234'
>val = VALUE(number)

I missed the previous postings in this thread, but the above is
redundant. Equivalent code is:

number = '1234'
val = number /* same effect as value(number), in this case */

number = 1234 /* identical to above... */
val = number

REXX numerics are strings. No special conversion of this kind is
required, unlike many other languages with distinct variable types.

------------------------------------------------------------------------
Peter HANSEN Engenuity Corporation
pe...@engcorp.com Guelph, Ontario, Canada
http://www.sentex.net/~engcorp/peter/
------------------------------------------------------------------------


Peter Hansen

unread,
Jan 18, 1996, 3:00:00 AM1/18/96
to
In <821288...@anweald.exnet.co.uk>, p...@anweald.exnet.co.uk (Patrick Herring) writes:
>> This is the Hex value. You can get binary value with d2c(). If you look at
>> the output you see "{" .
>
>Hmm, yes, elementary mistake by me, oh dear. Still, as Dan said, it's a
>confusing part of Rexx in that the input to d2c() is a char string and you get
>back a binary number.

No, actually you get back a byte (a.k.a. a character), which is what you
should expect from the name of the function "decimal-2-character". You
input a string containing digits which are interpreted as base ten, and
you get out a byte whose value matches that decimal number. The byte can
be represented as a character (which is is when REXX prints it), or as a
decimal number, a hex number, or a binary number such as 00100101. If
you want to get back the actual binary number, you need to use
x2b(d2x(decimal_number_in_string))

>One thing the thread did show up is that there is no one
>thing that is 'really' a number in Rexx.

There are numbers in REXX. You can't do "say 4*5" without numbers. I
think you meant to say there is no true numeric data storage type, and
that's basically true, if largely unimportant other than from the point
of view of speed.

0 new messages