Solved

Plugging a fixed length string in to a structure.

Posted on 2006-07-02
4
340 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
  • 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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 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