How to convert __u32 to __be32 to the Linux kernel

I have a variable

__be32 x;

I have a function

__u32 foo(void){
      __u32 a;
      return a;
}

I need to save the return of foo in a variable x.

x=htonl(foo());

Is it correct? I am embarrassed that the returned types ntohl()and htonl(). Are they the opposite of each other?

To check the output, I need to recompile the kernel, and I do not want to disturb the system with any errors. Therefore, I ask here.

+3
source share
1 answer

You can use the macros defined in kernel.h:

http://www.bruceblinn.com/linuxinfo/ByteOrder.html

. : linux/kernel.h , , , , .

#include <linux/kernel.h>
__u16   le16_to_cpu(const __le16);
__u32   le32_to_cpu(const __le32);
__u64   le64_to_cpu(const __le64);

__le16  cpu_to_le16(const __u16);
__le32  cpu_to_le32(const __u32);
__le64  cpu_to_le64(const __u64);

__u16   be16_to_cpu(const __be16);
__u32   be32_to_cpu(const __be32);
__u64   be64_to_cpu(const __be64);

__be16  cpu_to_be16(const __u16);
__be32  cpu_to_be32(const __u32);
__be64  cpu_to_be64(const __u64);
+6

All Articles