# Converting hex string to hex value

**URL:** <https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460>\
**Category:** Programming\
**Created:** [March 1, 2023, 2:40pm UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460 "2023-03-01T14:40:05Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![phjust](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@phjust](https://forum.arduino.cc/u/phjust)\
**Post date:** [March 1, 2023, 2:40pm UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/1 "2023-03-01T14:40:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 1, 2023, 2:50pm UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/2 "2023-03-01T14:50:23Z")

</div>

Welcome to the forum

> [@phjust](#):
>
> convert a hex string to hex value

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 ?

---

<div class="post-metadata">

**Author:** ![RayLivingston](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@RayLivingston](https://forum.arduino.cc/u/RayLivingston)\
**Post date:** [March 1, 2023, 2:57pm UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/3 "2023-03-01T14:57:23Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [March 1, 2023, 3:39pm UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/4 "2023-03-01T15:39:41Z")

</div>

> [@phjust](#):
>
> convert a hex string to hex value

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?

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [March 1, 2023, 10:39pm UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/5 "2023-03-01T22:39:07Z")

</div>

> [@anon94801460](#):
>
> 0x1.d3bc000000000p+17

A hex float (sort-of scientific notation).

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [March 1, 2023, 10:40pm UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/6 "2023-03-01T22:40:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [March 1, 2023, 10:50pm UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/7 "2023-03-01T22:50:15Z")

</div>

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** :

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [March 1, 2023, 10:55pm UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/8 "2023-03-01T22:55:19Z")

</div>

> [@anon94801460](#):
>
> what decimal number would you imagine that thing to represent

1.d3bc x 16 ^ 17?

Here ([link](https://stackoverflow.com/questions/7666713/converting-float-hex-value-to-binary-in-python)) is where I read an explanation:

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [March 1, 2023, 11:00pm UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/9 "2023-03-01T23:00:02Z")

</div>

I see, a Python thing.

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

---

<div class="post-metadata">

**Author:** ![Etienne\_74](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/etienne_74/32/1013044_2.png) [@Etienne\_74](https://forum.arduino.cc/u/Etienne_74)\
**Post date:** [March 1, 2023, 11:48pm UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/10 "2023-03-01T23:48:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [March 3, 2023, 2:03am UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/11 "2023-03-03T02:03:59Z")

</div>

> [@phjust](#):
>
> convert data of the type b'0x1.d3bc000000000p+17\r\n'

I learn something every day! The [C++ standard function atof()](https://www.programiz.com/cpp-programming/library-function/cstdlib/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:

```arduino
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() {}

```

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [March 3, 2023, 11:22am UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/12 "2023-03-03T11:22:48Z")

</div>

> [@phjust](#):
>
> I need to convert data of the type b'0x1.d3bc000000000p+17\r\n'

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 30, 2023, 11:23am UTC](https://forum.arduino.cc/t/converting-hex-string-to-hex-value/1096460/13 "2023-08-30T11:23:44Z")

</div>

Closed on August 30, 2023, 11:23am UTC

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