I need to convert data of the type b'0x1.d3bc000000000p+17\r\n' into a hex value 0x1.d3bc000000000p+17, i.e. remove '\r\n' and convert a hex string to hex value.
The initial data comes to the serial port from a python script. In the Arduino program it should converted as I described above and than passed to a Digital-Analog-Converter.
My attempt was the following:
void loop(){
if (Serial.available() > 0){
// reading serial for voltage input
String V = Serial.readStringUntil('\n');
Serial.println(V);
// extracting '0x1.d3bc0p+17' from datatype b'0x1.d3bc0p+17\r\n'
int len = V.length();
char **array;
byte y = 0;
for(byte i=0; i<len; i++){
if(V.charAt(i)=='\r'){y++; }
else{array[y] += V.charAt(i); }
}
char *hexstring = array[0];
WriteAD5791(0x02, hexstring);
}
However I get the error message "argument of type "char *" is incompatible with parameter of type "unsigned long"" since WriteAD5791 takes a hexvalue i.e. 0x1.d3bc000000000p+17 and not a hex string.
You define array as a pointer, to a pointer to char. You then "write" to array using array[i], which is scribbling somewhere in memory, but you have no idea where, as you never initialize array, nor do you allocate any memory for the actual array. Why is aray a pointer to a pointer, rather than simply an array of defined size? You should not use pointers if you do not understand how they work...
You probably mean binary value. The function atoi() converts ASCII strings to integer binary, either using base2, base10 or base16 arithmetic.
This looks like a HEX representation of a floating point or fractional binary value, though. Where did that ASCII string come from and what does it represent? What does the "p+17" do?
0x1.d3bc000000000p+17
from a python script
Can you change the script to have it transmit something more sensible?
I learn something every day! The C++ standard function atof() supports this hexadecimal notation for a floating point constant, but it appears that the Arduino implementation of it does not.
This just returns 0 on an Arduino Uno:
void setup() {
Serial.begin(115200);
while(!Serial);
const char data[]="0x1.d3bc0p+4";
float x=atof(data);
Serial.println(x,6); //prints 0.000000
// scientific notation does work as expected with
// const char data[]="1.2E-4";
}
void loop() {}
what value(s) is "b'0x1.d3bc000000000p+17\r\n'" intended to represent
the "b" prefix suggests binary
the "0x" prefix suggests hex
but why is there a "."
the "d3bc000000000" looks like hex
what does "p+17"
and the "\r\n" suggests these are ASCII values