1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 a portable way to convert between different byte orders.
7 <!-- ##### SECTION Long_Description ##### -->
9 These macros provide a portable way to determine the host byte order
10 and to convert values between different byte orders.
13 The byte order is the order in which bytes are stored to create larger
14 data types such as the #gint and #glong values.
15 The host byte order is the byte order used on the current machine.
18 Some processors store the most significant bytes (i.e. the bytes that
19 hold the largest part of the value) first. These are known as big-endian
23 Other processors (notably the x86 family) store the most significant byte
24 last. These are known as little-endian processors.
27 Finally, to complicate matters, some other processors store the bytes in
28 a rather curious order known as PDP-endian. For a 4-byte word, the 3rd
29 most significant byte is stored first, then the 4th, then the 1st and finally
33 Obviously there is a problem when these different processors communicate
34 with each other, for example over networks or by using binary file formats.
35 This is where these macros come in.
36 They are typically used to convert values into a byte order
37 which has been agreed on for use when communicating between different
38 processors. The Internet uses what is known as 'network byte order'
39 as the standard byte order (which is in fact the big-endian byte order).
42 Note that the byte order conversion macros may evaluate their arguments
43 multiple times, thus you should not use them with arguments which have
47 <!-- ##### SECTION See_Also ##### -->
52 <!-- ##### SECTION Stability_Level ##### -->
55 <!-- ##### MACRO G_BYTE_ORDER ##### -->
58 This can be either #G_LITTLE_ENDIAN or #G_BIG_ENDIAN (support for
59 #G_PDP_ENDIAN may be added in future.)
64 <!-- ##### MACRO G_LITTLE_ENDIAN ##### -->
66 Specifies one of the possible types of byte order.
72 <!-- ##### MACRO G_BIG_ENDIAN ##### -->
74 Specifies one of the possible types of byte order.
80 <!-- ##### MACRO G_PDP_ENDIAN ##### -->
82 Specifies one of the possible types of byte order (currently unused).
88 <!-- ##### MACRO g_htonl ##### -->
90 Converts a 32-bit integer value from host to network byte order.
93 @val: a 32-bit integer value in host byte order.
94 @Returns: @val converted to network byte order.
97 <!-- ##### MACRO g_htons ##### -->
99 Converts a 16-bit integer value from host to network byte order.
102 @val: a 16-bit integer value in host byte order.
103 @Returns: @val converted to network byte order.
106 <!-- ##### MACRO g_ntohl ##### -->
108 Converts a 32-bit integer value from network to host byte order.
111 @val: a 32-bit integer value in network byte order.
112 @Returns: @val converted to host byte order.
115 <!-- ##### MACRO g_ntohs ##### -->
117 Converts a 16-bit integer value from network to host byte order.
120 @val: a 16-bit integer value in network byte order.
121 @Returns: @val converted to host byte order.
124 <!-- ##### MACRO GINT_FROM_BE ##### -->
126 Converts a #gint value from big-endian to host byte order.
129 @val: a #gint value in big-endian byte order.
130 @Returns: @val converted to host byte order.
133 <!-- ##### MACRO GINT_FROM_LE ##### -->
135 Converts a #gint value from little-endian to host byte order.
138 @val: a #gint value in little-endian byte order.
139 @Returns: @val converted to host byte order.
142 <!-- ##### MACRO GINT_TO_BE ##### -->
144 Converts a #gint value from host byte order to big-endian.
147 @val: a #gint value in host byte order.
148 @Returns: @val converted to big-endian byte order.
151 <!-- ##### MACRO GINT_TO_LE ##### -->
153 Converts a #gint value from host byte order to little-endian.
156 @val: a #gint value in host byte order.
157 @Returns: @val converted to little-endian byte order.
160 <!-- ##### MACRO GUINT_FROM_BE ##### -->
162 Converts a #guint value from big-endian to host byte order.
165 @val: a #guint value in big-endian byte order.
166 @Returns: @val converted to host byte order.
169 <!-- ##### MACRO GUINT_FROM_LE ##### -->
171 Converts a #guint value from little-endian to host byte order.
174 @val: a #guint value in little-endian byte order.
175 @Returns: @val converted to host byte order.
178 <!-- ##### MACRO GUINT_TO_BE ##### -->
180 Converts a #guint value from host byte order to big-endian.
183 @val: a #guint value in host byte order.
184 @Returns: @val converted to big-endian byte order.
187 <!-- ##### MACRO GUINT_TO_LE ##### -->
189 Converts a #guint value from host byte order to little-endian.
192 @val: a #guint value in host byte order.
193 @Returns: @val converted to little-endian byte order.
196 <!-- ##### MACRO GLONG_FROM_BE ##### -->
198 Converts a #glong value from big-endian to the host byte order.
201 @val: a #glong value in big-endian byte order.
202 @Returns: @val converted to host byte order.
205 <!-- ##### MACRO GLONG_FROM_LE ##### -->
207 Converts a #glong value from little-endian to host byte order.
210 @val: a #glong value in little-endian byte order.
211 @Returns: @val converted to host byte order.
214 <!-- ##### MACRO GLONG_TO_BE ##### -->
216 Converts a #glong value from host byte order to big-endian.
219 @val: a #glong value in host byte order.
220 @Returns: @val converted to big-endian byte order.
223 <!-- ##### MACRO GLONG_TO_LE ##### -->
225 Converts a #glong value from host byte order to little-endian.
228 @val: a #glong value in host byte order.
229 @Returns: @val converted to little-endian.
232 <!-- ##### MACRO GULONG_FROM_BE ##### -->
234 Converts a #gulong value from big-endian to host byte order.
237 @val: a #gulong value in big-endian byte order.
238 @Returns: @val converted to host byte order.
241 <!-- ##### MACRO GULONG_FROM_LE ##### -->
243 Converts a #gulong value from little-endian to host byte order.
246 @val: a #gulong value in little-endian byte order.
247 @Returns: @val converted to host byte order.
250 <!-- ##### MACRO GULONG_TO_BE ##### -->
252 Converts a #gulong value from host byte order to big-endian.
255 @val: a #gulong value in host byte order.
256 @Returns: @val converted to big-endian.
259 <!-- ##### MACRO GULONG_TO_LE ##### -->
261 Converts a #gulong value from host byte order to little-endian.
264 @val: a #gulong value in host byte order.
265 @Returns: @val converted to little-endian.
268 <!-- ##### MACRO GINT16_FROM_BE ##### -->
270 Converts a #gint16 value from big-endian to host byte order.
273 @val: a #gint16 value in big-endian byte order.
274 @Returns: @val converted to host byte order.
277 <!-- ##### MACRO GINT16_FROM_LE ##### -->
279 Converts a #gint16 value from little-endian to host byte order.
282 @val: a #gint16 value in little-endian byte order.
283 @Returns: @val converted to host byte order.
286 <!-- ##### MACRO GINT16_TO_BE ##### -->
288 Converts a #gint16 value from host byte order to big-endian.
291 @val: a #gint16 value in host byte order.
292 @Returns: @val converted to big-endian.
295 <!-- ##### MACRO GINT16_TO_LE ##### -->
297 Converts a #gint16 value from host byte order to little-endian.
300 @val: a #gint16 value in host byte order.
301 @Returns: @val converted to little-endian.
304 <!-- ##### MACRO GUINT16_FROM_BE ##### -->
306 Converts a #guint16 value from big-endian to host byte order.
309 @val: a #guint16 value in big-endian byte order.
310 @Returns: @val converted to host byte order.
313 <!-- ##### MACRO GUINT16_FROM_LE ##### -->
315 Converts a #guint16 value from little-endian to host byte order.
318 @val: a #guint16 value in little-endian byte order.
319 @Returns: @val converted to host byte order.
322 <!-- ##### MACRO GUINT16_TO_BE ##### -->
324 Converts a #guint16 value from host byte order to big-endian.
327 @val: a #guint16 value in host byte order.
328 @Returns: @val converted to big-endian.
331 <!-- ##### MACRO GUINT16_TO_LE ##### -->
333 Converts a #guint16 value from host byte order to little-endian.
336 @val: a #guint16 value in host byte order.
337 @Returns: @val converted to little-endian.
340 <!-- ##### MACRO GINT32_FROM_BE ##### -->
342 Converts a #gint32 value from big-endian to host byte order.
345 @val: a #gint32 value in big-endian byte order.
346 @Returns: @val converted to host byte order.
349 <!-- ##### MACRO GINT32_FROM_LE ##### -->
351 Converts a #gint32 value from little-endian to host byte order.
354 @val: a #gint32 value in little-endian byte order.
355 @Returns: @val converted to host byte order.
358 <!-- ##### MACRO GINT32_TO_BE ##### -->
360 Converts a #gint32 value from host byte order to big-endian.
363 @val: a #gint32 value in host byte order.
364 @Returns: @val converted to big-endian.
367 <!-- ##### MACRO GINT32_TO_LE ##### -->
369 Converts a #gint32 value from host byte order to little-endian.
372 @val: a #gint32 value in host byte order.
373 @Returns: @val converted to little-endian.
376 <!-- ##### MACRO GUINT32_FROM_BE ##### -->
378 Converts a #guint32 value from big-endian to host byte order.
381 @val: a #guint32 value in big-endian byte order.
382 @Returns: @val converted to host byte order.
385 <!-- ##### MACRO GUINT32_FROM_LE ##### -->
387 Converts a #guint32 value from little-endian to host byte order.
390 @val: a #guint32 value in little-endian byte order.
391 @Returns: @val converted to host byte order.
394 <!-- ##### MACRO GUINT32_TO_BE ##### -->
396 Converts a #guint32 value from host byte order to big-endian.
399 @val: a #guint32 value in host byte order.
400 @Returns: @val converted to big-endian.
403 <!-- ##### MACRO GUINT32_TO_LE ##### -->
405 Converts a #guint32 value from host byte order to little-endian.
408 @val: a #guint32 value in host byte order.
409 @Returns: @val converted to little-endian.
412 <!-- ##### MACRO GINT64_FROM_BE ##### -->
414 Converts a #gint64 value from big-endian to host byte order.
417 @val: a #gint64 value in big-endian byte order.
418 @Returns: @val converted to host byte order.
421 <!-- ##### MACRO GINT64_FROM_LE ##### -->
423 Converts a #gint64 value from little-endian to host byte order.
426 @val: a #gint64 value in little-endian byte order.
427 @Returns: @val converted to host byte order.
430 <!-- ##### MACRO GINT64_TO_BE ##### -->
432 Converts a #gint64 value from host byte order to big-endian.
435 @val: a #gint64 value in host byte order.
436 @Returns: @val converted to big-endian.
439 <!-- ##### MACRO GINT64_TO_LE ##### -->
441 Converts a #gint64 value from host byte order to little-endian.
444 @val: a #gint64 value in host byte order.
445 @Returns: @val converted to little-endian.
448 <!-- ##### MACRO GUINT64_FROM_BE ##### -->
450 Converts a #guint64 value from big-endian to host byte order.
453 @val: a #guint64 value in big-endian byte order.
454 @Returns: @val converted to host byte order.
457 <!-- ##### MACRO GUINT64_FROM_LE ##### -->
459 Converts a #guint64 value from little-endian to host byte order.
462 @val: a #guint64 value in little-endian byte order.
463 @Returns: @val converted to host byte order.
466 <!-- ##### MACRO GUINT64_TO_BE ##### -->
468 Converts a #guint64 value from host byte order to big-endian.
471 @val: a #guint64 value in host byte order.
472 @Returns: @val converted to big-endian.
475 <!-- ##### MACRO GUINT64_TO_LE ##### -->
477 Converts a #guint64 value from host byte order to little-endian.
480 @val: a #guint64 value in host byte order.
481 @Returns: @val converted to little-endian.
484 <!-- ##### MACRO GUINT16_SWAP_BE_PDP ##### -->
486 Converts a #guint16 value between big-endian and pdp-endian byte order.
487 The conversion is symmetric so it can be used both ways.
490 @val: a #guint16 value in big-endian or pdp-endian byte order.
491 @Returns: @val converted to the opposite byte order.
494 <!-- ##### MACRO GUINT16_SWAP_LE_BE ##### -->
496 Converts a #guint16 value between little-endian and big-endian byte order.
497 The conversion is symmetric so it can be used both ways.
500 @val: a #guint16 value in little-endian or big-endian byte order.
501 @Returns: @val converted to the opposite byte order.
504 <!-- ##### MACRO GUINT16_SWAP_LE_PDP ##### -->
506 Converts a #guint16 value between little-endian and pdp-endian byte order.
507 The conversion is symmetric so it can be used both ways.
510 @val: a #guint16 value in little-endian or pdp-endian byte order.
511 @Returns: @val converted to the opposite byte order.
514 <!-- ##### MACRO GUINT32_SWAP_BE_PDP ##### -->
516 Converts a #guint32 value between big-endian and pdp-endian byte order.
517 The conversion is symmetric so it can be used both ways.
520 @val: a #guint32 value in big-endian or pdp-endian byte order.
521 @Returns: @val converted to the opposite byte order.
524 <!-- ##### MACRO GUINT32_SWAP_LE_BE ##### -->
526 Converts a #guint32 value between little-endian and big-endian byte order.
527 The conversion is symmetric so it can be used both ways.
530 @val: a #guint32 value in little-endian or big-endian byte order.
531 @Returns: @val converted to the opposite byte order.
534 <!-- ##### MACRO GUINT32_SWAP_LE_PDP ##### -->
536 Converts a #guint32 value between little-endian and pdp-endian byte order.
537 The conversion is symmetric so it can be used both ways.
540 @val: a #guint32 value in little-endian or pdp-endian byte order.
541 @Returns: @val converted to the opposite byte order.
544 <!-- ##### MACRO GUINT64_SWAP_LE_BE ##### -->
546 Converts a #guint64 value between little-endian and big-endian byte order.
547 The conversion is symmetric so it can be used both ways.
550 @val: a #guint64 value in little-endian or big-endian byte order.
551 @Returns: @val converted to the opposite byte order.