Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/swapbytes.m @ 18432:2e62b1f01bfe stable
* mkoctfile.in.cc: Use std:: instead of using declarartion.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 24 Jan 2014 04:19:00 -0500 |
parents | d63878346099 |
children | 827ac8b5ae07 |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
14868
diff
changeset
|
1 ## Copyright (C) 2007-2013 David Bateman |
6869 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6869 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6869 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} swapbytes (@var{x}) | |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
21 ## Swap the byte order on values, converting from little endian to big |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
22 ## endian and vice versa. For example: |
6869 | 23 ## |
24 ## @example | |
25 ## @group | |
26 ## swapbytes (uint16 (1:4)) | |
27 ## @result{} [ 256 512 768 1024] | |
28 ## @end group | |
29 ## @end example | |
30 ## | |
31 ## @seealso{typecast, cast} | |
32 ## @end deftypefn | |
33 | |
34 function y = swapbytes (x) | |
12847
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
35 |
6869 | 36 if (nargin != 1) |
37 print_usage (); | |
38 endif | |
39 | |
40 clx = class (x); | |
41 if (strcmp (clx, "int8") || strcmp (clx, "uint8") || isempty (x)) | |
42 y = x; | |
43 else | |
44 if (strcmp (clx, "int16") || strcmp (clx, "uint16")) | |
45 nb = 2; | |
46 elseif (strcmp (clx, "int32") || strcmp (clx, "uint32")) | |
47 nb = 4; | |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
48 elseif (strcmp (clx, "int64") || strcmp (clx, "uint64") |
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
49 || strcmp (clx, "double")) |
6869 | 50 nb = 8; |
51 else | |
52 error ("swapbytes: invalid class of object"); | |
53 endif | |
54 y = reshape (typecast (reshape (typecast (x(:), "uint8"), nb, numel (x)) | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
55 ([nb : -1 : 1], :) (:), clx), size (x)); |
6869 | 56 endif |
12847
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
57 |
6869 | 58 endfunction |
12847
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
59 |
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
60 |
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
61 %!assert (double (swapbytes (uint16 (1:4))), [256 512 768 1024]) |
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
62 %!error (swapbytes ()) |
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
63 %!error (swapbytes (1, 2)) |
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
64 |