Conversion

HI!

I was looking foward to convert uint32_t to const char*
Can someone point me out what I need to use? It whould be Strings?

Because I want to tweet my IP, and I aways get the error “invalid conversion from uint32_t to const char*”

Here is the code if someone want take look,

void sendIP()
{
    Serial.println("");
    Serial.println("Send IP to twitter");
    
    twitter.post(Ethernet.localIP());
    delay(1000);
}

I tryed to cast (char),but it dosent work also

Thanks ! I hope someone could help me,

You can't cast a non-constant variable to a constant, for -starts-.

Chars are signed 8 bit, you want to go from unsigned to signed.

You can't cast a 32 bit value into being a 16 bit pointer.

--- uint32_t converted to ascii text may be 10 characters long.

Serial.print does the conversion with no leading zeros if that's what you want.

Thanks GoForSmoke for the quick answer,

I actualy can use Serial.println(Ethernet.localIP());
It works just fine, but when i try to use twitter.post(Ethernet.localIP()); I can't because of uint32_t to const char* conversion.

How could I convert this information from 'uint32_t' to 'const char*' value I could use in twitter.post() function?

Actually this

You can't cast a non-constant variable to a constant, for -starts-.

is wrong. Sorry. You can but it'd have to be like ((const char*) myStringArray)

So you need to convert the binary value into a char array instead of printing.

This is from the AVR_Libc site (Arduino Reference page near bottom right links to AVR_Libc), it's the page on the General Utilities library. The function to use there is strtoul().

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html

You could just roll your own code but I don't want to get into explaining ascii right now.

Thanks!

I will give it try and I get back to you

I tryed this:

ipAddr=Ethernet.localIP();

const char* ip_to_str(const uint8_t* ipAddr)
{
static char buf[16];
sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
return buf;
}

twitter.post(buf);

no luky

=(

That's because the buffer you made can only be seen in the function.

Try making buf a global and maybe passing a pointer to buf as a function arg.

Well I tryed to change as you said but still no luck.
I’m wondering how to use strtoul function, I still havent figured out.

unsigned long strtoul (const char *__nptr, char **__endptr, int __base);
where ( ,NULL ,16 );

nptr
Null-terminated string to convert.
endptr
Pointer to character that stops scan.
base
Number base to use.

I also tryed using this:
for (byte thisByte = 0; thisByte < 4; thisByte++)
{
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}

vinicaog:
Well I tryed to change as you said but still no luck.

Code?

Try just this:

ipAddr=Ethernet.localIP();
char buf[16];
sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
twitter.post(buf);

Well after DHCP my IP acquisition is 192.168.0.43.

I tryed this:

uint8_t ipAddr[12] = {Ethernet.localIP()};
    char buf[12];
    sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
    Serial.println(buf);
    twitter.post((char*)buf);

and I got in my Tweeter "192.0.0.0"

missing something...

Ethernet.localIP() doesn't return an array of uint8_t values, it returns a single uint32_t value. So the first thing to do is store that in a variable:

uint32_t ipAddress = Ethernet.localIP();

You should then create a uint8_t pointer and have that point to the first byte in ipAddress:

uint8_t *ipPtr = (uint8_t*) &ipAddress;

You can then access the 4 values needed using ipPtr[0], ipPtr[1], ipPtr[2], and ipPtr[3].

Another option would be to use a union.

Yup, should have checked that.

This would be the modification as suggested by Arrch:

uint32_t ipAddress = Ethernet.localIP();
uint8_t* ipAddr = &ipAddress;
char buf[16];
sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
twitter.post(buf);

However depending on how the ip is returned, you may have an issue with endian-ness in that the IP could come out with the bytes reversed. In which case you would need this:

uint32_t ipAddress = Ethernet.localIP();
uint8_t* ipAddr = &ipAddress;
char buf[16];
sprintf(buf, "%d.%d.%d.%d\0", ipAddr[3], ipAddr[2], ipAddr[1],ipAddr[0]);
twitter.post(buf);

Yep this logic looks right, however when I try to compile I get "cannot convert 'uint32_t*' to uint8_t*' in initialization"

Fair enough. Lets do it properly and use a union.

union IPAddressConverter {
  uint32_t ipInteger;
  uint8_t ipArray[4];
};

void setup()
{
  // This code will only run once, after each powerup or reset of board
  IPAddressConverter ipAddress;
  ipAddress.ipInteger = Ethernet.localIP();
  char buf[16];
  sprintf(buf, "%d.%d.%d.%d", ipAddress.ipArray[0], ipAddress.ipArray[1], ipAddress.ipArray[2], ipAddress.ipArray[3]);
//twitter.post(buf);
}

void loop()
{
  // This code loops consecutively 
  
}

Oh, I should also mention, you don't need the \0 at the end of sprintf(), it is included for you as part of the " "

vinicaog:
Yep this logic looks right, however when I try to compile I get "cannot convert 'uint32_t*' to uint8_t*' in initialization"

Edited my post to add the proper cast.

Thanks so Much Tom Carpenter , Arrch and GoForSmoke.

Thats it works flawlessly, I appreciate for your help.

This C fussy logic somethimes get me crazy,

I hope I can help people like you do in the near future