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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:05:06+00:00 2026-05-20T10:05:06+00:00

I have this hexadecimal data: byte[] data = new Byte[] { 0xC1, 0x3A, 0x00,

  • 0

I have this hexadecimal data:

byte[] data = new Byte[] {
    0xC1, 0x3A, 0x00, 0x01, 0x5D, 0xDA, 0x47, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0xF0, 0xFC, 0x12, 0x00, 0x00, 0x00
};

I have C++ struct:

struct SERVICE
{
    unsigned char c;
    unsigned char size;
    unsigned char headcode;
    unsigned char Type;
    unsigned short Port;
    char ServiceName[50];
    unsigned short ServiceCode;
};

My question is: How to get from data ServiceName, Port and etc…?

Sorry for my bad english

  • 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-20T10:05:07+00:00Added an answer on May 20, 2026 at 10:05 am

    Here is one way to do it:

    struct SERVICE
    {
        public byte c;
        public byte size;
        public byte headcode;
        public byte Type;
        public ushort Port;
        public string ServiceName;
        public ushort ServiceCode;
    };
    
    string GetNullTerminatedString(byte[] data, Encoding encoding)
    {
        int index = Array.IndexOf(data, (byte)0);
        if (index < 0)
        {
            Debug.WriteLine("No string terminator found.");
            index = data.Length;
        }
    
        return encoding.GetString(data, 0, index);
    }
    
    SERVICE ByteArrayToService(byte[] array, Encoding encoding)
    {
        using (MemoryStream stream = new MemoryStream(array))
        {
            using (BinaryReader reader = new BinaryReader(stream))
            {
                SERVICE service = new SERVICE();
                service.c = reader.ReadByte();
                service.size = reader.ReadByte();
                service.headcode = reader.ReadByte();
                service.Type = reader.ReadByte();
                service.Port = reader.ReadUInt16();
                service.ServiceName = GetNullTerminatedString(reader.ReadBytes(50), encoding);
                service.ServiceCode = reader.ReadUInt16();
                return service;
            }
        }
    }
    
    void Main(string[] args)
    {
        byte[] data = new Byte[]
        {
            0xC1, 0x3A, 0x00, 0x01, 0x5D, 0xDA, 0x47, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0xF0, 0xFC, 0x12, 0x00, 0x00, 0x00
        };
    
        SERVICE s = ByteArrayToService(data, Encoding.Default);
    }
    

    This assumes, that the binary array uses the same Endianess as your architecture. If that is not the case you can use the EndianBinaryReader from the MiscUtil library.

    Edit: this is also a nice solution, that avoids the reader altogether. You can’t directly specify the encoding to use for the string however and the memory layout of the structure has to match the layout used in the byte array.

    [StructLayout(LayoutKind.Sequential)]
    struct SERVICE
    {
        public byte c;
        public byte size;
        public byte headcode;
        public byte Type;
        public ushort Port;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
        public string ServiceName;
        public ushort ServiceCode;
    };
    
    SERVICE ByteArrayToService(byte[] array)
    {
        GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
        SERVICE service = (SERVICE)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(SERVICE));
        handle.Free();
        return service;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
I have a large hexidecimal (16 byte, 32 hex digits) data item that always
I have found this example on StackOverflow: var people = new List<Person> { new
I have this function: void myFunc(NSString* data) { NSMutableArray *instrs = [[NSMutableArray alloc] initWithCapacity:[data
I can turn a byte into a hexadecimal number like this: myByte.ToString(X) but it
I have this code in jQuery, that I want to reimplement with the prototype
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this line in a javascript block in a page: res = foo('<%=

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.