Double to const char conversion


 
Thread Tools Search this Thread
Top Forums Programming Double to const char conversion
# 1  
Old 02-13-2013
Double to const char conversion

Dear all,
I am using C and ROOT for programming. And I need to incorporate following in my code.

Code:
 char *fps=NULL;
  int dec=0,sign=0;
  float mean = h1->GetMean(1);   //0.001298
  fps= fcvt(mean,6 , &dec, &sign);

I need to provide this mean as const char to some other function to get displayed on my final plot.

Alas, this is working. But not compltely.
For the mean value = 0.001298. It is only displaying on the plot as 1298 (depeneding on the no of char I asked for).

What I want is to display mean as 0.001298.

Please help.

Thanks a lot,
emily
# 2  
Old 02-13-2013
Man Page for fcvt (opensolaris Section 3c) - The UNIX and Linux Forums says The high-order digit is non-zero, unless the value is 0. The
low-order digit is rounded. The position of the radix character rela-
tive to the beginning of the string is stored in the integer pointed to
by decpt (negative means to the left of the returned digits).

Maybe you want to convert with one of the options of sprintf: https://www.unix.com/man-page/opensolaris/3/sprintf/
Code:
$ printf '%8.6f\n' .00123456
0.001235
$

Or use decpt to figure how much of "0.0*" to tack onto the front.
Code:
sprintf( fps2, "%.*s%s", 10 + dec, "0.00000000", fps );

This User Gave Thanks to DGPickett For This Post:
# 3  
Old 02-14-2013
thanks, Smilie it worked..
# 4  
Old 02-14-2013
PS: I prefer a no-*printf() solution on inner loops for speed, and just memcpy() the bytes of "0.0000*" I need to the buffer, or often even better, fwrite() or write() first the "0.0000*" bytes out and then the fps, so there is no excess copying. The idea of parsing the "%.*s%s" over and over bothers me. Maybe the optimizers chew it up? Output is, after all, the point of the exercise. Work back from output to the minimum variables you need. Write stuff in pieces if pieces is how it comes. However, write() is a system call where you can lose the CPU, so the FILE* buffering of fwrite() is a good thing for little pieces. And putc() is a macro, so it is always inline optimizable.

Last edited by DGPickett; 02-14-2013 at 02:24 PM..
# 5  
Old 02-14-2013
Quote:
Originally Posted by DGPickett
And putc() is a macro, so it is always inline optimizable.
Given that FILE * is opaque, how does it macroize it?
# 6  
Old 02-14-2013
I suspect that a FILE* points to a struct with a buffer pointer, a characters-in-buffer-count or next-character-pointer, a buffer size and a file descriptor, and putc() is a macro that directly puts one more character in the buffer, then if full writes it, returns EOF if EOF or error, else sets the characters-in-buffer-count to zero or next-character-pointer to the buffer pointer and returns the character written. There is no function call stack overhead as with not-inlined fwrite(), fputs() or fprintf(). Man Page for putc (opensolaris Section 3) - The UNIX and Linux Forums
# 7  
Old 02-14-2013
This also seems to apply to GNU GCC and libc, too. Neat.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Bool vs char * conversion

I have a problem at make step to install a downloaded package consisted of different programs. In file included from kcdbext.cc:16:0: kcdbext.h: In member function �char* kyotocabinet::IndexDB::get(const char*, size_t, size_t*)’: kcdbext.h:1281:14: error: cannot convert �bool’ to... (3 Replies)
Discussion started by: yifangt
3 Replies

2. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

3. Programming

Using const char*

I am writing some code in C++ to print a message using fprintf Here is an example void pr_desc( FILE* stream, int shift, const char* desc) { const char* format="%*s\e; fprintf(stream,format,shift,"",desc); } I call it using const char* desc; ... (4 Replies)
Discussion started by: kristinu
4 Replies

4. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

5. Programming

conversion to 'char' from 'int' warning

Hi, I wrote a simple code in C++ converting from UpperToLower case characters. However, my compiler gives me a warning: "warning: conversion to 'char' from 'int' may alter its value". Any tips? I would like to stress, I don't want to load my string into char array. int ToLower(string... (4 Replies)
Discussion started by: kajolo
4 Replies

6. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

7. UNIX for Dummies Questions & Answers

ANSI C, char to hex conversion

Hi, I have a char buf,ch; and the buf is filled with the result from MySQL server which I get like this numbytes = recv(sock, buf, 1024, 0));I have the followingcode to display the results printf("received %ld bytes:\n",numbytes); for(c=0;c<numbytes;c++){ ch = (char)buf; ... (2 Replies)
Discussion started by: alikims
2 Replies

8. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

9. Programming

diff b/w char const in C/C++

hi, what is the difference b/w char in C and C++.and give me the examples. Thanks... sarwan (3 Replies)
Discussion started by: sarwan
3 Replies

10. Programming

char to int64 conversion

Hi, I'm converting a C program that I made using the Visual Studio. I now use GCC (over Linux) and can't find some equivalences. I changed my __int64 definitions to unsigned long long, but can't find an equivalent to the microsoft i64toa() function, which let you convert a char* to a 64 bit... (1 Reply)
Discussion started by: Raspoutine
1 Replies
Login or Register to Ask a Question