TPCCLIB
Loading...
Searching...
No Matches
swap.c File Reference

Byte swapping for little to big endian (and vice versa) conversion. More...

#include "libtpcmisc.h"

Go to the source code of this file.

Functions

int little_endian ()
 
void swap (void *from, void *to, int size)
 
void swabip (void *buf, long long int size)
 
void swawbip (void *buf, long long int size)
 
void swawip (void *buf, long long int size)
 
void printf32bits (void *buf)
 

Detailed Description

Byte swapping for little to big endian (and vice versa) conversion.

Author
Vesa Oikonen

Definition in file swap.c.

Function Documentation

◆ little_endian()

int little_endian ( )

Check whether current platform uses little endian byte order. See H&S Sec. 6.1.2 pp. 163-4.

Returns
Returns 1, if current platform is little endian, and 0 if not.

Definition at line 14 of file swap.c.

15{
16 int x=1;
17 if(*(char *)&x==1) return(1); else return(0);
18}

Referenced by anaReadHeader(), anaReadImagedata(), anaWriteHeader(), dcmFileWrite(), dcmitemGetInt(), dcmitemGetReal(), dcmReadFileTag(), dcmReadFileVL(), dcmReadFileVRVL(), dcmValueString(), dcmWriteFileTag(), dcmWriteFileVRVL(), ecat63Create(), ecat63Matenter(), ecat63ReadAttnheader(), ecat63ReadImageheader(), ecat63ReadMainheader(), ecat63ReadMatdata(), ecat63ReadMatlist(), ecat63ReadNormheader(), ecat63ReadScanheader(), ecat63WriteAttnheader(), ecat63WriteImageheader(), ecat63WriteMainheader(), ecat63WriteMatdata(), ecat63WriteNormheader(), ecat63WriteScanheader(), ecat7Create(), ecat7EnterMatrix(), ecat7Read2DNormheader(), ecat7Read2DScanheader(), ecat7ReadAttenheader(), ecat7ReadImageheader(), ecat7ReadMainheader(), ecat7ReadMatlist(), ecat7ReadMatrixdata(), ecat7ReadNormheader(), ecat7ReadPolmapheader(), ecat7ReadScanheader(), ecat7Write2DNormheader(), ecat7Write2DScanheader(), ecat7WriteAttenheader(), ecat7WriteImageheader(), ecat7WriteMainheader(), ecat7WriteMatrixdata(), ecat7WriteNormheader(), ecat7WritePolmapheader(), ecat7WriteScanheader(), hrrtMakeCalHdr(), imgSetNiftiHeader(), imgWriteAnalyze(), imgWriteAnalyzeFrame(), niftiReadHeader(), niftiReadImagedata(), niftiWriteHeader(), tiffWriteImg(), and upetReadImagedata().

◆ printf32bits()

void printf32bits ( void * buf)

Printfs as bit string the 32-bit variable pointed to by buf. Far from being optimized, thus only for testing and development purposes.

Parameters
bufPointer to memory

Definition at line 133 of file swap.c.

133 {
134 unsigned int u, i;
135 int j;
136
137 memcpy(&u, buf, 4);
138 for(i=32; i>0; i--) {
139 j=i-1;if(i<32 && (i%8)==0) printf(" ");
140 if(u & (1L<<j)) printf("1"); else printf("0");
141 }
142 printf("\n");
143}

◆ swabip()

◆ swap()

void swap ( void * from,
void * to,
int size )

Swaps the specified short int, int, long int, float, or double from little endian to big endian or vice versa. Arguments are allowed to overlap.

Parameters
fromPointer to a short int, int, long int, float, or double variable
toPointer to a short int, int, long int, float, or double variable
sizeSize of from and to (byte nr) must be 1, 2, 4 or 8.

Definition at line 31 of file swap.c.

31 {
32 unsigned char c;
33 unsigned short int s;
34 unsigned long l;
35
36 switch(size) {
37 case 1:
38 *(char *)to=*(char *)from;
39 break;
40 case 2:
41 c=*(unsigned char *)from;
42 *(unsigned char *)to = *((unsigned char *)from+1);
43 *((unsigned char *)to+1) = c;
44 /*swab(from, to, size); // NOT ANSI */
45 break;
46 case 4:
47 s=*(unsigned short *)from;
48 *(unsigned short *)to = *((unsigned short *)from+1);
49 *((unsigned short *)to+1) = s;
50 swap((char*)to, (char*)to, 2);
51 swap((char*)((unsigned short *)to+1), (char*)((unsigned short *)to+1), 2);
52 break;
53 case 8:
54 l=*(unsigned long *)from;
55 *(unsigned long *)to = *((unsigned long *)from+1);
56 *((unsigned long *)to+1) = l;
57 swap((char *)to, (char *)to, 4);
58 swap((char*)((unsigned long *)to+1), (char*)((unsigned long *)to+1), 4);
59 break;
60 }
61}
void swap(void *from, void *to, int size)
Definition swap.c:31

Referenced by dcmitemGetInt(), dcmitemGetReal(), dcmReadFileTag(), dcmReadFileVL(), dcmReadFileVRVL(), dcmValueString(), dcmWriteFileVRVL(), intExpand(), and swap().

◆ swawbip()

void swawbip ( void * buf,
long long int size )

◆ swawip()

void swawip ( void * buf,
long long int size )

In-place swaw, switches words (but not bytes) from an array of 4-byte ints or floats.

Parameters
bufPointer to memory
sizeSize of buf in bytes

Definition at line 114 of file swap.c.

116 {
117 unsigned short int s, *sptr;
118
119 sptr=(unsigned short int*)buf;
120 for(long long i=0; i<size; i+=4, sptr+=2) {
121 s=sptr[0]; sptr[0]=sptr[1]; sptr[1]=s;
122 }
123}

Referenced by ecat63rFloat(), ecat63wFloat(), and ecat7rFloat().