我正在寻找有关基本C++类型大小的详细信息.我知道这取决于架构(16位,32位,64位)和编译器.
但是有没有C++的标准?
我在32位架构上使用Visual Studio 2008.这是我得到的:
char : 1 byte
short : 2 bytes
int : 4 bytes
long : 4 bytes
float : 4 bytes
double: 8 bytes
Run Code Online (Sandbox Code Playgroud)
我试图找到,但没有成功,可靠的信息,表述的大小char,short,int,long,double,float(和其他类型的我没想到的),在不同的体系结构和编译器.
#include <iostream>
using namespace std;
int main()
{
char c1 = 0xab;
signed char c2 = 0xcd;
unsigned char c3 = 0xef;
cout << hex;
cout << c1 << endl;
cout << c2 << endl;
cout << c3 << endl;
}
Run Code Online (Sandbox Code Playgroud)
我预计输出如下:
ab
cd
ef
Run Code Online (Sandbox Code Playgroud)
然而,我一无所获.
我想这是因为cout总是将'char','signed char'和'unsigned char'视为字符而不是8位整数.但是,'char','signed char'和'unsigned char'都是完整的类型.
所以我的问题是:如何通过cout将字符输出为整数?
PS:static_cast(...)很难看,需要更多的工作来修剪额外的比特.
我有以下代码。当我运行程序时,屏幕上出现未知字符而不是像素值。我想显示像素值。我该怎么做呢?谢谢你。
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("/home/fd/baby.jpg");
for( int i = 0 ; i < image.rows ; i++)
{
for( int j = 0 ; j < image.cols ; j++ )
{
if(image.type() == CV_8UC1)
{
image.at<uchar>(i,j) = 255;
}
else if(image.type() == CV_8UC3)
{
cout << image.at<Vec3b>(i,j)[0] << " " << image.at<Vec3b>(i,j)[1] << " " << image.at<Vec3b>(i,j)[2] << endl;
image.at<Vec3b>(i,j)[0] = 255;
image.at<Vec3b>(i,j)[1] = …Run Code Online (Sandbox Code Playgroud) 如何将一个uint16_t转换为两部分?
uint16_t value = 0x7133;
uint8_t partA = (uint8_t)((value & 0xFF00) >> 8);
uint8_t partB = (uint8_t)(value & 0x00FF);
std::cout << std::hex << partA << std::endl;
std::cout << std::hex << partB << std::endl;
Run Code Online (Sandbox Code Playgroud)
对于上面的代码,我用partAas q和partBas 3代替0x71and 0x33。