?
Solved

Plugging a fixed length string in to a structure.

Posted on 2006-07-02
4
Medium Priority
?
345 Views
Last Modified: 2010-04-16
Hi Experts

I'm receiving a fixed length string with fixed length fields that I need to break apart.  Ideally I'd like to take the string and put it into a Structure
and then access the individual fields from there. Is there a way of automatically "plugging" my string into this structure? I wish to do as little work as posible.


myFixedLengthLine = "AAAAABBBCCCCCCCCC"

Response.write MyStruct.FieldA
Response.write MyStruct.FieldB
Response.write MyStruct.FieldC

Cheers

0
Comment
Question by:Valimai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 17029223
Hi Valimai,
----------

not sure if this is going to work but have you tried
MyStruct = myFixedLengthLine.ToCharArray()

----------
bruintje
0
 
LVL 8

Accepted Solution

by:
bramsquad earned 2000 total points
ID: 17029360
is this something like what you want?  

namespace ConsoleApplication1
{
    public struct MyStruct
    {
        public string strA;
        public string strB;
        public string strC;
    }

    public class OverflowExample
    {
        public static void Main()
        {
            string myFixedLengthLine = "AAAAABBBCCCCCCCCC";

            MyStruct tempStruct = new MyStruct();

            tempStruct.strA = myFixedLengthLine.Substring(0, 5);
            tempStruct.strB = myFixedLengthLine.Substring(5, 3);
            tempStruct.strC = myFixedLengthLine.Substring(8);

            Console.WriteLine("strA: {0}", tempStruct.strA);
            Console.WriteLine("strB: {0}", tempStruct.strB);
            Console.WriteLine("strC: {0}", tempStruct.strC);
        }
    }
}

it outputs

strA: AAAAA
strB: BBB
strC: CCCCCCCCC

~b
0
 
LVL 44

Expert Comment

by:bruintje
ID: 17029437
another way is using a regex for this which makes it flexible if fields change in size or when there is another field added

        private void button1_Click(object sender, EventArgs e)
        {
            MyStruct.FieldA = GetValue(myFixedLengthLine, "A"));
            MyStruct.FieldB = GetValue(myFixedLengthLine, "B"));
            MyStruct.FieldC = GetValue(myFixedLengthLine, "C"));
        }

        private string GetValue(string data, string LookingFor)
        {
            // we need to use a regex to get the complete value
            Regex objNamePattern = new Regex(LookingFor + "*");
            objNamePattern.IsMatch(data);
            // now loop trough the mattches collection to build the filename
            StringBuilder Result = new StringBuilder();
            foreach (Match val in objNamePattern.Matches(data))
            {
                Result.Append(val.Value.ToString());
            }
            return Result.ToString();
        }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17039113
Yes take a look at fixed buffers.

I believe you can do this ...

public struct foo {
    public fixed char Field1[4];
    public fixed char Field2[9];
}

remember to set structlayout to sequential etc ...

You can then simply copy the string to the struct and view the struct (its just a different view of the same memory).

Cheers,

Greg Young
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question