Convert Double to Char.

#include <RFduinoBLE.h>

#include<Wire.h>
const int MPU_addr=0x68;
int16_t axis_X,axis_Y,axis_Z;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;
 
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Serial.begin(9600);
  RFduinoBLE.advertisementData = "xAng";
  RFduinoBLE.deviceName = "Noob";
  RFduinoBLE.begin();
}
void loop(){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);
  axis_X=Wire.read()<<8|Wire.read();
  axis_Y=Wire.read()<<8|Wire.read();
  axis_Z=Wire.read()<<8|Wire.read();
    int xAng = map(axis_X,minVal,maxVal,-90,90);
    int yAng = map(axis_Y,minVal,maxVal,-90,90);
    int zAng = map(axis_Z,minVal,maxVal,-90,90);
       x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
       y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
       z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);

  
  
    
/* Serial.print("Angle x = ");
  Serial.print(x);
  Serial.println((char)176);*/
  /* Serial.print("Angle y = ");
  Serial.print(y);
  Serial.println((char)176); */
 /* Serial.print("Angle z = ");
   Serial.print(z);
   Serial.println((char)176); */


   delay(1000);
}

I need help in converting my x,y,z value from double to char. I'm running a Bluetooth app that only capture data in char mode. PLEASE HELP

What ONE character do you want to convert x, y, or z to? That IS what you asked.

Converting a double to a string, a NULL terminated ARRAY of chars is a different story - one that dtostrf() handles nicely.

I think OP needs bytes not chars

I need help in converting my x,y,z value from double to char.

1. Assume DUE which supports 64-bit binary64 (IEEE-754 Standard) format for floating point number. It gives 12/13 digit accuracy after decimal point.

2. Assume: double x = 3.1415926;

3. Execution of --

String storage = String(x, 7); results in having:

storage[0] = 0x33      //ASCII code of 3
storage[1] = 0x2E      //ASCII code of .
.....................................................
storage[8] = 0x36     //ASCII code 6
storage[9] = 0x00     //ASCII code of null-byte (null character)

4. Let us check:

Serial.println(storage); //shows: 3.1415926
Serial.println(storage[0], HEX); //shows: 33 = ASCII code of 3
..................................................
Serial.println(storage[9], HEX);  //shows: 0 = ASCII code of null character

First of all, you should settle on using "float" instead of "double" on Arduino's. If the question is about how to convert a "double" to an array of bytes and transmit them, then use memcpy:

double d = 3.141;
unsigned char b[sizeof(double)];

//Transmit
memcpy(b, &d, sizeof(double));
for (byte i = 0; i < sizeof(double); i++) someThing.write(b[i]);

//Receive
for (byte i = 0; i < sizeof(double); i++) b[i] = someThing.read();
memcpy(&d, b, sizeof(double));

Yes, it's almost two years old, but it has the exact Subject for this, so...
Here is what I cobbled together some time ago, using someone's non-working version as a starting point. I thank them for the incentive.
I have not put it through the ringer of extremes - it seems to work for my normal range of real-world needs so I leave it alone. :slight_smile:

//Ex.) char cVal[10];  float fVal=((22.0 /7.0)*256)-46.85;
// dtoa(fVal,cVal,4); Serial.println (String(cVal));

  //arguments... 
  // float-double value, char array to fill, precision (4 is .xxxx)
  //and... it rounds last digit

char* dtoa(double dN, char *cMJA, int iP) {
  char *ret = cMJA; long lP=1; byte bW=iP;
  while (bW>0) { lP=lP*10;  bW--;  }
  long lL = long(dN); double dD=(dN-double(lL))* double(lP); 
  if (dN>=0) { dD=(dD + 0.5);  } else { dD=(dD-0.5); }
  long lR=abs(long(dD));  lL=abs(lL);  
  if (lR==lP) { lL=lL+1;  lR=0;  }
  if ((dN<0) & ((lR+lL)>0)) { *cMJA++ = '-';  } 
  ltoa(lL, cMJA, 10);
  if (iP>0) { while (*cMJA != '\0') { cMJA++; } *cMJA++ = '.'; lP=10; 
  while (iP>1) { if (lR< lP) { *cMJA='0'; cMJA++; } lP=lP*10;  iP--; }
  ltoa(lR, cMJA, 10); }  return ret; }
1 Like

Thanks BhitHead. Your solution worked when others did not.

See ya,
Sean

sjmill01:
Thanks BhitHead. Your solution worked when others did not.

See ya,
Sean