Gerhard Niklasch on Fri, 25 Jun 1999 00:38:22 +0200 (MET DST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Hex input


In response to:
> Message-ID: <Pine.SOL.3.96.990624152959.15543E-100000@elios>
> Date: Thu, 24 Jun 1999 15:32:51 +0100 (BST)
> From: Mark Chimley <M.Chimley@bristol.ac.uk>
> 
> Does anybody know of an easy way to input a number in hexadecimal format
> into GP? Alternatively, has anybody written a function to convert a
> string to an integer?

Evil hack warning, put your coffee/tea mug at a safe distance from
your keyboard before reading on...

The follwing kind of thing should work on Linux as well as Solaris,
as well as any system where bc is in your path, and it'll work for
any base bc can handle:

(00:28) gp > a=extern("(echo ibase=16;echo 0AF)|bc")
%1 = 175

Actually,

(00:29) gp > a=extern("echo \"ibase=16\n0AF\"|bc")
%2 = 175

is slightly more efficient  (it saves a subshell)  but IMHO less
readable, and possibly not as portable  (it depends on echo and shell
idiosyncrasies).

Exercise:  Modify the above to pass a string from a gp variable
into the middle of bc's input.

Enjoy, Gerhard