DATRW++ library: seismic data I/O with multiple formats

◆ magic()

int datrw::util::magic ( const char *const  cmagic)

function to create the magic number

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})). \]

.

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

Definition at line 52 of file bytesex.cc.

Referenced by cpu(), file_magic_test(), and file_magic_write().

53  {
54  union {
55  unsigned int uival;
56  int ival;
57  } out;
58  union {
59  char chr[sizeof(int)];
60  unsigned char uchr[sizeof(int)];
61  } in;
62  const int& intsize=sizeof(int);
63  for (int i=0; i<intsize; i++)
64  { in.chr[i]=cmagic[i]; }
65  out.uival=0;
66  for (int i=0; i<intsize; i++)
67  { out.uival=out.uival*256+in.uchr[i]; }
68  return(out.ival);
69  } // magic()
Here is the caller graph for this function: