简体   繁体   English

C中的奇怪int输出

[英]Strange int output in C

I am using OpenCV and I need to convert Iplimage->ID to char[ ] so that I can send it using TCP and then reconvert it to int on the server. 我正在使用OpenCV,我需要将Iplimage-> ID转换为char [],以便我可以使用TCP发送它,然后在服务器上将其重新转换为int。

Here is the Iplimage header: 这是Iplimage标头:

typedef struct _IplImage
{
    int  nSize;
    int  ID;  //<--- ID of type INT
    int  nChannels;
    int  alphaChannel;
    int  depth;
    char colorModel[4];
    char channelSeq[4];
    int  dataOrder;
    int  origin;
    int  align;
    int  width;
    int  height;
    struct _IplROI *roi;
    struct _IplImage *maskROI;
    void  *imageId;
    struct _IplTileInfo *tileInfo;
    int  imageSize;
    char *imageData;
    int  widthStep;
    int  BorderMode[4];
    int  BorderConst[4];
    char *imageDataOrigin;
}
IplImage;

this is my code: 这是我的代码:

char IDbuffer[10];
snprintf(IDbuffer,10,"%e",frame->ID);//where frame is of type IplImage*
printf("frame->ID= %a\n",IDbuffer);

and this what I got printed: 这是我得到的:

frame->ID= 0x0.0000000037d0cp-1022 框架-> ID = 0x0.0000000037d0cp-1022

even trying 甚至尝试

printf("frame->ID= %a\n",frame->ID);

give me the same output. 给我同样的输出。

Is this an integer format ?? 这是整数格式吗? and if yes how could I convert the char * of this format to an int?? 如果是的话,如何将这种格式的char *转换为int?

Thanks in advance. 提前致谢。

Use the %d format specifier since frame->ID is an integer: 使用%d格式说明符,因为frame->ID是一个整数:

snprintf(IDbuffer,10,"%d",frame->ID);

And then use the %s format specifier to print the buffer: 然后使用%s格式说明符来打印缓冲区:

printf("frame->ID= %s\n",IDbuffer);

There's more information about the format specifiers in printf man page. 有关printf手册页中格式说明符的更多信息。

%e format specifier requires an argument of type double , while you are passing an int instead. %e格式说明符在传递int时需要double类型的参数。 The resultant behavior is undefined. 结果行为是不确定的。 That's all there is to it. 这里的所有都是它的。

By using %e you are making a promise to snprintf . 通过使用%e您可以承诺使用snprintf You promise that you will supply a double argument in the corresponding position. 您保证在相应位置提供double参数。 Later you break that promise by supplying an int instead of double , which leads to a meaningless result. 稍后,您通过提供int而不是double来破坏了诺言,这导致了毫无意义的结果。 You are essentially lying to snprintf about the type of frame->ID . 您实质上是对snprintf说谎关于frame->ID的类型。

%a also requires a double argument. %a还需要一个double参数。 Why do you insist on using double format specifiers with a int argument? 为什么您坚持使用带int参数的double格式说明符? How do you expect this to work? 你觉得这个怎么样?

Either supply an argument of correct type (which is double ), or use the proper format specifier (which is %d for int ). 提供正确类型的参数( double ),或使用正确的格式说明符( int %d )。 Either this or that. 这个或那个。 Mixing things up like you do will not achieve anything. 像你一样混合起来将无法实现任何目标。

You need cast ID to double before printing it: 您需要在转换前将演员表ID加倍:

snprintf(IDbuffer,10,"%e", (double)frame->ID);

Alternatively, you can print it as integer: 或者,您可以将其打印为整数:

snprintf(IDbuffer,10,"%d", frame->ID)

Check this for more information on snprintf 检查此以获取有关snprintf的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM