TF++, Miscellaneous classes and modules in C++:

◆ magic()

int tfxx::ioswap::magic ( const char *const  cmagic)

Create a magic number from a character string. ,If $x_{i}=(\vec{x})_{i}$ represents the input character sequence cmagic and $N=4$ is the value of sizeof(int), then the return value will be

\[ \textrm{magic}(\vec{x}) =\sum\limits_{i=0}^{N-1} x_{i}\; 2^{8\; (N-1-i)} =x_{3}+256\; (x_{2}+256\; (x_{1}+256\; x_{0})). \]

.

function to create the magic number

Parameters
cmagic4-byte character sequence representing magic number (most restrictive: pass a const pointer to a const char) is pointer to character array of size sizeof(int)
Returns
The magic number representing the 4-byte character sequence on your system
See also
TEST: Fortran I/O and byte swapping.

Definition at line 59 of file ioswap.cc.

Referenced by cpu(), file_magic_test(), file_magic_write(), and tfxx::fortranio::FortranBinOutput::write_magic().

60 {
61  union {
62  unsigned int uival;
63  int ival;
64  } out;
65  union {
66  char chr[sizeof(int)];
67  unsigned char uchr[sizeof(int)];
68  } in;
69  const int& intsize=sizeof(int);
70  for (int i=0; i<intsize; i++)
71  { in.chr[i]=cmagic[i]; }
72  out.uival=0;
73  for (int i=0; i<intsize; i++)
74  { out.uival=out.uival*256+in.uchr[i]; }
75  return(out.ival);
76 } // magic()
Here is the caller graph for this function: