LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-30-2011, 01:03 AM   #1
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
C# error: cannot use fixed size buffers contained in unfixed expressions


Hi -

I'm struggling with a C# program to read binary records from a database. The records were created with Borland Delphi. Here's an example
Code:
// Delphi record definition
  tBowler_Rec = Record
  public
    gender          : tGender;
    bowler_num      : byte;
    name            : tString32;
    initials        : String[3];
    ...
Code:
// Corresponding C# definition (unmanaged code)
    [StructLayout(LayoutKind.Sequential, Pack=4)]
    public unsafe struct tBowler_Rec
    {
        public tGender gender;
        public byte bowler_num;
        public fixed byte name[32];
        public fixed byte initials[3];
        ...
I'm actually able to read this binary struct out of a SQL Server database and see the data in the Visual Studio debugger. Yay! I'm able to access fields like "gender" and "bowler_num" with no problem. Yay!

Q: How do I turn "name" into a C# string?

An example name is "ASHTON". It's a Pascal string (byte count, followed by ASCII characters). It looks like this in memory:
Code:
\0x6ASHTON\0x0\0x0...
Here's how I'm trying to access it:

Code:
[StructLayout(LayoutKind.Sequential, Pack=4)]
public unsafe struct tBowler_Rec
{
    public tGender gender;
    public byte bowler_num;
    public fixed byte name[32];
    ...
    public string Name
    {
        get
        {
            StringBuilder sb = new StringBuilder();
            int ilen = name[0];
            for (int i = 1; i <= ilen; i++)
                sb.Append(name[i]);
            return sb.ToString();
        }
    }
I'm getting this error:

Quote:
Error: You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement.
Help?????

Thank you in advance!

Last edited by paulsm4; 06-30-2011 at 12:11 PM. Reason: Changed title
 
Old 06-30-2011, 02:02 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I found the answer here:
http://stackoverflow.com/questions/6...om-this-object

Solution:
Code:
    [StructLayout(LayoutKind.Sequential, Pack=4)]
    public unsafe struct tBowler_Rec
    {
        public tGender gender;
        public byte bowler_num;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
        public byte[] name;
        ...
        public string Name
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                int ilen = name[0];
                for (int i = 1; i <= ilen; i++)
                    sb.Append(name[i]);
                return sb.ToString();
            }
        }
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ : converting to binary numbers... jubaitca Programming 2 11-16-2006 07:14 AM
converting a hex file to binary linx win Programming 3 07-20-2006 03:04 AM
Converting perl to Binary DanTaylor Programming 3 02-02-2006 08:57 PM
Voice Record in Binary format knockout_artist Linux - Software 5 12-28-2005 02:27 PM
Converting ASCII to Binary? Darx Linux - Software 0 04-08-2004 10:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration