c语言处理大小端问题

素衣白马客京华 2013-06-25 04:02:13
有这么一段程序
#include <stdio.h>
#include <stdint.h>
struct abc {
uint16_t i;
};
int main()
{
struct abc info;
scanf("%x",&(info.i));
printf("%x\n",info.i);
}

在pc上运行正常,输入0x01显示0x01
但在powerpc上运行,输入0x01,显示0x0
poerpc上都是大端模式,应该怎么解决呢
...全文
330 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
DaiwjDev 2013-06-26
  • 打赏
  • 举报
回复
引用 5 楼 nice_cxf 的回复:
怎么这么多人误导,这个显然跟大小端有关啊,不过最根本原因是越界了 uint16_t是2字节,%d是4字节,小端把01填入i,大端把0填入i
这个 还有点像
nice_cxf 2013-06-26
  • 打赏
  • 举报
回复
引用 13 楼 judwenwen2009 的回复:
[quote=引用 5 楼 nice_cxf 的回复:] 怎么这么多人误导,这个显然跟大小端有关啊,不过最根本原因是越界了 uint16_t是2字节,%d是4字节,小端把01填入i,大端把0填入i
那么%x也不行吗,我也觉得是scanf输入的问题,但不知道应该怎么解决[/quote] 不看回帖么?如果不改结构就用%hx,或者把uint16_t修改为uint32_t,总之scanf参数要和数据类型匹配
  • 打赏
  • 举报
回复
引用 5 楼 nice_cxf 的回复:
怎么这么多人误导,这个显然跟大小端有关啊,不过最根本原因是越界了 uint16_t是2字节,%d是4字节,小端把01填入i,大端把0填入i
那么%x也不行吗,我也觉得是scanf输入的问题,但不知道应该怎么解决
rocktyt 2013-06-26
  • 打赏
  • 举报
回复
引用 3 楼 whizer 的回复:
你可以试试用下面的code测试大小端:

#include <stdio.h>
#include <stdint.h>
union utest{
    unsigned int test_val;
    struct abc {
        unsigned int byte0:8;
        unsigned int byte1:8;
        unsigned int byte2:8;
        unsigned int byte3:8;
    };
};
int main()
{
    union utest info;
    scanf("%x",&(info.test_val));
    printf("%x\n",info.abc.byte0);
    ...
}
这种真的有点多此一举
inline bool is_little_endian()
{
    int i = 1;
    return reinterpret_cast<char&>(i);
}
mujiok2003 2013-06-26
  • 打赏
  • 举报
回复
%hx --> unsigned short
  • 打赏
  • 举报
回复
引用 3 楼 whizer 的回复:
你可以试试用下面的code测试大小端:

#include <stdio.h>
#include <stdint.h>
union utest{
    unsigned int test_val;
    struct abc {
        unsigned int byte0:8;
        unsigned int byte1:8;
        unsigned int byte2:8;
        unsigned int byte3:8;
    };
};
int main()
{
    union utest info;
    scanf("%x",&(info.test_val));
    printf("%x\n",info.abc.byte0);
    ...
}
编译报错:test_endian.c:10: warning: declaration does not declare anything test_endian.c: In function 'main': test_endian.c:16: error: 'union utest' has no member named 'abc' 求解
CedarDiao 2013-06-25
  • 打赏
  • 举报
回复
解决大小端问题,可以看看编译器是否有相关的编译选项。
AnYidan 2013-06-25
  • 打赏
  • 举报
回复
引用 5 楼 nice_cxf 的回复:
怎么这么多人误导,这个显然跟大小端有关啊,不过最根本原因是越界了 uint16_t是2字节,%d是4字节,小端把01填入i,大端把0填入i
类型不匹配,
cq08meng 2013-06-25
  • 打赏
  • 举报
回复
如果大端把0填入i,楼主可以试验在powerpc中输入0x10,看下输出是什么?
nice_cxf 2013-06-25
  • 打赏
  • 举报
回复
怎么这么多人误导,这个显然跟大小端有关啊,不过最根本原因是越界了 uint16_t是2字节,%d是4字节,小端把01填入i,大端把0填入i
xiangzhihappy 2013-06-25
  • 打赏
  • 举报
回复
引用 3 楼 whizer 的回复:
你可以试试用下面的code测试大小端:

#include <stdio.h>
#include <stdint.h>
union utest{
    unsigned int test_val;
    struct abc {
        unsigned int byte0:8;
        unsigned int byte1:8;
        unsigned int byte2:8;
        unsigned int byte3:8;
    };
};
int main()
{
    union utest info;
    scanf("%x",&(info.test_val));
    printf("%x\n",info.abc.byte0);
    ...
}
学习了,这个不错
whizer 2013-06-25
  • 打赏
  • 举报
回复
你可以试试用下面的code测试大小端:

#include <stdio.h>
#include <stdint.h>
union utest{
    unsigned int test_val;
    struct abc {
        unsigned int byte0:8;
        unsigned int byte1:8;
        unsigned int byte2:8;
        unsigned int byte3:8;
    };
};
int main()
{
    union utest info;
    scanf("%x",&(info.test_val));
    printf("%x\n",info.abc.byte0);
    ...
}
图灵狗 2013-06-25
  • 打赏
  • 举报
回复
跟大小端没有关系,你先换用%d测试看看。
引用 楼主 judwenwen2009 的回复:
有这么一段程序
#include <stdio.h>
#include <stdint.h>
struct abc {
    uint16_t i;
};
int main()
{
    struct abc info;
    scanf("%x",&(info.i));
    printf("%x\n",info.i);
}
在pc上运行正常,输入0x01显示0x01 但在powerpc上运行,输入0x01,显示0x0 poerpc上都是大端模式,应该怎么解决呢
www_adintr_com 2013-06-25
  • 打赏
  • 举报
回复
这个应该和大小端没有关系. 你里面又没有吧 i 分开来用. 看看是不是你的 powerpc 上的编译器不识别 0x, 只输入 1 试试.

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧