Solved

Plugging a fixed length string in to a structure.

Posted on 2006-07-02
4
339 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connection String 16 61
What can cause the styling on a .NET site to not be found? 17 26
Problem!!! 8 33
What Does This C# Code Block Do? 5 32
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…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

803 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