Converting hex string to hex value

Hello together,

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.

Thanks in advance for your help!
Philipp

Welcome to the forum

There is no such thing as a hex value

Hexadecimal is just a way of displaying a number in a format that a human can understand more easily

For example, 0b01111011 has the same value as 0x7B . Note how much easier the hex version is to read than the binary version of the same value

So, what exactly do you want to do, what output do you want and what data type should it be ?

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?

A hex float (sort-of scientific notation).

That is what I suggested. But what decimal number would you imagine that thing to represent?

From stack-overflow:

hexadecimal floating-point literals: 0x1ffp10, 0X0p-1, 0x1.p0, 0xf.p-1, 0x0.123p-1, 0xa.bp10l

The exponent syntax for hexadecimal floating-point literal has the form
p | P exponent-sign(optional) digit-sequence

exponent-sign, if present, is either + or -

suffix, if present, is one of f, F, l, or L. The suffix determines the type of the floating-point literal:

1.d3bc x 16 ^ 17?

Here (link) is where I read an explanation:

I see, a Python thing.

The OP should have the Python program print out the decimal representation, and transmit that.

WOW, that's huge !!! 1.d3bc x 16 ^ 17 is a 69 bit value. AD5791 is a 20 bit DAC. Scaling required...

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.