File: conversion-macros.md

package info (click to toggle)
glib2.0 2.84.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 66,112 kB
  • sloc: ansic: 538,924; python: 9,661; sh: 1,612; xml: 1,482; perl: 1,222; cpp: 535; makefile: 321; javascript: 11
file content (356 lines) | stat: -rw-r--r-- 11,791 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
Title: Conversion Macros

# Conversion Macros

## Type Conversion

Many times GLib, GTK, and other libraries allow you to pass "user data" to a
callback, in the form of a void pointer. From time to time you want to pass
an integer instead of a pointer. You could allocate an integer, with
something like:

```c
int *ip = g_new (int, 1);
*ip = 42;
```

But this is inconvenient, and it's annoying to have to free the memory at
some later time.

Pointers are always at least 32 bits in size (on all platforms GLib intends
to support). Thus you can store at least 32-bit integer values in a pointer
value. Naively, you might try this, but it's incorrect:

```c
gpointer p;
int i;
p = (void*) 42;
i = (int) p;
```

Again, that example was not correct, don't copy it.

The problem is that on some systems you need to do this:

```c
gpointer p;
int i;
p = (void*) (long) 42;
i = (int) (long) p;
```

The GLib macros [`GPOINTER_TO_INT()`](#gpointer-to-int), [`GINT_TO_POINTER()`](#gint-to-pointer),
etc. take care to do the right thing on every platform.

**Warning**: You may not store pointers in integers. This is not portable in
any way, shape or form. These macros only allow storing integers in
pointers, and only preserve 32 bits of the integer; values outside the range
of a 32-bit integer will be mangled.


`GINT_TO_POINTER(value)`<a name="gint-to-pointer"></a>, `GPOINTER_TO_INT(value)`<a name="gpointer-to-int"></a>

:   Stuffs an integer into a pointer type, and vice versa.

    Remember, you may not store pointers in integers. This is not portable in
    any way, shape or form. These macros only allow storing integers in
    pointers, and only preserve 32 bits of the integer; values outside the
    range of a 32-bit integer will be mangled.

`GUINT_TO_POINTER(value)`<a name="guint-to-pointer"></a>, `GPOINTER_TO_UINT(value)`<a name="gpointer-to-uint"></a>

:   Stuffs an unsigned integer into a pointer type, and vice versa.

`GSIZE_TO_POINTER(value)`<a name="gsize-to-pointer"></a>, `GPOINTER_TO_SIZE(value)`<a name="gpointer-to-size"></a>

:   Stuffs a `size_t` into a pointer type, and vice versa.

`GTYPE_TO_POINTER(value)`<a name="gtype-to-pointer"></a>, `GPOINTER_TO_TYPE(value)`<a name="gpointer-to-type"></a>

:   Stuffs a [`GType`](../gobject/alias.Type.html) into a pointer type, and
    vice versa.

    These macros should be used instead of [`GSIZE_TO_POINTER()`](#gsize-to-pointer),
    [`GPOINTER_TO_SIZE()`](#gpointer-to-size) to ensure portability, since
    `GType` is not guaranteed to be the same as `size_t`.

    Since: 2.80

## Byte Order Conversion

These macros provide a portable way to determine the host byte order and to
convert values between different byte orders.

The byte order is the order in which bytes are stored to create larger data
types such as the #gint and #glong values.  The host byte order is the byte
order used on the current machine.

Some processors store the most significant bytes (i.e. the bytes that hold
the largest part of the value) first. These are known as big-endian
processors. Other processors (notably the x86 family) store the most
significant byte last. These are known as little-endian processors.

Finally, to complicate matters, some other processors store the bytes in a
rather curious order known as PDP-endian. For a 4-byte word, the 3rd most
significant byte is stored first, then the 4th, then the 1st and finally the
2nd.

Obviously there is a problem when these different processors communicate
with each other, for example over networks or by using binary file formats.
This is where these macros come in. They are typically used to convert
values into a byte order which has been agreed on for use when communicating
between different processors. The Internet uses what is known as 'network
byte order' as the standard byte order (which is in fact the big-endian byte
order).

Note that the byte order conversion macros may evaluate their arguments
multiple times, thus you should not use them with arguments which have
side-effects.

`G_BYTE_ORDER`<a name="g-byte-order"></a>

:   The host byte order. This can be either [`G_LITTLE_ENDIAN`](#g-little-endian)
    or [`G_BIG_ENDIAN`](#g-big-endian).

`G_LITTLE_ENDIAN`<a name="g-little-endian"></a>

:   Specifies the little endian byte order.

`G_BIG_ENDIAN`<a name="g-big-endian"></a>

:   Specifies the big endian byte order.

`G_PDP_ENDIAN`<a name="g-pdp-endian"></a>

:   Specifies the PDP endian byte order.

### Signed

`GINT_FROM_BE(value)`<a name="gint-from-be"></a>

:   Converts an `int` value from big-endian to host byte order.

`GINT_FROM_LE(value)`<a name="gint-from-le"></a>

:   Converts an `int` value from little-endian to host byte order.

`GINT_TO_BE(value)`<a name="gint-to-be"></a>

:   Converts an `int` value from host byte order to big-endian.

`GINT_TO_LE(value)`<a name="gint-to-le"></a>

:   Converts an `int` value from host byte order to little-endian.

`GLONG_FROM_BE(value)`<a name="glong-from-be"></a>

:   Converts a `long` value from big-endian to the host byte order.

`GLONG_FROM_LE(value)`<a name="glong-from-le"></a>

:   Converts a `long` value from little-endian to host byte order.

`GLONG_TO_BE(value)`<a name="glong-to-be"></a>

:   Converts a `long` value from host byte order to big-endian.

`GLONG_TO_LE(value)`<a name="glong-to-le"></a>

:   Converts a `long` value from host byte order to little-endian.

`GSSIZE_FROM_BE(value)`<a name="gssize-from-be"></a>

:   Converts a `ssize_t` value from big-endian to host byte order.

`GSSIZE_FROM_LE(value)`<a name="gssize-from-le"></a>

:   Converts a `ssize_t` value from little-endian to host byte order.

`GSSIZE_TO_BE(value)`<a name="gssize-to-be"></a>

:   Converts a `ssize_t` value from host byte order to big-endian.

`GSSIZE_TO_LE(value)`<a name="gssize-to-le"></a>

:   Converts a `ssize_t` value from host byte order to little-endian.

`GINT16_FROM_BE(value)`<a name="gint16-from-be"></a>

:   Converts an `int16_t` value from big-endian to host byte order.

`GINT16_FROM_LE(value)`<a name="gint16-from-le"></a>

:   Converts an `int16_t` value from little-endian to host byte order.

`GINT16_TO_BE(value)`<a name="gint16-to-be"></a>

:   Converts an `int16_t` value from host byte order to big-endian.

`GINT16_TO_LE(value)`<a name="gint16-to-le"></a>

:   Converts an `int16_t` value from host byte order to little-endian.

`GINT32_FROM_BE(value)`<a name="gint32-from-be"></a>

:   Converts an `int32_t` value from big-endian to host byte order.

`GINT32_FROM_LE(value)`<a name="gint32-from-le"></a>

:   Converts an `int32_t` value from little-endian to host byte order.

`GINT32_TO_BE(value)`<a name="gint32-to-be"></a>

:   Converts an `int32_t` value from host byte order to big-endian.

`GINT32_TO_LE(value)`<a name="gint32-to-le"></a>

:   Converts an `int32_t` value from host byte order to little-endian.

`GINT64_FROM_BE(value)`<a name="gint64-from-be"></a>

:   Converts an `int64_t` value from big-endian to host byte order.

`GINT64_FROM_LE(value)`<a name="gint64-from-le"></a>

:   Converts an `int64_t` value from little-endian to host byte order.

`GINT64_TO_BE(value)`<a name="gint64-to-be"></a>

:   Converts an `int64_t` value from host byte order to big-endian.

`GINT64_TO_LE(value)`<a name="gint64-to-le"></a>

:   Converts an `int64_t` value from host byte order to little-endian.

### Unsigned

`GUINT_FROM_BE(value)`<a name="guint-from-be"></a>

:   Converts an `unsigned int` value from big-endian to host byte order.

`GUINT_FROM_LE(value)`<a name="guint-from-le"></a>

:   Converts an `unsigned int` value from little-endian to host byte order.

`GUINT_TO_BE(value)`<a name="guint-to-be"></a>

:   Converts an `unsigned int` value from host byte order to big-endian.

`GUINT_TO_LE(value)`<a name="guint-to-le"></a>

:   Converts an `unsigned int` value from host byte order to little-endian.

`GULONG_FROM_BE(value)`<a name="gulong-from-be"></a>

:   Converts an `unsigned long` value from big-endian to host byte order.

`GULONG_FROM_LE(value)`<a name="gulong-from-le"></a>

:   Converts an `unsigned long` value from little-endian to host byte order.

`GULONG_TO_BE(value)`<a name="gulong-to-be"></a>

:   Converts an `unsigned long` value from host byte order to big-endian.

`GULONG_TO_LE(value)`<a name="gulong-to-le"></a>

:   Converts an `unsigned long` value from host byte order to little-endian.

`GSIZE_FROM_BE(value)`<a name="gsize-from-be"></a>

:   Converts a `size_t` value from big-endian to the host byte order.

`GSIZE_FROM_LE(value)`<a name="gsize-from-le"></a>

:   Converts a `size_t` value from little-endian to host byte order.

`GSIZE_TO_BE(value)`<a name="gsize-to-be"></a>

:   Converts a `size_t` value from host byte order to big-endian.

`GSIZE_TO_LE(value)`<a name="gsize-to-le"></a>

:   Converts a `size_t` value from host byte order to little-endian.

`GUINT16_FROM_BE(value)`<a name="guint16-from-be"></a>

:   Converts a `uint16_t` value from big-endian to host byte order.

`GUINT16_FROM_LE(value)`<a name="guint16-from-le"></a>

:   Converts a `uint16_t` value from little-endian to host byte order.

`GUINT16_TO_BE(value)`<a name="guint16-to-be"></a>

:   Converts a `uint16_t` value from host byte order to big-endian.

`GUINT16_TO_LE(value)`<a name="guint16-to-le"></a>

:   Converts a `uint16_t` value from host byte order to little-endian.

`GUINT32_FROM_BE(value)`<a name="guint32-from-be"></a>

:   Converts a `uint32_t` value from big-endian to host byte order.

`GUINT32_FROM_LE(value)`<a name="guint32-from-le"></a>

:   Converts a `uint32_t` value from little-endian to host byte order.

`GUINT32_TO_BE(value)`<a name="guint32-to-be"></a>

:   Converts a `uint32_t` value from host byte order to big-endian.

`GUINT32_TO_LE(value)`<a name="guint32-to-le"></a>

:   Converts a `uint32_t` value from host byte order to little-endian.

`GUINT64_FROM_BE(value)`<a name="guint64-from-be"></a>

:   Converts a `uint64_t` value from big-endian to host byte order.

`GUINT64_FROM_LE(value)`<a name="guint64-from-le"></a>

:   Converts a `uint64_t` value from little-endian to host byte order.

`GUINT64_TO_BE(value)`<a name="guint64-to-be"></a>

:   Converts a `uint64_t` value from host byte order to big-endian.

`GUINT64_TO_LE(value)`<a name="guint64-to-le"></a>

:   Converts a `uint64_t` value from host byte order to little-endian.

`GUINT16_SWAP_BE_PDP(value)`<a name="guint16-swap-be-pdp"></a>

:   Converts a `uint16_t` value between big-endian and pdp-endian byte order.
    The conversion is symmetric so it can be used both ways.

`GUINT16_SWAP_LE_BE(value)`<a name="guint16-swap-le-be"></a>

:   Converts a `uint16_t` value between little-endian and big-endian byte order.
    The conversion is symmetric so it can be used both ways.

`GUINT16_SWAP_LE_PDP(value)`<a name="guint16-swap-le-pdp"></a>

:   Converts a `uint16_t` value between little-endian and pdp-endian byte order.
    The conversion is symmetric so it can be used both ways.

`GUINT32_SWAP_BE_PDP(value)`<a name="guint32-swap-be-pdp"></a>

:   Converts a `uint32_t` value between big-endian and pdp-endian byte order.
    The conversion is symmetric so it can be used both ways.

`GUINT32_SWAP_LE_BE(value)`<a name="guint32-swap-le-be"></a>

:   Converts a `uint32_t` value between little-endian and big-endian byte order.
    The conversion is symmetric so it can be used both ways.

`GUINT32_SWAP_LE_PDP(value)`<a name="guint32-swap-le-pdp"></a>

:   Converts a `uint32_t` value between little-endian and pdp-endian byte order.
    The conversion is symmetric so it can be used both ways.

`GUINT64_SWAP_LE_BE(value)`<a name="guint64-swap-le-be"></a>

:   Converts a `uint64_t` value between little-endian and big-endian byte order.
    The conversion is symmetric so it can be used both ways.