• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

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







0
hansw77041
Asked:
hansw77041
  • 5
  • 4
  • 4
  • +1
1 Solution
 
daveamourCommented:
Not quiet sure what you are after - is it something like the attached code?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace EEConsoleApp
{
    class Strings
    {
        static void Main()
        {
            int[] chars = new int[] {65, 66, 67, 68};
 
            for (int i = 0; i < chars.Length; i++)
            {
                Console.WriteLine((char)chars[i]);
            }
 
            Console.Read();
        }
    }
}

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
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'?
0
 
Fernando SotoCommented:
Hi hansw77041;

This will do what you need.

Fernando
private void button1_Click(object sender, EventArgs e)
{
    byte[] receivedData = { 0x32, 0x33 };
    StringBuilder s = new StringBuilder();
    s.Append(System.Text.Encoding.UTF8.GetString(receivedData));
 
    MessageBox.Show(s.ToString());
 
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hansw77041Author Commented:
tgerbert::  Yes that's correct

daveamour: This assumes the output is the Console !  I want to save the string and use it later.
0
 
daveamourCommented:
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();
        }
    }
}

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
char[] testCharArray = new char[] { '2', '3' };
string testString = new string(testCharArray);

The string constructor will take a char array as an argument.
0
 
daveamourCommented:
Nice one tgerbert
0
 
hansw77041Author Commented:
Some good answer here.
I'll try them this evening.  

0
 
hansw77041Author Commented:
Update:  I'll need more time for this.  
Sorry about the delay but I will close this question soon.
Thanks for your patience.

 
0
 
hansw77041Author Commented:
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.
0
 
Fernando SotoCommented:
Not a problem, always glad to help.  ;=)
0
 
Todd GerbertIT ConsultantCommented:
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).
0
 
hansw77041Author Commented:
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.
0
 
Fernando SotoCommented:
@ 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
0
 
Todd GerbertIT ConsultantCommented:
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...
0
 
Fernando SotoCommented:
Not a problem tgerbert.

Have a great day.  ;=)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now