1

For a table I would like to convert the first column from decimal numbers to binary. The table looks so far like this:

|    .   |  . |         . |       . |     . |
|    .   |  . |         . |       . |     . |
|    .   |  . |         . |       . |     . |
|   1304 | 24 |         9 |      15 |     5 |
|   1305 | 25 |        10 |      15 |     5 |
|   1306 | 26 |        11 |      15 |     5 |
|   1307 | 27 |        12 |      15 |     5 |
|   1308 | 28 |        13 |      15 |     5 |
|   1309 | 29 |        14 |      15 |     5 |
|   1310 | 30 |        15 |      15 |     5 |
|   1311 | 31 |        16 |      15 |     5 |
|   1312 | 32 |        17 |      15 |     5 |
|   1313 | 33 |        18 |      15 |     5 |

For the conversion I found already a working function:

(defun binList(N)
  (cond 
   ((< N 1) '())
   ((= N 1) '(1))
   ((> N 1) (if(=(mod N 2) 0) 
            (append (binList(/ N 2)) '(0))
            (append (binList(floor(/ N 2))) '(1))))))

When I want to apply the TBLFM

+TBLFM: $1='(binList $1)

i just get errors ( I tried also to convert the 2. column and write into the first)

|      .   |  . |         . |       . |     . |
|      .   |  . |         . |       . |     . |
|      .   |  . |         . |       . |     . |
|   #ERROR | 24 |         9 |      15 |     5 |
|   #ERROR | 25 |        10 |      15 |     5 |
|   #ERROR | 26 |        11 |      15 |     5 |
|   #ERROR | 27 |        12 |      15 |     5 |
|   #ERROR | 28 |        13 |      15 |     5 |
|   #ERROR | 29 |        14 |      15 |     5 |
|   #ERROR | 30 |        15 |      15 |     5 |
|   #ERROR | 31 |        16 |      15 |     5 |
|   #ERROR | 32 |        17 |      15 |     5 |
|   #ERROR | 33 |        18 |      15 |     5 |

are there some further steps needed/ in between ?

1

Two problems:

  • Table entries are strings. You need to convert the string "10" to the number 10 before passing it to your binList function.

  • The binList function returns a list and AFAICT org has trouble converting it to a string.

Try the following:

 |  n |
 |----|
 |  1 |
 |  2 |
 |  3 |
 |  4 |
 |  5 |
 |  6 |
 |  7 |
 |  8 |
 |  9 |
 | 10 |
 #+TBLFM: $1='(binString (string-to-number $1))


 #+BEGIN_SRC emacs-lisp
   (defun binString(N)
     (cond 
       ((< N 1) "")
       ((= N 1) "1")
       ((> N 1) (if (= (mod N 2) 0) 
                   (format "%s0" (binString(/ N 2)))
                 (format "%s1" (binString(floor(/ N 2))))))))
 #+END_SRC

The logic of binString is identical to that of binList but it deals with strings instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.