I am trying to convert the output buffer(character array)
of the code below to floating point format for further calculations.
Can anybody tell me how to do it.
#include "usbtmc.h"
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <inttypes.h>
#include <sys/types.h>
#include <pthread.h>
int main()
{
int myfile;
char buffer[4000];
int actual;
myfile=open("/dev/usbtmc1",O_RDWR);
if(myfile>0)
{
system("echo MEAS:VOLT:AC?>/dev/usbtmc1");
actual=read(myfile,buffer,4000);
buffer[actual] = 0;
printf("Response = \n %s\n",buffer);
close(myfile);
}
return 0;
}
The sample output for this code is
Response =
+1.29273072E-04
You may have two ways:
using double atof(const char* str)
float f;
f = (float)atof(buffer);
printf("%f",f); // here you can use f
using int sscanf( const char * s, const char * format, ...)
float f;
sscanf(buffer,"%f",&f);
printf("%f",f); // here you can use f
Related
I have a problem. I am using this code to print from text file but the program gives me a different number -such as 11732408.000000- each time. However I don't get this problem when ex is integer.
#include <stdio.h>
#include <string.h>
int main() {
char example[] ="123.12/456 ";
double ex = atof(strtok(example, "/"));
printf("%lf", ex);
return 0;
}
I could solve my problem. Thank you for your helps.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char example[20] ="123.12/456 ";
double ex=atof(strtok(example,"/"));
printf("%lf",ex);
return 0;
}
You forgot to include <stdlib.h> which contains the declaration of atof().
Your compiler is lenient and accepts your code is spite of the missing declaration, and it incorrectly infers the prototype to be int atof(char *), which causes undefined behavior when storing the return value to ex.
Hence the bogus output.
Note also that the l in the format %lf is necessary for scanf() but ignored by printf() as float arguments are implicitly converted to double when passed to vararg functions.
Here is a corrected version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char example[] = "123.12/456 ";
char *p = strtok(example, "/");
if (p != NULL) {
double ex = atof(p);
printf("%f\n", ex);
}
return 0;
}
I am trying to parse a stack of tiff files using the Library libtiff5. To do so, I first need to read the the imagelength, width and depth. But for the imagedepth, I dont get any value back. The variable a in my code (below) was used to check if the function returns any value but it doesn't.
So my question is: how I can correctly read the imagedepth parameter.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include "tiffio.h"
#include "tiff.h"
int main()
{
TIFF* tif = TIFFOpen("spots.tif", "r");
int w, h, d;
int a;
printf("%d\n", d);
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
a = TIFFGetField(tif, TIFFTAG_IMAGEDEPTH, &d);
printf("width = %d, length = %d, depth = %d\n",w, h, a );
TIFFClose(tif);
return 0;
}
I want to get 2d array from a Mat in Vivado SDSoC but Im not able to do that because as described Xilinx(XAPP1167),
cv::Mat<>.at() method and cvGet2D() function have no corresponding equivalent
function in the synthesizable library
I appreciate any help. Thank you.
The project is about face recognition system. The system first will go viola-jone face detection and then feed the output to the CNN classification.
The output of viola-jones face detection is in unsigned char*.
I plant to convert it back to Mat and obtain 2d array for the input of CNN Classifier.
#include <cstdio>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <ctime>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <sys/mman.h>
#include <malloc.h>
#include <unistd.h>
#include <sds_lib.h>
#include <opencv2/core/core.hpp>
#include <opencv2/contrib/contrib.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/core/core_c.h"
using namespace cv;
using namespace std;
void resize_2_gray(unsigned char *imageIn, unsigned char *imageOut)
{
int k=0;
int coord;
for (int i=0; i<240; i++) {
for(int j=0; j<320; j++){
//coord=(i*2*640+j*2)*3;
#pragma HLS PIPELINE II=1
coord=6*(i*640+j);
imageOut[k] = 0.2126*imageIn[coord] + 0.7152*imageIn[coord+1] + 0.0722*imageIn[coord+2] ;
k++;
}
}
}
int main(){
Mat ROI;
unsigned int cam_num =5;//camera USB port
unsigned char *image_ROI;
image_ROI = (unsigned char*)sds_alloc(sizeof(unsigned char)*56*46);
VideoCapture stream(cam_num);
stream.read(ROI);
cvtColor(ROI,ROI, CV_BGR2RGB);
resize_2_gray(ROI.data, image_ROI);
double face_2darray [56][46]={0};
int h1=3, w1=3,w2=46,h2=56;
int x_ratio = (int)((w1<<16)/w2)+1;
int y_ratio = (int)((h1<<16)/h2)+1;
int x2,y2;
//resize image before fed into classification
for (int a=0;a<h2;a++)
{
for (int b=0;b<w2;b++)
{
x2=((b*x_ratio)>>16);
y2=((a*y_ratio)>>16);
image_ROI[(a*w2)+b]=ROI.data[(y2*w1)+x2];
face_2darray[a][b]=(double)image_ROI[a+b];
face_2darray[a][b]= 2*(face_2darray[a][b]/255)-1;
}
}
Mat Image = Mat(56,46,0,image_ROI);
imshow("Output",Image);
sds_free(image_ROI);
}
I believe the synthesized version is using a streaming interface, so you can't use random access APIs, and instead must read pixels/elements one by one.
i want write program in C via bluez API
I have used this site for tutorial :
and this is my code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
int main(int argc, char **argv)
{
int dev_id = hci_get_route(NULL);
int res_scan=NULL;
int count;
inquiry_info *device_info=NULL;
res_scan = hci_inquiry(dev_id,3,255,NULL,&device_info,IREQ_CACHE_FLUSH);
printf("%i\n",res_scan);
for(count = 0;count < res_scan;count++)
{
char *name;
printf("count Before : %i\n",count);
ba2str(&(device_info+count)->bdaddr,&name);
printf("count After : %i\n",count);
printf("%s\n",&name);
}
}
and out console :
2
count Before : 0
count After : 1111833143
00:17:EB:5D:1B:86
why count value after ba2str(&(device_info+count)->bdaddr,&name); get random value ?
in that source i linked this issue wont occur !?
instead of
char *name;
...
printf("%s\n",&name);
use
char name[248] = { 0 };
...
printf("%s\n",name);
You need allocate memory before pass the variable as reference, and the best option is to do that out of the loop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
int main(int argc, char **argv)
{
int dev_id = hci_get_route(NULL);
int res_scan=NULL;
int count;
char *name = (char *) malloc(248*sizeof(char));
inquiry_info *device_info=NULL;
res_scan = hci_inquiry(dev_id,3,255,NULL,&device_info,IREQ_CACHE_FLUSH);
printf("%i\n",res_scan);
for(count = 0;count < res_scan;count++)
{
printf("count Before : %i\n",count);
ba2str(&(device_info+count)->bdaddr,name);
printf("count After : %i\n",count);
printf("%s\n",name);
}
free(name);
}
doing that your code will be faster because you will allocate memory only one time.
I'm trying to compile a C method
#include <stdio.matrixVector>
#include <gsl/gsl_matrix.matrixVector>
#include <gsl/gsl_math.matrixVector>
#include <gsl/gsl_multifit.matrixVector>
void myMethod(double vector[], double matrixVector[])
{
int n = sizeof(matrixVector)/sizeof(double);
gsl_matrix *X = gsl_matrix_calloc(n, 3);
gsl_vector *Y = gsl_vector_alloc(n);
gsl_vector *beta = gsl_vector_alloc(3);
for (int i = 0; i < n; i++) {
....
}
....
}
but I get this error
fatal error: stdio.matrixVector: File or directory not found
How could I fix this?
1- all the include are wrong
#include <stdio.matrixVector>
#include <gsl/gsl_matrix.matrixVector>
#include <gsl/gsl_math.matrixVector>
#include <gsl/gsl_multifit.matrixVector>
should be
#include <stdio.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_multifit.h>
note that 3 include need the GNU Scientific Library, make sure to have installed it before compile the code
2- in the loop
for (int i = 0; i < n; i++) {
you have to declare i outside of the loop (if you aren't in C99 mode).
You need to change the #include lines, you've replaced all of the .h suffixes with .matrixVector in what I assume was a bulk replace for the rest of the file.
Change:
#include <stdio.matrixVector>
#include <gsl/gsl_matrix.matrixVector>
#include <gsl/gsl_math.matrixVector>
#include <gsl/gsl_multifit.matrixVector>
To:
#include <stdio.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_multifit.h>