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 6187983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:09:05+00:00 2026-05-24T02:09:05+00:00

I’m trying to convert a bit of VC 6.0 C++ code to C#. Specifically,

  • 0

I’m trying to convert a bit of VC 6.0 C++ code to C#. Specifically, I’m parsing through a binary dat file and I’ve run into a problem converting this bit of code:

ar.GetFile()->Read(buf,sizeof(int));  
memmove(&x,buf,4);

pEBMA->before_after = static_cast<enum EBMA_Reserve>(x);
pEBMA->method       = static_cast<enum EBMA_Method>(x >> 4);

Here is some related code.

struct EBMA_Data *pEBMA = &EBMA_data;

typedef CArray<struct EBMA_Data,struct EBMA_Data&> EBMA_data;

enum EBMA_Reserve
   {EBMA_DONT_RESERVE,
    EBMA_BEFORE,
    EBMA_AFTER
   };

enum EBMA_Method
   {EBMA_CENTER,
    EBMA_ALL_MATERIAL,
    EBMA_FRACTION,
    EBMA_RESERVE
   };


struct EBMA_Data
   {double reserved;
    double fraction;
    enum EBMA_Method method : 4;
    enum EBMA_Reserve before_after : 4;
   };

I’ve read this thread here Cast int to Enum in C#, but my code isn’t giving me the same results as the legacy program.

Here is some of my code in C#:

reserved = reader.ReadDouble();
fraction = reader.ReadDouble();
beforeAfter = (EBMAReserve)Enum.ToObject(typeof(EBMAReserve), x);
method = (EBMAMethod)Enum.ToObject(typeof(EBMAMethod), (x >> 4));

I do have an endianness problem so I am reversing the endianness like so.

public override double ReadDouble()
        {
            byte[] b = this.ConvertByteArrayToBigEndian(base.ReadBytes(8));
            double d = BitConverter.ToDouble(b, 0);
            return d;
        }
 private byte[] ConvertByteArrayToBigEndian(byte[] b)
        {
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(b);
            }

            return b;
        }

So then I thought that maybe the endianness issue was still throwing me off so here is another attempt:

byte[] test = reader.ReadBytes(8);
Array.Reverse(test);
int test1 = BitConverter.ToInt32(buffer, 0);
int test2 = BitConverter.ToInt32(buffer, 4);
beforeAfter = (EBMAReserve)test1;
method = (EBMAMethod)test2;

I hope I’ve given enough details about what I’m trying to do.

EDIT:

This is how I solved my issue, apparently the values I needed were stored in the first byte of a 4 byte segment in the binary file. This is in a loop.

byte[] temp = reader.ReadBytes(4);
byte b = temp[0];

res = (EBMAReserve)(b & 0x0f);
meth = (EBMAMethod)(b >> 4);
  • 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-24T02:09:06+00:00Added an answer on May 24, 2026 at 2:09 am

    EDIT: It actually looks like the structure size of EBMA_Data is 17 bytes.

    struct EBMA_DATA
    {
      double reserved; //(8 bytes)
      double fraction; //(8 bytes)
      enum EBMA_Method method : 4; //(this is packed to 4 bits, not bytes)
      enum EMBA_Reserve before_after : 4; //(this too, is packed to 4 bits)
    }
    

    so your read code should look something more like this:

     EBMA_Data data = new EBMA_Data;
     data.reserved = reader.ReadDouble();
     data.fraction = reader.ReadDouble();
     byte b = reader.ReadByte();
     data.method = (EBMAMethod)(b >> 4);
     data.before_after = (EBMAReserve)(b & 0x0f);
    

    Not 100% sure, but it looks like the code that does the shift x >> 4 bytes may be the underlying issue that’s being overlooked. If the EBMAReserve is the lower 4 bits of x and EBMAMethod is the top 4 bits, maybe this code would work?

     EBMAReserve res = (EBMAReserve)(x & 0x0f);
     EBMAMethod meth = (EBMAMethod)(x >> 4);
    

    I think that is what the : 4 means after the enumerations in the struct, it’s packing the two enums into the structure as a single byte instead of 2 bytes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm parsing an XML file, the creators of it stuck in a bunch social
I am trying to render a haml file in a javascript response like so:
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.