std::cout 输出 unsigned char类型数据

写C++代码的时候可能发现,输出char, float, int, 还有double 等类型的时候正常,但是遇到unsigned char有时候就输出乱码。
所以自己测试一下:

int main(int argc, char** argv)
{
	std::cout << "test for uchar" << std::endl;
	int h = 255;
	//unsigned char u = (unsigned char)(h);

	unsigned char u = '255';
	char c = 'a';
	float f = 1.0f;
	double d = 1.0f;
	int i = 1;

	std::cout << "unsigned char: " << u << std::endl;
	std::cout << "char: " << c << std::endl;
	std::cout << "float: " << f << std::endl;
	std::cout << "double: " << d << std::endl;
	std::cout << "int: " << i << std::endl;
}

输出的结果正常:
在这里插入图片描述

unsigned char 一般是0-255之间,当我们赋值的方式给予一个值的时候:

int main(int argc, char** argv)
{
	std::cout << "test for uchar" << std::endl;
	int h = 255;
	unsigned char u = (unsigned char)(h);

	//unsigned char u = '255';
	char c = 'a';
	float f = 1.0f;
	double d = 1.0f;
	int i = 1;

	std::cout << "unsigned char: " << u << std::endl;
	std::cout << "char: " << c << std::endl;
	std::cout << "float: " << f << std::endl;
	std::cout << "double: " << d << std::endl;
	std::cout << "int: " << i << std::endl;
}

输出的结果为垃圾值:
在这里插入图片描述
It will not work because here u(unsigned char) is a unsigned char (with value 255), cout actually will print some garbage value (or nothing) as it is just a non-printable ASCII character which is getting printed anyway. Note that ASCII character corresponding to 255 is non-printable. Check out here for the ASCII table.
P.S. You can check whether u is printable or not using isprint() as
上面的意思是:当赋予这个超过了ASCII的范围了,所以打印不出来,或者是垃圾值。

最后测试:
当赋予u为正常的正常的值的时候,97对应为’a’,输出为‘a’,测试结果如下:
测试代码:

int main(int argc, char** argv)
{
	std::cout << "test for uchar" << std::endl;
	int h = 97;
	unsigned char u = (unsigned char)(h);

	//unsigned char u = '255';
	char c = 'a';
	float f = 1.0f;
	double d = 1.0f;
	int i = 1;

	std::cout << "unsigned char: " << u << std::endl;
	std::cout << "char: " << c << std::endl;
	std::cout << "float: " << f << std::endl;
	std::cout << "double: " << d << std::endl;
	std::cout << "int: " << i << std::endl;
}

测试结果
在这里插入图片描述
所以,std::cout出不来的时候,可以用强制转换为int,cout就可以了。
代码如下:

int main(int argc, char** argv)
{
	std::cout << "test for uchar" << std::endl;
	int h = 255;
	unsigned char u = (unsigned char)(h);

	//unsigned char u = '255';
	char c = 'a';
	float f = 1.0f;
	double d = 1.0f;
	int i = 1;

	std::cout << "unsigned char: " << (int)u << std::endl;
	std::cout << "char: " << c << std::endl;
	std::cout << "float: " << f << std::endl;
	std::cout << "double: " << d << std::endl;
	std::cout << "int: " << i << std::endl;
}

测试的结果:
在这里插入图片描述
参见的材料如下:
https://www.it1352.com/457126.html

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言版课程设计 #include #include 产生随机数 #include 调用计算时间的函数 #include using namespace std; 产生随机数,输入产生随机数的个数,范围,即可产生待排数据 void Random(long p[],long n,long a,long b) { long max,min; if(a>b) { max=a-b+1; min=b; } else { max=b-a+1; min=a; } srand((unsigned)time(NULL)); for(long i=1;i<=n;i++) p[i]=rand()%max+min; } 输出排序后的结果;用已检测排序的正确,是否能正确排序 void Print(long p[],long n) { cout<<"\n排序后的结果:"; for(long i=1;i<=n;i++) cout<<p[i]<<' '; } void Initiate(long p[],long q[],long n) { for(long i=1;i<=n;i++) q[i]=p[i]; } 直接插入排序:排序并测试排序过程中的时间消耗 void StraightInsertionSort(long p[],long n) { long *q=new long[n+1]; Initiate(p,q,n); LARGE_INTEGER m_liPerfFreq={0}; QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart={0}; QueryPerformanceCounter(&m_liPerfStart); for(long i=2;i<=n;++i) if(q[i]<q[i-1]) { q[0]=q[i]; q[i]=q[i-1]; for(long j=i-2;q[0]<q[j];--j) q[j+1]=q[j]; q[j+1]=q[0]; } LARGE_INTEGER liPerfNow={0}; QueryPerformanceCounter(&liPerfNow); double time=liPerfNow.QuadPart - m_liPerfStart.QuadPart; time/=m_liPerfFreq.QuadPart; // Print(q,n); cout<<"\n直接插入排序时间消耗:"<<time<<" MS"; delete []q; } 折半插入排序:排序并测试排序过程中的时间消耗 void BinaryInsertionSort(long p[],long n) { long *q=new long[n+1]; Initiate(p,q,n); LARGE_INTEGER m_liPerfFreq={0}; QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart={0}; QueryPerformanceCounter(&m_liPerfStart); for(long i=2;i<n;i++) { q[0]=q[i]; long low=1; long high=i-1; while(low<=high) { long m=(low+high)/2; if(q[0]=high+1;--j) q[j+1]=q[j]; q[high+1]=q[0]; } LARGE_INTEGER liPerfNow={0}; QueryPerformanceCounter(&liPerfNow); double time=liPerfNow.QuadPart - m_liPerfStart.QuadPart; time/=m_liPerfFreq.QuadPart; // Print(q,n); cout<<"\n折半插入排序时间消耗:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值