from uint8_t to string

Hi need some helt please
I like to make this

void printAddress(uint8_t deviceAddress[])
{
  for (uint8_t i=0; i < 8;i++)
  {
    Serial << _HEX(deviceAddress[i]);
  }
}

Into somthing thats return a string?? and format the hex like 0E and not just only E??

Can anyone help me

Please don’t cross post the same question.

    Serial << _HEX(deviceAddress[i] >>4 );
    Serial << _HEX(deviceAddress[i] & 0x0f);

Sorry for the cross post.

 Serial << _HEX(deviceAddress[i] >>4 );
  Serial << _HEX(deviceAddress[i] & 0x0f);

How can this make

void printAddress(uint8_t deviceAddress[])
{
  for (uint8_t i=0; i < 8;i++)
  {
    Serial << _HEX(deviceAddress[i]);
  }
}

This into a string???

I was think somthing like this, and iam new to C

string printAddress(uint8_t deviceAddress[])
{
  for (uint8_t i=0; i < 8;i++)
  {
    string = string + _HEX(deviceAddress[i]);
  }
return string;
}

I know that dosent work, bout i dont know how to do it in C

You might want to have a look at your question mark key on your keyboard - it looks like it is sticking.

Your original question wasn’t clear whether you wanted leading zero suppression removed or a string.
It isn’t clear how the contents of “deviceAddress” are structured (you missed that out of your original posts):

char string [17];

void printAddress(uint8_t* deviceAddress)
{
  int i;
  const char hex_string = "0123456789ABCDEF"; 
  for (i=0; i < 8; i++) {
    string [i*2] = hex_string [deviceAddress[i] >> 4];
    string [(i*2) + 1] = hex_string [deviceAddress [i] & 255];   
  }
  string [i*2] = '\0';
}

Hi AWOL

Sorry for that but my english is not that good. I am from denmark and new into C.

I have a deviceAddress like 0xAABBAABBAABBAABB (8 hex numbers ( from 00 to FF)) and that i like the function void printAddress to return as a string so i can show it on a web page.

when i call it like client << "DS18B20 ID: " << printAddress(DS0);
the DS0 is a array from 0 to 7 og uint8_t
DS0[0] = 1
DS0[1] = aa
DS0[2] = ee

and so on…

the number i like to get back as a string is somting like 00ed0aa and so on.

But i eks DS0[0] is 0x1 I only get 1 out and I like it to be 01

Hope that helps.
And sorry for my english.

Can't anyone help me

Where are you up to?

Hi Groover

I like to have this to return a string, so I can use it like client << printAdress(DS0) << "
";

Its returning a address of a onewire device.

void printAddress(uint8_t deviceAddress[])
{
  for (uint8_t i=0; i < 8;i++)
  {
    Serial << _HEX(deviceAddress[i]);
  }
}

What's wrong with the solution in reply #3?

Hi

That i have to define a char string [17] out site, and its not a function that return it.

If you want a function to return a string, you have several options:

  1. the function allocates the memory for the string and returns a pointer to it. It is then the responsibility of the caller to free the memory when it has finished with it, otherwise you will leak memory.
  2. You can pass the function a reference (pointer or reference (C++)) to a pre-allocated string, which the function fills in. The string is returned implicitly.
  3. you can define a global string (as in reply #3).

You can't have the function allocate the string as an automatic variable and return a pointer to it, because once the string goes out of scope when the function returns, the pointer will (may) be invalid.

Where's the issue?