Reputation: 1707
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.
Here is the Iplimage header:
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
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??
Thanks in advance.
Upvotes: 2
Views: 148
Reputation: 40887
You need cast ID
to double before printing it:
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
Upvotes: 1
Reputation: 17312
Use the %d
format specifier since frame->ID
is an integer:
snprintf(IDbuffer,10,"%d",frame->ID);
And then use the %s
format specifier to print the buffer:
printf("frame->ID= %s\n",IDbuffer);
There's more information about the format specifiers in printf man page.
Upvotes: 4
Reputation: 320421
%e
format specifier requires an argument of type double
, while you are passing an int
instead. The resultant behavior is undefined. That's all there is to it.
By using %e
you are making a promise to snprintf
. You promise that you will supply a double
argument in the corresponding position. Later you break that promise by supplying an int
instead of double
, which leads to a meaningless result. You are essentially lying to snprintf
about the type of frame->ID
.
%a
also requires a double
argument. Why do you insist on using double
format specifiers with a int
argument? 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
). Either this or that. Mixing things up like you do will not achieve anything.
Upvotes: 2