annotate scripts/strings/bin2dec.m @ 2276:6dedd4e0a82f

[project @ 1996-05-24 04:58:21 by jwe]
author jwe
date Fri, 24 May 1996 04:58:21 +0000
parents ee5ec3133ed3
children 5cffc4b8de57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2276
6dedd4e0a82f [project @ 1996-05-24 04:58:21 by jwe]
jwe
parents: 2268
diff changeset
1 # Copyright (C) 1996 Kurt Hornik
2268
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
2 #
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
3 # This file is part of Octave.
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
4 #
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
5 # Octave is free software; you can redistribute it and/or modify it
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
6 # under the terms of the GNU General Public License as published by the
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
7 # Free Software Foundation; either version 2, or (at your option) any
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
8 # later version.
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
9 #
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
10 # Octave is distributed in the hope that it will be useful, but WITHOUT
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
13 # for more details.
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
14 #
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
16 # along with Octave; see the file COPYING. If not, write to the Free
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
17 # Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
18
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
19 function y = bin2dec (x)
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
20
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
21 # usage: bin2dec (x)
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
22 #
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
23 # Returns the decimal number corresponding to the binary number in
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
24 # quotes. For example, bin2dec ("1110") returns 14.
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
25
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
26 # Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>.
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
27
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
28 if (nargin != 1)
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
29 usage ("bin2dec (x)");
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
30 endif
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
31
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
32 x = toascii (x) - toascii ("0");
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
33
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
34 if (all (x == 0 | x == 1))
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
35 y = sum ((x .* (ones (rows (x), 1) * 2.^((length (x) - 1) : -1 : 0)))')';
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
36 else
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
37 error ("bin2dec: argument must be a string of zeros and ones");
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
38 endif
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
39
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
40 endfunction