1
## Copyright (C) 2008 Thomas Treichl <thomas.treichl@gmx.net>
2
## Copyright (C) 2013 Carnë Draug <carandraug@octave.org>
4
## This program is free software; you can redistribute it and/or modify it under
5
## the terms of the GNU General Public License as published by the Free Software
6
## Foundation; either version 3 of the License, or (at your option) any later
9
## This program is distributed in the hope that it will be useful, but WITHOUT
10
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
## You should have received a copy of the GNU General Public License along with
15
## this program; if not, see <http://www.gnu.org/licenses/>.
18
## @deftypefn {Function File} {} ascii ()
19
## @deftypefnx {Function File} {} ascii (@var{columns})
22
## If this function is called without any input argument and without any output
23
## argument then prints a nice ASCII-table (excluding special characters with
24
## hexcode 0x00 to 0x20). The input argument @var{columns} specifies the
25
## number of columns and defaults to 4.
27
## If it is called with one output argument then return the ASCII table as
28
## a string without displaying anything. Run @code{demo ascii} for examples.
30
## @seealso{char, isascii, toascii}
33
function table = ascii (vcol = 4)
37
elseif (! isnumeric (vcol) || ! isscalar (vcol) ||
38
fix (vcol) != vcol || vcol < 1)
39
error ("ascii: COLUMNS must be a positive integer");
42
## First char is #32 (0x20) and last char is #128 (0x80)
43
voff = floor ((128 - 32) / vcol);
45
## Print a first row for the and underline that row
46
vtab = [repmat(" Dec Hex Chr ", [1 vcol])
47
repmat("-------------", [1 vcol])];
49
## Create the lines and columns of the asci table
50
for vpos = 32:(32+voff)
53
vact = (vcnt-1)*voff+vpos;
54
vstr = {num2str(vact), dec2hex(vact), char(vact)};
55
vline = [vline sprintf(" %3s", vstr{1:length (vstr)}) " "];
60
## Print table to screen or return it to output argument
69
%! ## Display pretty table of conversion between ASCII, decimal and hexadecimal
73
%! ## Display 6 columns table of conversion between ASCII, decimal and hexadecimal
77
%! ## Return a string with a pretty formatted table (but don't display it)
79
%! display (table (65, :));
83
%! assert (str(1,:), " Dec Hex Chr Dec Hex Chr Dec Hex Chr Dec Hex Chr Dec Hex Chr ");
84
%! assert (str(4,:), " 33 21 ! 52 34 4 71 47 G 90 5A Z 109 6D m ");
88
%! assert (str(1,:), " Dec Hex Chr ");
89
%! assert (str(6,:), " 35 23 # ");
91
%!error <positive> ascii (0)
92
%!error <integer> ascii (4.5)
93
%!error <COLUMNS> ascii ("dec")