Beginner trying to figure out strings

Hi. I am new to programming. String manipulation appears to be the bane of my existence.

I have modified the DHCPAddressPrinter example to broadcast the IP address over UDP…

(some code removed for brevity)

void setup() { 
  Ethernet.begin(mac);
}

void loop() {

  Udp.begin(localPort);
  Udp.beginPacket(broadcastIP, broadcastPort);
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
   Udp.write(Ethernet.localIP()[thisByte]);
   }

  Udp.endPacket();
  delay(1000);
}

This prints the 4 bytes of the IP Address to UDP, and I have verified that it works. However, it’s just 4 bytes, not a readable text string like “192.168.1.234”.

First, a favor. I learn best when I see how to solve my problem. So if it’s a quick solution, could someone show me how to massage the individual IP bytes into decimal strings? I tried using String(Ethernet.localIP(),DEC) as an argument to Udp.write(), but this seems to pass the value, not the pointer to Udp.write().
Second, is there a good place for me to learn String manipulation for Processing (as the projects I wish to work on require some ethernet communication with string commands and arguments)? The Arduino Reference has all the documentation for the available functions, but not a lot of examples that deal with multi-character string manipulation.
Thank you!

Don’t learn String manipulation, just use sprintf.

If you want the value of a byte as an ASCII string then use print not write. Print converts write does not!.

Mark

holmes4:
If you want the value of a byte as an ASCII string then use print not write. Print converts write does not!.

The EthernetUDP Class only has a write() function, not a print() fucntion.

Are you suggesting that I first translate thernet.localIP() to a string ip, then call Udp.write(ip,LEN)? Again, very new at this, I am not sure how I would do that either.

If the address comes in as 4 byte parts, then

byte part1, part2, part3, part4;
char buffer[20];
sprintf(buffer, "%d.%d.%d.%d",part1,  part2,  part3, part4);
Serial.println(buffer);

The compiler is smart enough to cast the bytes to integers, if that's what you want.

Thank you!

I just figured out a much more difficult way to do it…

(I need the ‘len’ variable to deal with the variable length of the decimal digits; however, using what you sent, I imagine I can add leading zeros with %03d. I’ll try it out).

Thank you, I need to become much more familiar with the print/string functions.

  int len = 0;
  byte S1[15];
  String str1;
  str1 = String(Ethernet.localIP()[0],DEC)
    + "." + String(Ethernet.localIP()[1],DEC)
    + "." + String(Ethernet.localIP()[2],DEC)
    + "." + String(Ethernet.localIP()[3],DEC);
    
  for (byte i=0; i<15; i++){
    if (str1[i]==0x00)
    {
      break;
    }
    else
    {
      S1[i] = str1[i];
      len=i+1;
    }
  }

Why do you want leading zeros? #where and how are you sending the TCP/ip address. The dot quoted from is normally just for humans (eg debug)?.

Mark

I actually dropped the leading zeros, but I was going to use them to make sure that there was a fixed field width.

I deal with a lot of old serial communication equipment, and all those serial protocols are different and (usually) quirky in their own way. In general, I have found that using fixed-width data makes for easier parsing down the line when trying to write interfaces for these things. So my default is to use fixed width. But you are totally right, I don't need that here.

Also, just FYI, this is my first project on the platform, and I am doing something two-fold…

  1. Creating a library to store basic data in EEPROM (Name, version, MAC, etc)
  2. Creating a basic loop to broadcast that data in XML over UDP
  3. Create a basic TCP interface, so that I can program those variables over the network

So the idea is that as I create network devices to do various things, they will all have basic custom library functions that will allow my client software to manage:

-Verifying that the device has been configured and the date
-Finding the device on the network when DHCP
-Setting MAC (should only be required once)
-Setting Static IP values, options
-Setting a Model name, serial
-Setting a User-supplied Name (“GarageTemp”, etc)