Convert String to Byte (not a Byte array)

I am trying to set a DS1307 Real Time Clock over Serial (xbee serial, the arduino is not connected to a computer).

You use bytes to set the RTC (at least the code that I stole uses bytes):

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
            second = 0;
            minute = 43;
            hour = 0;
            dayOfWeek = 5; //sunday = 1
            dayOfMonth = 21;
            month = 3;
            year = 13;
setDateDs1307(); //comment to stop setting the clock on power cycle

I want to be able to run a routine over serial to set the clock using user input.

I have a general function that captures input during the serialEvent() loop, but it is capturing it as a string:

void get_input()
{
        char inChar = Serial.read();
        if(inChar == 8 || inChar == 127) { //backspace or delete
            int len = inputStr.length();
            inputStr = inputStr.substring(0, len -1);
        } else {
            inputStr += (char)inChar;
        }
        if(inChar == '\n') {
            inputStr.trim(); //clean up the string
            stringComplete = true;
        }
}

I want to be able to enter the format of the time and date over serial (through a menu system) to set the date with the format HH:MM:SS-DD/MM/YY.

Using String.substring(), I will grab the parts of the string I need to set up the variables.

However, my C knowledge is horribly poor...

What I tried to do was this:

second = (byte)inputStr.substring(6, 8);
minute = (byte)inputStr.substring(3, 5);
...

Of course, I get the error "error: invalid cast from type 'String' to type 'byte'"

:astonished:

How can I convert 2 digits of a String into a byte?

And yes, I am aware that using the String class comes with a whole host of other problems, but for most of the menu stuff I am running over serial, it is doing what I need it to do. I'd just like to be able to substring out some parts of the string, stick them into a byte variable, and set my RTC.

Thanks in advance for any suggestions!

The String class has a toInt() method. Use that to get a numeric value. Store that in a byte variable to make it a byte. If the String instance didn't hold a value in the range that fits in a byte, it will be truncated to fit, so be aware that that can happen.

Thanks! I'll try that. For some reason the Arduino reference doesn't mention the toInt method...

Because, I did look RTFM before asking here! :slight_smile:

And this does what I need it to:

            second = inputStr.substring(6, 8).toInt();
            Serial.print("Seconds Byte: ");
            Serial.println(second);

Outputs:
Enter Time in date in the format HH:MM:SS-DD/MM/YY
10:22:45-04/07/13
...
Seconds Byte: 45

Thank you!

It would be a much better idea to not assume that users can follow simple directions.

PaulS:
It would be a much better idea to not assume that users can follow simple directions.

Do you assume I am not validating the input? :slight_smile:

Do you assume I am not validating the input?

Yes, I do, since you didn't post any code that proves any different.

I just posted the "Does this undocumented method work like I think it does" code.

Thanks again for letting me know about the ToInt() method.