Convert comma delimited string to int array - c#

On clicking an OK button I have a string of values, namely:
88,2015,5,17,22,6,53,2015,05,17,22,06,53,0,0,0,0,0,0,0,0
I am going to be sending these to an Arduino via serial, the problem is the format they are in at the moment.
The Arduino is expecting them in the same format (data type) as one would send from the serial terminal window and without a newline character added.
I am currently using Serial.parseint() in my code on the Arduino to receive the values separated by commas and load them into variables. (Which currently is working when I type the following into a serial terminal window:
88,2015,5,17,22,6,53,2015,05,17,22,06,53,0,0,0,0,0,0,0,0)
I could probably do this:
string mystr = "88,2015,5,17,22,6,53,2015,05,17,22,06,53,0,0,0,0,0,0,0,0";
int[] nums = Array.ConvertAll(s.Split(','), int.Parse);
But the I have to take them back to the same format.
How to I set/change the value of mystr to what the Arduino needs?

Not sure I understand, but if you mean you want to convert them back to a comma-separated string, you could do something like this:
string nums = string.Join(",", Array.ConvertAll(s.Split(','), int.Parse));

I've did some conversion functions for a friend some time ago, and He was also using arduino, I'm not sure it is exactly what you need, but maybe it can help
http://blogs.dotnetwork.it/sabrina/en/blog/c-conversioni-dati-che-passione/
The post is originally written in italian, but on the top of the page you can find a combobox to set the translation to english I've checked it and I think is comprehensible.

Related

Send plain text mixed with Emojis as Unicode data via SMS by using C# in Unity

I am writing an Android app in Unity using C#. The app will send SMS text messages that include a mixture of text and emojis.
My initial thought is to send the Unicode values of the respective emojis inline with any plain text. I have searched StackOverflow and I haven't found a concise example that solves this problem.
Here is code I have tried:
string mobile_num = "+18007671111; //Placeholder
string text = "Test: \\uFFFd\\uFFFd"; //(smile emoji Unicode value)
char[] chars = text.ToCharArray();
byte[] bytes = Encoding.UTF8.GetBytes(chars);
string message = HttpUtility.UrlEncode(bytes);
string sms = string.Format("sms:{0}?body={1}", mobile_num, message);
Application.OpenURL(sms);
I need to know:
1. Is this the correct approach?
a. if not, please help me correctly encode text + emoji data
b. What is the step required to covert so that the final message can be sent via SMS?
So after much searching, I found the simplest way in C# is to use:
\U########
Where:
\ is an escape character
U is a constant to define a Unicode sequence follows
## is the hex value of the emoticon encoded in exactly 8 characters left filled with zeros if necessary.
For example:
string u = "Smile: \U0001F601";
Will send:
Smile: 😁
Thank you Jeppe Stig Nielsen for your insight. For the full discussion follow this link:
How to convert numbers between hexadecimal and decimal

getting multiple parameters separately (C# discord bot 0.9.6)

So what I'm trying to do is that I want my bot to be able to have two different parameters. Like what I mean is something like I can extract like a certain part of it and then after there's a "," or another symbol I can extract the following separately. So I get two different strings from one input. So like I have two strings and I want one of them to be the first half and the second one to be the rest. And I am not planning on updating to 1.0 so tell me if it's not possible in 0.9.6.
Your question isn't very clear but I think I know what you are looking for. This is a general answer for C# as I don't know how the Discord interface differs. You seem to be taking input in the form of a string, for example: play *songname*,*channelname*. To split this string into two inputs you want to use String.Split(',')
An example would be this:
string stringTakenFromDiscord = "play *songname*,*channelname*";
String[] input = stringTakenFromDiscord.Split(',');
//input[0] will be equal to what comes before the comma
//if you were to print it, it would be "play *songname*"
//input[1] will be what comes after the comma
//if you were to print it, it would be "*channelname*"
Now you can do anything you want with either of the values of the array input[] and feed them through your code to parse them. Do note that when it splits by the character, the character won't appear in either of the output strings. This will only work for inputs that only have one instance of your chosen character. You can change the character to whatever you want.
It occurs to me that it might be easier to just take the input on two separate lines instead.

How do you delete text surrounding a string that you want?

I've looked online for this but not been able to find an answer unfortunately (sorry if there is something I have missed).
I have some code which filters out a specific string (which can change depending on what is read from the serial port). I want to be able to delete all of the characters which I am not using.
e.g. the string I want from the text below is "ThisIsTheStringIWant"
efefhokiehfdThisIsTheStringIWantcbunlokew
Now, I already have a function with some code which will identify this and print it to where I want. However, as the comms could be coming in from multiple ports at any frequency, before printing the string to where I want it, I need to have a piece of code which will recognise everything I don't want and delete it from my buffer.
e.g. Using the same random text above, I want to get rid of the two random strings at the ends (which are before and after "ThisIsTheStringIWant" in the middle).
efefhokiehfdThisIsTheStringIWantcbunlokew
I have tried using the highest voted answer from this question, however I can't find a way to delete the unwanted text before my wanted string. Remove characters after specific character in string, then remove substring?
If anyone can help, that would be great!
Thanks!
Edit:
Sorry, I should have probably made my question clearer.
Any possible number of characters could be before and/or after the actual string I want, and as the string I want is coming from a serial port it will be different every time depending on what comms are coming in from the serial port. On my application I have a cell in a DGV called "Extract" and by typing in the first bit of the comms I am expecting (in this case, the extract would be This). But that will be different depending on what I am doing.
Find the position of the string you want, delete from the beginning to the predecessor of that position, then delete everything from the length of your string to the end.
String: efefhokiehfdThisIsTheStringIWantcbunlokew
Step 1 - "ThisIsTheStringIWant" starts at position 13, so delete the first twelve, leaving...
String: ThisIsTheStringIWantcbunlokew
Step 2 - "ThisIsTheStringIWant" is 20 characters long, so delete from character 21 to the length of the string, leaving:
String: ThisIsTheStringIWant

string.format - Commas as decimal separator

Given the input 123.45, I'm trying to get the output 123,45 via String.Format.
Because of the system I'm working in, the actual format strings (e.g. {0:0.00}) are saved in a config file and transformed later down the pipeline.
Editing the config file to add a new format is "safe" and I can get this through quite quickly. Editing the actual parser later down the line is "risky" so will need a more significant QA resource and I need to avoid this.
As a result, some caveats:
I only have access to string.Format(pattern, input). No overloads.
I cannot send a localisation. I know that if I send string.Format(new System.Globalization.CultureInfo("de-DE"), "{0:0.00}", 123.45) then I've got what I need. But I cannot pass the localisation.
So can it be done?
Is there any format I can pass to string.Format which will transform 123.45 into 123,45?
If you can you multiply the input by 100 then the following should work:
double input = 123.45;
string pattern = "{0:###\\,##}";
var result = String.Format(pattern, input*100);
Just for the fun of it :)
double value =123.45;
Console.WriteLine(String.Format("{0:#0.00}\b\b\b,", value));
This of course only works when there is a cursor, like in the console, otherwise the backspace control characters have no effect.
Sorry, but i can't think of a real way in accomplishing this.

custom string format puzzler

We have a requirement to display bank routing/account data that is masked with asterisks, except for the last 4 numbers. It seemed simple enough until I found this in unit testing:
string.Format("{0:****1234}",61101234)
is properly displayed as: "****1234"
but
string.Format("{0:****0052}",16000052)
is incorrectly displayed (due to the zeros??): "****1600005252""
If you use the following in C# it works correctly, but I am unable to use this because DevExpress automatically wraps it with "{0: ... }" when you set the displayformat without the curly brackets:
string.Format("****0052",16000052)
Can anyone think of a way to get this format to work properly inside curly brackets (with the full 8 digit number passed in)?
UPDATE: The string.format above is only a way of testing the problem I am trying to solve. It is not the finished code. I have to pass to DevExpress a string format inside braces in order for the routing number to be formatted correctly.
It's a shame that you haven't included the code which is building the format string. It's very odd to have the format string depend on the data in the way that it looks like you have.
I would not try to do this in a format string; instead, I'd write a method to convert the credit card number into an "obscured" string form, quite possibly just using Substring and string concatenation. For example:
public static string ObscureFirstFourCharacters(string input)
{
// TODO: Argument validation
return "****" + input.Substring(4);
}
(It's not clear what the data type of your credit card number is. If it's a numeric type and you need to convert it to a string first, you need to be careful to end up with a fixed-size string, left-padded with zeroes.)
I think you are looking for something like this:
string.Format("{0:****0000}", 16000052);
But I have not seen that with the * inline like that. Without knowing better I probably would have done:
string.Format("{0}{1}", "****", str.Substring(str.Length-4, 4);
Or even dropping the format call if I knew the length.
These approaches are worthwhile to look through: Mask out part first 12 characters of string with *?
As you are alluding to in the comments, this should also work:
string.Format("{0:****####}", 16000052);
The difference is using the 0's will display a zero if no digit is present, # will not. Should be moot in your situation.
If for some reason you want to print the literal zeros, use this:
string.Format("{0:****\0\052}", 16000052);
But note that this is not doing anything with your input at all.

Categories