Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to int then to string?

I have tried to search for the duplicates of my problem, but could not find. Also, sorry for the not so understandable title. I am so confused about what I am searching, and also about the terminology. I am an electronics engineer with very little knowledge on .Net framework, so treat me well :)

My current project is a heart rate monitor that can store measured heart rates to an EEPROM to be sent to a PC for later log viewing.

I am interfacing with an 8-bit microcontroller using RS-232 (serial) protocol. Whenever I send a char l, which stands for log, to the microcontroller, it will send me back some information in the following format:

0x1E 0x21 0x01 0x13 0x14 0x35 0x46 0x1E 0x21 0x01 0x13 0x14 0x43 0x48 0x0A 0x0D

Empty spaces are for information purposes, these bytes are not seperated from each other.

These 8 bit HEX values that are sent from the microcontroller include a start of record information, which is 0x1E, the date and time in the format DD MM YY HH MM and also the recorded heart rate. When all the records are echoed, then 0x0A and 0x0D character is sent.

For example, above code means:

Record start.
21.01.13 14:35
Heart Rate: 70
Record start.
21.01.13 14:43
Heart Rate: 72
End of records.

How can I get the string value like this: "S210113143570S210113144372", where S can be anything. Afterwards, I am going to apply a regex syntax to this and divide it to groups so that I can add these values to a listviewcontrol.

Edit after comments and answers:

I have not written the sample wrong. Unfortunately, the encoding is exactly like above.

like image 695
abdullah kahraman Avatar asked Jan 21 '13 12:01

abdullah kahraman


1 Answers

So, actually each byte represents a number. I assume you somehow already have a byte[] array containing these values:

var data = new byte[] { 0x1E, 0x21, 0x01, 0x13, 0x14, 0x35, 0x46,
                        0x1E, 0x21, 0x01, 0x13, 0x14, 0x43, 0x48, 0x0A, 0x0D };

You now could create real objects like this:

class LogEntry
{
    public DateTime Timestamp { get; set; }
    public int HeartRate { get; set; }
}

var logEntries = new List<LogEntry>();

for(int i = 0; i < data.Length; i+= 7)
{
    if(data[i] == 0x0a && data[i + 1] == 0x0d)
        break;
    if(data[i] != 0x1e)
        throw new InvalidDataException();

    var logEntry = new LogEntry();
    var year = BcdToDecimal(data[i + 3]) + 2000;
    var month = BcdToDecimal(data[i + 2]);
    var day = BcdToDecimal(data[i + 1]);
    var hour = BcdToDecimal(data[i + 4]);
    var minute = BcdToDecimal(data[i + 5]);
    var heartRate = data[i + 6];
    logEntry.Timestamp = new DateTime(year, month, day, hour, minute, 0);
    logEntry.HeartRate = heartRate;
    logEntries.Add(logEntry);
}

byte BcdToDecimal(byte bcdValue)
{
    return (byte)((bcdValue / 16 * 10) + (bcdValue % 16));
}

The method BcdToDecimal converts the BCDs to their real value.

like image 72
Daniel Hilgarth Avatar answered Oct 29 '22 02:10

Daniel Hilgarth