hansw77041
asked on
string format
Data received through a serial device and saved in an array.
The array contains the ascii string (not numeric) 23 or 0x32 0x33
The C# Question:
How to format the contents of the ascii_array using StringBuilder ?
StringBuilder s = new StringBuilder();
s.Format( lost !
using array[i].ToString outputs 5051 and not 23
The array contains the ascii string (not numeric) 23 or 0x32 0x33
The C# Question:
How to format the contents of the ascii_array using StringBuilder ?
StringBuilder s = new StringBuilder();
s.Format( lost !
using array[i].ToString outputs 5051 and not 23
I'm not sure I understand; you have an array with the string value "23" - an array of char's...so array[0] = '2' and array[1] = '3'?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
tgerbert:: Yes that's correct
daveamour: This assumes the output is the Console ! I want to save the string and use it later.
daveamour: This assumes the output is the Console ! I want to save the string and use it later.
Ok try this -you will need to take out what you need and code it into your own method and context. I just used a Console app for an easy example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EEConsoleApp
{
class Strings
{
static void Main()
{
string data = string.Empty;
int[] chars = new int[] {65, 66, 67, 68};
for (int i = 0; i < chars.Length; i++)
{
//Console.WriteLine((char)chars[i]);
data += (char)chars[i];
}
Console.WriteLine(data);
Console.Read();
}
}
}
char[] testCharArray = new char[] { '2', '3' };
string testString = new string(testCharArray);
The string constructor will take a char array as an argument.
string testString = new string(testCharArray);
The string constructor will take a char array as an argument.
Nice one tgerbert
ASKER
Some good answer here.
I'll try them this evening.
I'll try them this evening.
ASKER
Update: I'll need more time for this.
Sorry about the delay but I will close this question soon.
Thanks for your patience.
Sorry about the delay but I will close this question soon.
Thanks for your patience.
ASKER
tgerbert's suggestion :
quote: The string constructor will take a char array as an argument end quote.
Is correct, but the FernandoSoto's solution came in before this.
Thanks for the help, sorry for the delayed response.
quote: The string constructor will take a char array as an argument end quote.
Is correct, but the FernandoSoto's solution came in before this.
Thanks for the help, sorry for the delayed response.
Not a problem, always glad to help. ;=)
That's true, but my answer was "more correct." ;)
I don't care about the points, but it makes more sense to just pass the String() constructor your char[] array - otherwise you're calling 4 methods (StringBuilder(), GetString(), Append() and ToString()) when only one is necessary (String()).
Depending on your application that additional overhead may or may not become significant (probably not), but still it's good to be in the habit of doing things efficiently, plus it's less typing (which is nice in and of itself, but also helps to reduce coding errors).
I don't care about the points, but it makes more sense to just pass the String() constructor your char[] array - otherwise you're calling 4 methods (StringBuilder(), GetString(), Append() and ToString()) when only one is necessary (String()).
Depending on your application that additional overhead may or may not become significant (probably not), but still it's good to be in the habit of doing things efficiently, plus it's less typing (which is nice in and of itself, but also helps to reduce coding errors).
ASKER
tgerbert:
Thanks I appreciate the info.
I'm fighting the move to C# I don't like C# and doubt I ever will.
I find C++ much more efficient but not when it comes to using .NET stuff.
Thanks I appreciate the info.
I'm fighting the move to C# I don't like C# and doubt I ever will.
I find C++ much more efficient but not when it comes to using .NET stuff.
@ tgerbert;
The question posted by hansw77041 was the following, "The C# Question: How to format the contents of the ascii_array using StringBuilder ?"
You are assuming that the data coming from the serial device will be coming in and fill a string and done with it. If this is in a loop getting many lines of data reformatting the data within the same string using the StringBuilder class to hold the data is more efficient and more correct. The reason being is that String class is immutable and therefore each time you assign a value to it, it creates a new object in memory and assigns it to the variable leaving the old string in memory for garbage collection to clean up the mess and release the memory. If the data that is coming in from the serial device is allot this will cause garbage collection to happen more frequently causing the program to be less efficient. This may be the reason why hansw77041 asked the question the way it was stated, "using StringBuilder".
Fernando
The question posted by hansw77041 was the following, "The C# Question: How to format the contents of the ascii_array using StringBuilder ?"
You are assuming that the data coming from the serial device will be coming in and fill a string and done with it. If this is in a loop getting many lines of data reformatting the data within the same string using the StringBuilder class to hold the data is more efficient and more correct. The reason being is that String class is immutable and therefore each time you assign a value to it, it creates a new object in memory and assigns it to the variable leaving the old string in memory for garbage collection to clean up the mess and release the memory. If the data that is coming in from the serial device is allot this will cause garbage collection to happen more frequently causing the program to be less efficient. This may be the reason why hansw77041 asked the question the way it was stated, "using StringBuilder".
Fernando
Sorry Fernando, I was being facetious with the "more correct" crack, you make a good point! I still prefer less source code. :)
I was making assumptions about details of the askers program I don't know...
I was making assumptions about details of the askers program I don't know...
Not a problem tgerbert.
Have a great day. ;=)
Have a great day. ;=)
Open in new window