Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

Dumb problem

 [1/4] from: mat:0wnerage at: 23-Apr-2002 23:14

Folks, Stupid little thing I had to do today and couldn't think of an elegant way of doing it. It's been bugging me. Need function which take a two char string input and returns the integer value. The problem here is I can't work out how to tell Rebol that a string variable can be thought of as hex. Here's my hacky solution; StringToHex: func [ stringin [string!] ][ do rejoin["result: to-integer #{"stringin"}"] result ] Is there a proper way? P.S. I sent a mail earlier that never seemed to get to the list. If I'm not mistaken the real name isn't pulled out into an object with the import-email function? Am I missing something or do I have to parse it out of the full mail myself? (which is what I'm doing now) Regards, Mat.

 [2/4] from: anton:lexicon at: 24-Apr-2002 13:26

Use to-issue instead of to-hex. (to-hex doesn't like strings):
>> to-hex "1e"
** Script Error: to-hex expected value argument of type: integer ** Near: to-hex "1e"
>> to-issue "1e"
== #1e
>> to-integer to-issue "1e"
== 30
>> to-integer to-issue to-hex 30
== 30 Hence, hex-string-to-integer: func [ "Converts a hexadecimal string of two characters to an integer" string [string!] "string you want to convert" ][ to-integer to-issue string ] example:
>> hex-string-to-integer "1f"
== 31 It's too little for a function, just put a comment instead, unless you want to check your inputs. Anton.

 [3/4] from: chris::ross-gill::com at: 23-Apr-2002 23:42

Hi Mat,
> StringToHex: func [ > stringin [string!] > ][ > do rejoin["result: to-integer #{"stringin"}"] > result > ]
How about:
>> s-to-h: func [str [string!]][to-integer debase/base str 16] >> s-to-h "2a"
== 42 - Chris

 [4/4] from: mat:0wnerage at: 24-Apr-2002 8:53

Hi Anton, A> Use to-issue instead of to-hex. A> (to-hex doesn't like strings): to-issue! Gah, I had a feeling they'd be a proper keyword :) Cheers for that. Regards, Mat.