如何转换__u32在Linux内核__be32 [英] How to convert __u32 to __be32 in Linux Kernel

查看:2959
本文介绍了如何转换__u32在Linux内核__be32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量

  __ BE32 X;

我有一个函数

  __ U32富(无效){
      __u32一个;
      返回;
}

我需要存储的foo变量返回X

  X = htonl(富());

是不是正确的?
我有一个困惑什么是返回类型 ntohl() htonl()的。他们是彼此的对立面?

要检查输出,我需要重新编译内核,我不想麻烦我的任何错误的系统。所以我问在这里。


解决方案

您可以使用kernel.h定义的宏:

<一个href=\"http://www.bruceblinn.com/linuxinfo/ByteOrder.html\">http://www.bruceblinn.com/linuxinfo/ByteOrder.html


  

下面的宏返回它已转换后的值。
  注:在linux / kernel.h头文件是头文件,应该是
  包括在其中使用这些宏的源文件,但它是
  不要在那里的宏定义实际的头文件。


 的#include&LT;的Linux / kernel.h&GT;
__u16 le16_to_cpu(常量__le16);
__u32 le32_to_cpu(常量__le32);
__u64 le64_to_cpu(常量__le64);__le16 cpu_to_le16(常量__u16);
__le32 cpu_to_le32(常量__u32);
__le64 cpu_to_le64(常量__u64);__u16 be16_to_cpu(常量__be16);
__u32 be32_to_cpu(常量__be32);
__u64 be64_to_cpu(常量__be64);__be16 cpu_to_be16(常量__u16);
__be32 cpu_to_be32(常量__u32);
__be64 cpu_to_be64(常量__u64);

I've a variable

__be32 x;

I've a function

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

I need to store the return of foo in variable x.

x=htonl(foo());

Is it correct? I've a confusion that what are the return types of ntohl() and htonl(). Are they opposite of each other?

To check the output, I need to recompile the kernel and I don't want to trouble my system with any errors. So I'm asking here.

解决方案

You can use the macros defined in kernel.h:

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

The following macros return the value after it has been converted. Note: the linux/kernel.h header file is the header file that should be included in the source files where these macros are used, but it is not the header file where the macros are actually defined.

#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);

这篇关于如何转换__u32在Linux内核__be32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆