Solved

Plugging a fixed length string in to a structure.

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now