Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Plugging a fixed length string in to a structure.

Posted on 2006-07-02
4
Medium Priority
?
348 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

604 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