How convert string to byte array?

I have ip address 192.168.0.100. In c# i can make operation split and add it to array :slight_smile: but i dont understand how make it in c.

Is the IP address in a string (null terminated character array) or a String object? What will the resulting array look like? An array of 4 bytes?

I dont know differences between string and string object.
i read string from serial and want pass it to method

void initEthernetConfig(String ip)
{

// How convert String to byte array
Ethernet.begin(mac, ip);

Serial.begin(9600);
Serial.println(“Better client test 12/01/11”); // so I can keep track of what is loaded
Serial.println(“Send an e in serial monitor to test”); // what to do to test
}

I dont know differences between string and string object.

The String object has a capital S, and isn’t liked much.
A C string is just a null-terminated char array.

void initEthernetConfig(String ip)

The capital s (S) means that the function wants a string object (String). Your snippet does not show how the received serial data is formatted. Please post the whole sketch. Before you post the sketch, please read the "how to use the forum" stickies to see how to format and post code (CTRL-T and code tags).

while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}

if (readString.length() >0) {
initEthernetConfig(readString);

}

What part of

Before you post the sketch, please read the "how to use the forum" stickies to see how to format and post code (CTRL-T and code tags).

was so hard to understand?

Why do you need a byte array? The serial input is a String and the initEthernetConfig(String ip) function is being passed a String [initEthernetConfig(readString);]. What is the problem?

If you are getting compile errors, we need to see the entire text of the error message(s).

Please post the whole sketch. Before you post the sketch, please read the "how to use the forum" stickies to see how to format and post code (CTRL-T and code tags).

//zoomkat 12-08-11
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>
String readString;
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
//byte ip = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
//byte gateway = { 192, 168, 1, 1 }; // internet access via router
//byte subnet = { 255, 255, 255, 0 }; //subnet mask
byte myserver = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void initEthernetConfig(String ip)
{

Ethernet.begin(mac, ip);
//Ethernet.begin(mac, ip, subnet, gateway);
Serial.begin(9600);
Serial.println(“Better client test 12/01/11”); // so I can keep track of what is loaded
Serial.println(“Send an e in serial monitor to test”); // what to do to test
}

void setup(){

initEthernetConfig(“192.168.1.100”);
}

void loop(){
// check for serial input

while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}

if (readString.length() >0) {
initEthernetConfig(readString);

}

/*
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == ‘e’) // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
} */
}

#include <SPI.h>
#include <Ethernet.h>

// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
//the IP address for the shield:
byte ip[] = { 10, 0, 0, 177 };    

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

Ip requires to be an array. To split an char string into a number/ byte array, use a c function strtok - C++ Reference

Make sure to atoi() the char string to convert them to numbers.

while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

You should be aware that though zoomkat is/was fond of this approach, it isn’t very robust, and will fail if the the data is being sent by a human using a regular terminal emulator.

Please remember to use code tags when posting code.

mistergreen:

#include <SPI.h>

#include <Ethernet.h>

// the media access control (ethernet hardware) address for the shield:
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
//the IP address for the shield:
byte ip = { 10, 0, 0, 177 };

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




Ip requires to be an array. To split an char string into a number/ byte array, use a c function http://www.cplusplus.com/reference/cstring/strtok/

Make sure to atoi() the char string to convert them to numbers.

strtok convert to char array - but how convert it to byte array?

atoi(char) // converts to int

actually, I have my code up already, here

byte inArray[4];
  char *tokens;
    int i = 0;    
    tokens = strtok(inString, ".");
    while (tokens != NULL) {
        inArray[i] = atoi(tokens);
        tokens = strtok(NULL, ".");
        i++;
    }

mistergreen:
atoi(char) // converts to int

actually, I have my code up already, here

byte inArray[4];

char *tokens;
    int i = 0;   
    tokens = strtok(inString, ".");
    while (tokens != NULL) {
        inArray[i] = atoi(tokens);
        tokens = strtok(NULL, ".");
        i++;
    }

nope, that's not how you use it. It requires a char string, not String
and for pete sakes, don't stick it in a loop();

mistergreen:
nope, that's not how you use it. It requires a char string, not String
and for pete sakes, don't stick it in a loop();

I want dynamicly change ip, because i use it in loop