Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 825823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:18:58+00:00 2026-05-15T03:18:58+00:00

I am packing bytes into a struct, and some of them correspond to a

  • 0

I am packing bytes into a struct, and some of them correspond to a Unicode string. The following works fine for an ASCII string:

[StructLayout(LayoutKind.Sequential)]
private struct PacketBytes
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
    public string MyString;
}

I assumed that I could do

[StructLayout(LayoutKind.Sequential)]
private struct PacketBytes
{
    [MarshalAs(UnmanagedType.LPWStr, SizeConst = 32)]
    public string MyString;
}

to make it Unicode, but that didn’t work (the field value was empty and the other fields had incorrect values, indicating that the byte unpacking was messed up). (Since this field is part of a struct with other fields, which I’ve omitted for clarity, I can’t simply change the CharSet of the containing struct.)

Any idea what I’m doing wrong?

Here is the input (64 bytes, little endian):

31:00:31:00:32:00:33:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00

The output should be the Unicode string “1123”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:18:58+00:00Added an answer on May 15, 2026 at 3:18 am

    I would do this by declaring a nested structure for the string type. The “inner” structure can declare its CharSet. This is similar to the solution on my blog: http://nitoprograms.blogspot.com/2010/02/interop-multidimensional-arrays-of.html

    e.g.:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct StringSizeConst32AsString
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        private string Value;
    
        public static implicit operator string(StringSizeConst32AsString source)
        {
            return source.Value;
        }
    
        public static implicit operator StringSizeConst32AsString(string source)
        {
            // Note that longer strings would be silently truncated
            //  if we didn't explicitly check this.
            if (source.Length >= 32)
                throw new Exception("String too large for field: " + source);
    
            return new StringSizeConst32AsString { Value = source };
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm packing some classes into ptr_map with any typed value. class EventManager { ptr_map<string,
Here is my method: public static string GenerateRandomString(int bytes) { var rng = new
I need to automate packing of Java EE projects into EAR archive in Eclipse.
i'm having trouble packing a bunch of files into one archive. the boost docs
I have been working on packing a project lately but it has turned into
I need to set the struct alignment in XCode to 1-byte packing, and adding
Take this structure for example: struct Packing { int x; // 4-byte align int
I've made a simple resource packer for packing the resources for my game into
Is it possible to specify the struct packing size in a C# implementation of
Before suggesting bin packing algorithms, they assume you can re-order the elements and arrange

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.