1


I am writing a code for data acquisition for my hardware.

However, I need to find the average of the data that I have collected.

Here's the problem, the acquired data is in unsigned char format and I need to convert it to double format. Furthermore, the data collected is in exponential form [1.789232E-05].

Is there any possible way of converting a unsigned char to double then back again??

I have got a double j and a unsigned char a[200]
The unsigned char consist of 1.789232E-05. How do I convert that data to double?

Thank you!

Ashton
  • 83
  • 1
  • 3
  • 12
  • Is `1.789232E-05` an example? In this case, this is not "unsigned char", it's a string. You might want to read question “Confused how to convert from a string to double using strtod() in C++” [http://stackoverflow.com/q/5687269/139746] – Pascal Cuoq Jun 17 '13 at 09:13
  • What's `exponential char`? oO – riv Jun 17 '13 at 09:16
  • Or this question: http://stackoverflow.com/questions/1710447/string-in-scientific-notation-c-to-double-conversion – ThePragmatist Jun 17 '13 at 09:17
  • @PascalCuoq This is what's extracted to a text file. So I assume it is a string format. But, it is a unsigned char format in my codes which is provided by the manufacturer. – Ashton Jun 17 '13 at 09:17
  • Then is the problem the conversion from `unsigned char *` to `char *`? – Mr Lister Jun 17 '13 at 09:26

2 Answers2

3

you could try to use atof for converting from char to double ,as for representing an double to an char I suggest you use char* to store the converted double.

Link to atof from msdn : http://msdn.microsoft.com/en-us/library/hc25t012%28v=vs.71%29.aspx

Ionut Daniel
  • 312
  • 2
  • 9
  • 20
  • Is there any example as to how I use this conversion method?? I could be of great help if you show me some. :) – Ashton Jun 17 '13 at 13:11
  • Have you looked at the link? You should take a look at the link. – Mr Lister Jun 17 '13 at 13:15
  • Just a small note: If you are programming for Microsoft, use a TCHAR version `_tstof`. That way it will be automatically converted to UNICODE if you compile for unicode. It is actually applies to all C string function: always look for `_t` version of the C functions – cha Jun 17 '13 at 22:57
  • @cha That would only matter if the type was one of those Microsoft types (such as `CHAR` or `LPCTSTR`). If it's `unsigned char` as in the OP's question, better stick with `atof`. – Mr Lister Jun 18 '13 at 06:54
  • OP mentioned MFC in the tags, thus is my note. – cha Jun 18 '13 at 22:55
0

"Exponential form" doesn't mean anything concrete. If the data is on 8 bits, it is probably in G.711 format, either A-Law or μ-Law. Depending on which one, you'll have to use a different conversion routine. (In both cases, the 8 bit format is 1 bit sign, 3 bits exponent, and 4 bits mantissa.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • With `1.789232E-05` for an example in the OP's question, I think there's more than 4 bits mantissa. – Mr Lister Jun 17 '13 at 10:43
  • @MrLister Yes. His question really isn't very clear. What other "exponential formats" do you know that fit on one byte, however? (G.711 formats don't have negative exponents at all, since they convert 13 or 14 bit integers to an 8 bit exponential format, with loss of precision.) – James Kanze Jun 17 '13 at 10:54