诠释为char铸造 [英] int to char casting

查看:166
本文介绍了诠释为char铸造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int i = 259;       /* 03010000 in Little Endian ; 00000103 in Big Endian */
char c = (char)i;  /* returns 03 in both Little and Big Endian?? */

在我的电脑03分配到char C ,我有小端,但我不知道,如果焦炭铸造读取至少显著字节或读取由指出字节我变量。

In my computer it assigns 03 to char c and I have Little Endian, but I don't know if the char casting reads the least significant byte or reads the byte pointed by the i variable.

推荐答案

字节序实际上并没有改变任何东西。它不会尝试存储其中一个字节(MSB,LSB等等)。

Endianness doesn't actually change anything here. It doesn't try to store one of the bytes (MSB, LSB etc).


  • 如果字符是无符号,将环绕。假设8位字符259%256 = 3

  • 如果字符签署结果是实现默认。谢谢 PMG :6.3.1.3/3在C99标准

  • If char is unsigned it will wrap around. Assuming 8-bit char 259 % 256 = 3
  • If char is signed the result is implementation default. Thank you pmg: 6.3.1.3/3 in the C99 Standard

这篇关于诠释为char铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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