关于int、 unsigned int、 char、 short int在内存中的存储

本文深入探讨了C语言中各种数据类型的内部表示及其在不同系统中的占用空间,包括int、unsigned int、char和unsigned char等基本类型。通过具体示例,如变量赋值和打印,解释了有符号和无符号整数的表示范围及二进制形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先我们应该知道:

int在win32内存中占四个字节,以%d输出时有符号数则要去除一个符号位,可以表示的数据范围是-2^31~(2^31)-1.

在16位中,用高位置1来表示负数,int型占两个字节共16位,32768的二进制是10000000,00000000,高位为1,系统会认为是 负数,所以32768需要用长型表示,占四个字节。最高位就不是1了。

unsigned int 也占四个字节在32位系统中,占四个字节能表示的数据是0~(2^31)-1

char在win32中占1个字节,能表示的数据范围是-128~127

unsigned char在win32中占1个字节,能表示的数据范围是0~255

#include<stdio.h>
#include<stdlib.h>
int main()
{
	char a = 0x80;
	unsigned int b = 0x80;
	unsigned int c = a;
	int d = a;
	printf("%d\n", sizeof(unsigned int));//unsigned int 占四个字节
	printf("a=%d,a=%u\n", a, a);
	printf("b=%d,b=%u\n", b, b);
	printf("c=%d,c=%u\n", c, c);//char 0x80 占一个字节,一个字节有符号输出时是-128-127,无符号是0-256
	//所以有符号时是-128而不是128。
	printf("d=%d,d=%u\n", d, d);
	//00000000 00000000 00000000 00000010 2
	//11111111 11111111 11111111 11111101 
	//11111111 11111111 11111111 11111110 -2
	//00000000 00000000 00000000 10000000 0x80
	//11111111 11111111 11111111 01111111 
	//11111111 11111111 11111111 10000000 0x-80
		
	system("pause");
	return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string>
int main()
{
#if 0
	int i=0;
	char str[1000];
	for (i = 0; i < 1000; i++)
	{
		str[i] = -1 - i;//str[255]
		//含义是当i=?的时候使得str[i]=0;str[i]是char只能存储一个字节那么1 0000 0000,就是0
		//1 0000 0000的十进制数是256,所以strlen是255.
	}
	printf("%d\n", strlen(str));
	//从第一个字符开始到‘\0’结束\0对应的ASCII码值是0(本身是字符数组),找到0就是找到了\0
	//-1-255=-256
	//265  1 0000 0000
	//-256 1 0000 0000
#endif
	int a = 0x11223344;
	short int b = a;
	char c = a;
	printf("%x,%x,%x\n", a, b, c);
	printf("%d\n", sizeof(short int));//short int两个字节
	system("pause");
	return 0;
}

 

### 将字符串转换为不同数值类型的方法 在编程中,将字符串(`string` 或 `std::string` 类型)转换为目标数值类型的过程可以通过多种方式进行。以下是针对无符号短整型 (`unsigned short`)、无符号字符型 (`unsigned char`)、无符号整型 (`unsigned int`) 和短整型 (`short`) 的具体实现方法。 --- #### 1. **使用标准库函数** C++ 提供了丰富的标准库函数用于执行此类转换操作。以下是一些常用的标准库工具: ##### (a) 转换为 `unsigned short` ```cpp #include <iostream> #include <cstdlib> // std::strtoul int main() { const char* str = "65535"; unsigned short ushort_val = static_cast<unsigned short>(std::strtoul(str, nullptr, 10)); std::cout << "Unsigned Short Value: " << ushort_val << std::endl; return 0; } ``` 此代码片段展示了如何通过 `std::strtoul` 函数将字符串安全地转换成无符号长整型后再强制转换至目标类型[^4]。 --- #### 2. **借助 C++ STL 中的 `stoi` 家族** 现代 C++ 推荐使用更简单且不易出错的 `stoi`, `stol`, 及其他变种来进行类似的转换工作: ##### (b) 转换为 `unsigned char` 由于 `unsigned char` 实际上只占用一个字节范围有限因此需要特别注意溢出问题. ```cpp #include <iostream> #include <string> int main(){ std::string sChar = "255"; unsigned char ucharVal = static_cast<unsigned char>(std::stoi(sChar)); std::cout<<"Unsigned Char Value:"<<ucharVal<<std::endl; return 0; } ``` 这里我们采用的是先调用通用版本 stoi 来获得常规意义上的 integer 数值接着再做进一步缩小类型的 cast 动作.[^2] --- #### 3. **运用 Qt 框架下的功能** 如果你正在开发基于 Qt 应用程序那么也可以考虑利用框架内部定义好的一系列辅助方法来做同样事情. ##### (c) 转变为 `unsigned int` ```cpp #include <QCoreApplication> #include <QString> #include <QDebug> void testUintConversion(const QString& input){ bool ok=false; quint32 uintValue=input.toUInt(&ok); if(ok){ qDebug()<<"Successful conversion to Unsigned Int:"<<uintValue; }else{ qDebug()<<"Failed Conversion!"; } } // Example Usage within Main Function... testUintConversion(QString("4294967295")); ``` 上述示例说明了怎样依靠 Qt 特定的数据类型(quint32代表 unsigned int )以及相应 member function 成功完成转型动作.[^2] --- #### 4. **传统方式 - 自己解析并验证输入合法性** 最后一种手段就是完全不依赖任何高级别的抽象而是手动去拆解分析源串内容从而得出最终结果. ##### (d) 处理回 `short` ```cpp #include<stdio.h> #include<limits.h> bool convertToShort(const char *str,int base,short &result){ long temp=strtol(str,NULL,base); if((temp >= SHRT_MIN && temp <=SHRT_MAX)){ result=(short)temp; return true; }else{ return false; } } int main(){ short res; if(convertToShort("-32768",10,res)) printf("Valid Short:%hd\n",res); else puts("Invalid Input!"); return 0; } ``` 这种方法虽然繁琐但是能够提供最大程度上的灵活性同时也允许开发者自行决定错误处理逻辑等等细节部分. --- ### 结论 每种技术都有各自适用场合及优缺点所在,在实际项目当中应该根据具体情况灵活选用最恰当的那个方案解决问题即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值