How to join array to String?

I have an array DeviceAddress tP200 = {0x28, 0xFF, 0x4F, 0xEA, 0xB3, 0x16, 0x03, 0x60}; the elements in array are unit8_t type how to convert it to hex string to get String converted = "28FF4FEAB3160360";?

Why do you need it as a String?

to send it in http POST method

I don't believe that answers my question.

A POST doesn't require a String, as far as I know.

ok, then maybe how convert to any variable that I can use in POST?

There must be dozens of posts on how to convert binary to ASCII hex - I'm not going to do your Googling for you.

Code:

---



```
#include <DallasTemperature.h>

char nibble_to_hex(uint8_t nibble) {  // convert a 4-bit nibble to a hexadecimal character
 nibble &= 0xF;
 return nibble > 9 ? nibble - 10 + 'A' : nibble + '0';
}

char* byte_to_str(char* buff, uint8_t val, bool terminate = true) {  // convert an 8-bit byte to a string of 2 hexadecimal characters
 buff[0] = nibble_to_hex(val >> 4);
 buff[1] = nibble_to_hexcolor=#000000[/color];
 if color=#000000[/color] {
   buff[2] = '\0';
   buff++;
 }
 return buff + 2;
}

char* DeviceAddress_to_str(char* buff, DeviceAddress &devaddr, bool terminate = true) {
 for (uint8_t i = 0; i < sizeofcolor=#000000[/color]; i++) {
   buff = byte_to_str(buff, devaddr[i], false);
 }
 if color=#000000[/color] {
   *buff = '\0';
   buff++;
 }
 return buff;
}

void setupcolor=#000000[/color] {
 Serial.begincolor=#000000[/color];
 while color=#000000[/color];
 DeviceAddress tP200 = {0x28, 0xFF, 0x4F, 0xEA, 0xB3, 0x16, 0x03, 0x60};
 char buffer[sizeofcolor=#000000[/color] * 2 + 1];
 DeviceAddress_to_str(buffer, tP200);
 Serial.printlncolor=#000000[/color];
}

void loopcolor=#000000[/color] {}
```

|

GrooveFlotilla:
There must be dozens of posts on how to convert binary to ASCII hex - I'm not going to do your Googling for you.

I'm googling for more that 3 hours without success, even if I find something I don't know how use it in my sketch.
Thank you so much for your help :confused: it will be better for me if you don't start write in my theard. You waste Your and also my time.
Please if you don't want help don't help!

This is the programming section of the forum.
If you don't want to post whatever code you've got, you've got to expect vague answers.

If you want code writing for you, post in gigs and open your wallet.

You can't stop even if I ask?

Are you seriously telling me that in three hours you couldn't find a method to convert binary to ASCII hex, and that you shouldn't be using String?

GrooveFlotilla:
Are you seriously telling me that in three hours you couldn't find a method to convert binary to ASCII hex, and that you shouldn't be using String?

Perhaps you should offer up some help or move on to help someone else...

no need to berate the OP.

@PieterP you are awesome! It's exactly what I need, unfortunatley is hard for me to understand how it's works. Playing with strings (text) is hard to understand for me it's completly diffrent that I know from php, js or VB.NET.

I have one question to your solution. Why functions byte_to_str and DeviceAddress_to_str need to be declared in code before its firts use and nibble_to_hex can by declared on the end sketch?
So far I thought that prodedures and functions order in Arduino sketch file doesn't matter.

/EDIT/ Another question. I try to understand it but...
in void setup() from your example line:

char buffer[sizeof(DeviceAddress) * 2 + 1];

I change to:
char buffer[1]; and it still works... and don't know why

The IDE code mangling / automatic prototype generation used to get upset about defaulted parameters, and references.
Not sure that's still true

XnIcRaM:
I have one question to your solution. Why functions byte_to_str and DeviceAddress_to_str need to be declared in code before its firts use and nibble_to_hex can by declared on the end sketch?
So far I thought that prodedures and functions order in Arduino sketch file doesn't matter.

The compiler reads top to bottom, so it doesn't know functions that are defined further down the sketch.
The Arduino IDE automatically generates function prototypes, so you won't have to worry about that, but it seems to fail when default parameters are given (as well as with template functions that aren't on a single line).

XnIcRaM:
/EDIT/ Another question. I try to understand it but...
in void setup() from your example line:

char buffer[sizeof(DeviceAddress) * 2 + 1];

I change to:
char buffer[1]; and it still works... and don't know why

You're writing to memory you're not supposed to. Since your program isn't doing anything else, and since there's no error checking (cfr. segfault in an operating system), it'll just overwrite whatever is in that memory space, and print will just keep on printing until it encounters a null character.
You'll run into trouble when you are actually using that memory:

  [color=#d35400]DeviceAddress[/color] [color=#000000]tP200[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]0x28[/color][color=#434f54],[/color] [color=#000000]0xFF[/color][color=#434f54],[/color] [color=#000000]0x4F[/color][color=#434f54],[/color] [color=#000000]0xEA[/color][color=#434f54],[/color] [color=#000000]0xB3[/color][color=#434f54],[/color] [color=#000000]0x16[/color][color=#434f54],[/color] [color=#000000]0x03[/color][color=#434f54],[/color] [color=#000000]0x60[/color][color=#000000]}[/color][color=#000000];[/color]
  [color=#00979c]char[/color] [color=#d35400]buffer[/color][color=#000000][[/color][color=#00979c]sizeof[/color][color=#000000]([/color][color=#d35400]DeviceAddress[/color][color=#000000])[/color] [color=#434f54]*[/color] [color=#000000]2[/color] [color=#434f54]+[/color] [color=#000000]1[/color][color=#000000]][/color][color=#000000];[/color]
  [color=#00979c]char[/color] [color=#000000]a[/color] [color=#434f54]=[/color] [color=#00979c]'a'[/color][color=#000000];[/color]
  [color=#000000]DeviceAddress_to_str[/color][color=#000000]([/color][color=#d35400]buffer[/color][color=#434f54],[/color] [color=#000000]tP200[/color][color=#000000])[/color][color=#000000];[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color][color=#000000]([/color][color=#d35400]buffer[/color][color=#000000])[/color][color=#000000];[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color][color=#000000]([/color][color=#000000]a[/color][color=#000000])[/color][color=#000000];[/color]

If you change the buffer size to 1 here, it will no longer print the 'a'.

PieterP:
If you change the buffer size to 1 here, it will no longer print the 'a'.

Oh, its correct it is work when I set it to 1 only if first time when I repeat it in for loop then it doesn't work till I back to value which you give.
Is it important to write it like that char buffer[sizeof(DeviceAddress) * 2 + 1]; or if DeviceAddress size is constant then I could give exact number as buffer size?

XnIcRaM:
I'm googling for more that 3 hours without success, even if I find something I don't know how use it in my sketch.

One needs to learn to crawl before one can walk :wink:

Adding to that, you need to understand code that you find somewhere to such an extent that you can use it. If you do not understand it, do not use it. Either find something else or ask a question here to explain it.

I know, you're eager to finish your project and show it. But remember, Rome wasn't build in one day :wink:

XnIcRaM:
Is it important to write it like that

char buffer[sizeof(DeviceAddress) * 2 + 1];

or if DeviceAddress size is constant then I could give exact number as buffer size?

Why would you do that?
Of course it's constant, but replacing it by a magic number is very bad practice.
The compiler will calculate the buffer size, there is no run-time overhead.

PieterP:
The compiler will calculate the buffer size, there is no run-time overhead.

Ohh, ok then I think that it was calculated by uPC (after compliation)

thank you so much for you help!