How to convert a string to int and then to string?

I tried to find duplicates of my problem, but could not find. Also, sorry for the obscure name. I am so confused by what I'm looking for, as well as the terminology. I am an electronics engineer with very little knowledge of the .Net framework, so treat me well :)

My current project is a heart rate monitor that can store the measured heart rate in the EEPROM for sending to a PC for later viewing the log.

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

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

Empty space for informational purposes, these bytes are not separated from each other.

These 8-bit HEX values ​​that are sent from the microcontroller include the start of recording information that is 0x1E , the date and time in DD MM YY HH MM format, and the recorded heart rate. When all records are reflected, then 0x0A and 0x0D are sent.

For example, the 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 to get a string value as follows: "S210113143570S210113144372", where S can be anything. Subsequently, I am going to apply the regular expression syntax to this and divide it into groups so that I can add these values ​​to the listview control.

Edit after comments and replies:

I did not write the sample incorrectly. Unfortunately, the encoding is exactly the same as above.

+6
source share
4 answers

So, virtually every byte is a number. I assume that you 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 }; 

Now you can 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 BcdToDecimal method converts the BCD to their real value.

+5
source

brother, I think you should use the line ie,

 string heartRate=70; string recordStart=21.01.13 14:35; string fullValue = heartRate + '|' + recordStart; 

now instead of using a regular expression, you can easily split this line using the split method for example.

 string splittedValues[]=fullValue.Split('|'); 

and u will get these values ​​when necessary, for example,

 splittedValues[0] \\it will be heard rate string (at index zero) similarly splittedValues[1] \\it will be record start string (at index one) 

I still do not understand your problem, and if my answer does not matter, please tell me and I will clarify

0
source
 public static String ParseData(String data) { MatchCollection matches = Regex.Matches(data, "0[xX]([a-fA-F0-9]{2})"); Int32 count = 0; String result = String.Empty; foreach (Match match in matches) { switch (count) { case 0: { if (match.Value.Equals("0x1E", StringComparison.OrdinalIgnoreCase)) { result += "S"; break; } goto End; } case 1: case 2: case 3: case 4: case 5: result += match.Groups[1].Value; break; case 6: result += Int32.Parse(match.Groups[1].Value, NumberStyles.HexNumber).ToString(); count = -1; break; } ++count; } End:; return result; } 
0
source

I am very sorry for my most stupid mistake. I saw that it is really easy and not so long to do it on the side of the microcontroller. Now that I have done this, it is really easy to parse the values.

I used this procedure on the microcontroller side:

 unsigned char BCDtoDecimal(unsigned char bcd_number) { return ((bcd_number>>4)*10 + (bcd_number&0x0F)); } 

So the code is very simple on the C # side:

  private void buttonGetLogs_Click(object sender, EventArgs e) { string inputBuffer; char[] charArray; try { serialPort.Write("l"); inputBuffer = serialPort.ReadLine(); charArray = inputBuffer.ToCharArray(); inputBuffer = string.Empty; foreach (char item in charArray) { inputBuffer += Convert.ToInt32(item).ToString(); } } catch (Exception) { } } 
0
source

All Articles