Mercurial > hg > octave-thorsten
changeset 2268:ee5ec3133ed3
[project @ 1996-05-24 00:53:19 by jwe]
Initial revision
author | jwe |
---|---|
date | Fri, 24 May 1996 00:53:19 +0000 |
parents | 4028b7c79927 |
children | 517a43164a60 |
files | scripts/strings/bin2dec.m scripts/strings/blanks.m scripts/strings/deblank.m scripts/strings/dec2bin.m scripts/strings/dec2hex.m scripts/strings/hex2dec.m |
diffstat | 6 files changed, 299 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/scripts/strings/bin2dec.m @@ -0,0 +1,40 @@ +# Copyright (C) 1996 John W. Eaton +# +# This file is part of Octave. +# +# Octave is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2, or (at your option) any +# later version. +# +# Octave is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with Octave; see the file COPYING. If not, write to the Free +# Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +function y = bin2dec (x) + +# usage: bin2dec (x) +# +# Returns the decimal number corresponding to the binary number in +# quotes. For example, bin2dec ("1110") returns 14. + +# Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. + + if (nargin != 1) + usage ("bin2dec (x)"); + endif + + x = toascii (x) - toascii ("0"); + + if (all (x == 0 | x == 1)) + y = sum ((x .* (ones (rows (x), 1) * 2.^((length (x) - 1) : -1 : 0)))')'; + else + error ("bin2dec: argument must be a string of zeros and ones"); + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/strings/blanks.m @@ -0,0 +1,41 @@ +# Copyright (C) 1996 John W. Eaton +# +# This file is part of Octave. +# +# Octave is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2, or (at your option) any +# later version. +# +# Octave is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with Octave; see the file COPYING. If not, write to the Free +# Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +function s = blanks (n) + +# usage: blanks (n) +# +# Returns a string of n blanks. + +# Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. + + if (nargin != 1) + usage ("blanks (n)"); + endif + + if (is_scalar (n) && n > 0 && n == round (n)) + s = setstr (ones (1, n) * toascii (" ")); + else + error ("blanks: n must be a positive integer"); + endif + +endfunction + + + +
new file mode 100644 --- /dev/null +++ b/scripts/strings/deblank.m @@ -0,0 +1,49 @@ +# Copyright (C) 1996 John W. Eaton +# +# This file is part of Octave. +# +# Octave is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2, or (at your option) any +# later version. +# +# Octave is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with Octave; see the file COPYING. If not, write to the Free +# Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +function t = deblank (s) + +# usage: deblank (s) +# +# Remove trailing blanks from the string s. + +# Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. + + if (nargin != 1) + usage ("deblank (s)"); + endif + + if (isstr (s)) + + [nr, nc] = size (s); + len = nr * nc; + + if (len == 0) + t = s; + else + s = toascii (s); + s = reshape (s, 1, len); + k = max (find (s != toascii (" "))); + t = setstr (s (1:k)); + endif + + else + error ("deblank: expecting string argument"); + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/strings/dec2bin.m @@ -0,0 +1,62 @@ +# Copyright (C) 1996 John W. Eaton +# +# This file is part of Octave. +# +# Octave is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2, or (at your option) any +# later version. +# +# Octave is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with Octave; see the file COPYING. If not, write to the Free +# Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +function y = dec2bin (x) + +# usage: dec2bin (x) +# +# Returns the binary number corresponding to the nonnegative integer +# x. For example, dec2bin (14) returns "1110". + +# Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. + + if (nargin != 1) + usage ("dec2bin (x)"); + endif + + [nr, nc] = size (x); + + len = nr * nc; + + x = reshape (x, 1, len); + + eleo = empty_list_elements_ok; + unwind_protect + empty_list_elements_ok = 1; + y = []; + for i = 1:len + tmp = x (i); + if (tmp == round (tmp) && tmp >= 0) + while (tmp >= 2) + z = fix (tmp ./ 2); + y = [y, tmp - 2 * z]; + tmp = z; + endwhile + y = [y, tmp]; + else + error ("dec2hex: invalid conversion"); + endif + endfor + y = fliplr (y); + y = setstr (y + toascii ("0")); + unwind_protect_cleanup + empty_list_elements_ok = eleo; + end_unwind_protect + +endfunction +
new file mode 100644 --- /dev/null +++ b/scripts/strings/dec2hex.m @@ -0,0 +1,54 @@ +# Copyright (C) 1996 John W. Eaton +# +# This file is part of Octave. +# +# Octave is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2, or (at your option) any +# later version. +# +# Octave is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with Octave; see the file COPYING. If not, write to the Free +# Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +function h = dec2hex (d) + +# usage: dec2hex (d) +# +# Returns the hex number corresponding to the decimal number d. For +# example, dec2hex (2748) returns "abc". + +# Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. + + if (nargin != 1) + usage ("dec2hex (d)"); + endif + + [nr, nc] = size (d); + + len = nr * nc; + + d = reshape (d, 1, len); + + eleo = empty_list_elements_ok; + unwind_protect + empty_list_elements_ok = 1; + h = ""; + for i = 1:len + tmp = d (i); + if (tmp == round (tmp)) + h = [h, sprintf ("%x", tmp)]; + else + error ("dec2hex: invalid conversion"); + endif + endfor + unwind_protect_cleanup + empty_list_elements_ok = eleo; + end_unwind_protect + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/strings/hex2dec.m @@ -0,0 +1,53 @@ +# Copyright (C) 1996 John W. Eaton +# +# This file is part of Octave. +# +# Octave is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2, or (at your option) any +# later version. +# +# Octave is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with Octave; see the file COPYING. If not, write to the Free +# Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +function d = hex2dec (h) + +# usage: hex2dec (h) +# +# Returns the decimal number corresponding to the hex number in +# quotes. For example, hex2dec ("12B") and hex2dec ("12b") both +# return 299. + +# Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. + + if (nargin != 1) + usage ("hex2dec (x)"); + endif + + if (isstr (h)) + nr = rows (h); + d = zeros (nr, 1); + for i = 1:nr + s = h (i, :); + if (isxdigit (s)) + tmp = sscanf (s, "%x"); + if (isempty (tmp)) + error ("hex2dec: invalid conversion"); + else + d (i) = tmp; + endif + else + error ("hex2dec: argument must be a string of hexadecimal digits"); + endif + endfor + else + error ("hex2dec: expecting a string argument"); + endif + +endfunction