#include <byteorder.h>
Static Public Member Functions | |
bool | is_host_big_endian () |
bool | is_float_big_endian (float small_number) |
given a small floating number, return whether the number is in big endian or not. | |
template<class T> | |
bool | is_data_big_endian (const T *small_num_addr) |
given a pointer to a reasonable small integer number, return whether the number is big endian or not. | |
template<class T> | |
void | become_big_endian (T *data, size_t n=1) |
convert data from host byte order to big endian order. | |
template<class T> | |
void | become_little_endian (T *data, size_t n=1) |
convert data from host byte order to little endian order. | |
template<class T> | |
void | swap_bytes (T *data, size_t n=1) |
swap the byte order of data with 'n' T-type elements. | |
Static Private Attributes | |
bool | is_host_endian_checked = false |
bool | host_big_endian = false |
The byte order is the order in which bytes are stored to create larger data types such as the int and long values. Different kinds of computers use different byte order conventions.
There are 2 major byte orders: big-endian and little-endian.
big-endian (like SGI) store the most significant bytes (i.e. the bytes that hold the largest part of the value) first. little-endian (like x86) store the most significant byte last.
The host byte order is the byte order used on the current host.
ByteOrder class defines functions for checking running-host byte-order, checking data byte-order, convert data from one byte-order to another byte-order.
Definition at line 66 of file byteorder.h.
|
convert data from host byte order to big endian order. 'n' is the number of elements of type T. Definition at line 120 of file byteorder.h. 00121 { 00122 if (!is_host_big_endian()) { 00123 swap_bytes < T > (data, n); 00124 } 00125 }
|
|
convert data from host byte order to little endian order. 'n' is the number of elements of type T. Definition at line 130 of file byteorder.h. 00131 { 00132 if (is_host_big_endian()) { 00133 swap_bytes < T > (data, n); 00134 } 00135 }
|
|
given a pointer to a reasonable small integer number, return whether the number is big endian or not. For a n-bit integer, the number should < (2 ^ (n/2)). e.g., for 'int', number should < 65535; for 'short', number should < 255. Definition at line 84 of file byteorder.h. Referenced by EMAN::MrcIO::is_valid(), EMAN::ImagicIO2::is_valid(), EMAN::ImagicIO::is_valid(), EMAN::IcosIO::is_valid(), EMAN::Gatan2IO::is_valid(), EMAN::EmIO::is_valid(), EMAN::EmimIO::is_valid(), and EMAN::Df3IO::is_valid(). 00085 { 00086 if (!small_num_addr) { 00087 return false; 00088 } 00089 00090 bool data_big_endian = false; 00091 size_t typesize = sizeof(T); 00092 char *d = (char *) small_num_addr; 00093 00094 if (is_host_big_endian()) { 00095 data_big_endian = false; 00096 for (size_t i = typesize / 2; i < typesize; i++) 00097 { 00098 if (d[i] != 0) { 00099 data_big_endian = true; 00100 break; 00101 } 00102 } 00103 } 00104 else { 00105 data_big_endian = true; 00106 for (size_t j = 0; j < typesize / 2; j++) { 00107 if (d[j] != 0) { 00108 data_big_endian = false; 00109 break; 00110 } 00111 } 00112 } 00113 00114 return data_big_endian; 00115 }
|
|
given a small floating number, return whether the number is in big endian or not. If a number is smaller than 65535, it is defined as a "small" number here. Definition at line 63 of file byteorder.cpp. References EMAN::Util::goodf(), and is_host_big_endian(). Referenced by EMAN::SingleSpiderIO::is_valid(), and EMAN::SpiderIO::is_valid(). 00064 { 00065 bool is_big = false; 00066 00067 if (Util::goodf(&f) && f > 0 && f < 65535.0 && f == floor(f)) { 00068 is_big = is_host_big_endian(); 00069 } 00070 else { 00071 is_big = !is_host_big_endian(); 00072 } 00073 00074 return is_big; 00075 }
|
|
|
swap the byte order of data with 'n' T-type elements.
Definition at line 139 of file byteorder.h. Referenced by EMAN::SingleSpiderIO::is_valid(), EMAN::SpiderIO::is_valid(), EMAN::PifIO::is_valid(), EMAN::OmapIO::is_valid(), EMAN::MrcIO::is_valid(), EMAN::ImagicIO2::is_valid(), EMAN::ImagicIO::is_valid(), EMAN::IcosIO::is_valid(), EMAN::Gatan2IO::is_valid(), EMAN::EmIO::is_valid(), EMAN::EmimIO::is_valid(), and EMAN::MrcIO::swap_header(). 00140 { 00141 unsigned char s; 00142 size_t p = sizeof(T); 00143 char *d = (char *) data; 00144 00145 if (p > 1) { 00146 for (size_t i = 0; i < n; i++, d += p) { 00147 for (size_t j = 0; j < p / 2; j++) { 00148 s = d[j]; 00149 d[j] = d[p - 1 - j]; 00150 d[p - 1 - j] = s; 00151 } 00152 } 00153 } 00154 }
|
|
Definition at line 42 of file byteorder.cpp. Referenced by is_host_big_endian(). |
|
Definition at line 41 of file byteorder.cpp. Referenced by is_host_big_endian(). |