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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:15:24+00:00 2026-05-23T01:15:24+00:00

I have a TCP socket connection where I get byte array of data which

  • 0

I have a TCP socket connection where I get byte array of data which I have to decrypt and then format, so I am looking for a simple way to read this resulted byte array that I can easyly walk thru the elements exporting it as I need to a type or variable etc, to explain it better see the below example:

Let’s say we have the byte array (packets are little-endian, this is just hypotetical example):

01 02 00 03 74 00 74 00 69 00

I want to put the first 2 bytes into an int, the next 2 bytes into another int and rest as an string.

So it would look like this translated:

int first = 258;
int seconnd = 3;
string rest = "tti"

I am not a java person, but on java there was something interesting I noticed somewhere of people reading packets directly like this:

messageSize = readH(); // for 2 bytes
key = readH(); // for 2 bytes
message = readS(); // read as string

And I was wondering if there is similar pre-made feature in c# or any other resource that may help me better doing this task ?

  • 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-23T01:15:25+00:00Added an answer on May 23, 2026 at 1:15 am

    You can use BinaryReader like this:

    using (var reader = new BinaryReader(stream))
    {
        var first = reader.ReadUInt16();
        var second = reader.ReadUInt16();
        var stringBytes = reader.ReadBytes(6);
        var str = Encoding.Unicode.GetString(stringBytes);
    }
    

    However, this will only work if little-endian is used.

    The example you posted is big-endian.

    I assume that you implement both writer and sender in C#, so you are good to go with BinaryReader and BinaryWriter, they both use little-endian, so they will understand each other.

    [Edit]

    Another approach you might want to consider is using struct.
    For example in your case :

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct MyStruct
    {
        public ushort First;
        public ushort Second;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
        public string MyString;
    }
    

    The code would look like this

    var myStruct = new MyStruct { First = 1, Second = 2, MyString = "asd" };
    
    var bytes = StructToBytes(myStruct);
    
    var myStruct1 = BytesToStruct<MyStruct>(bytes);
    

    And two utility methods:

    public static T BytesToStruct<T>(byte[] bytes) where T : struct
    {
        AssertUtilities.ArgumentNotNull(bytes, "bytes");
    
        var structSize = Marshal.SizeOf(typeof(T));
        var pointer = IntPtr.Zero;
        try
        {
            pointer = Marshal.AllocHGlobal(structSize);
            Marshal.Copy(bytes, 0, pointer, structSize);
            return (T)Marshal.PtrToStructure(pointer, typeof(T));
        }
        finally
        {
            if (pointer != IntPtr.Zero)
                Marshal.FreeHGlobal(pointer);
        }
    }
    
    public static byte[] StructToBytes<T>(T structObject) where T : struct
    {
        var structSize = Marshal.SizeOf(typeof(T));
        var bytes = new byte[structSize];
        var pointer = IntPtr.Zero;
        try
        {
            pointer = Marshal.AllocHGlobal(structSize);
            Marshal.StructureToPtr(structObject, pointer, true);
            Marshal.Copy(pointer, bytes, 0, structSize);
            return bytes;
        }
        finally
        {
            if (pointer != IntPtr.Zero)
                Marshal.FreeHGlobal(pointer);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Php application using stream_socket_client(), to get data through tcp from a
I have a web app which connects to a server using a TCP connection
I have a class that encapsulates tcp socket communications with a server. For each
I have a worker thread that is listening to a TCP socket for incoming
I want to open a TCP client socket in Python. Do I have to
I have a very simple TCP server written in C. It runs indefinitely, waiting
I have written a secure TCP server in .NET. This was basically as simple
If you have a situation where a TCP connection is potentially too slow and
Hi i have got a TCP/IP Socket project. i can send string messages to
I have a simple query which returns 25,026 rows: MySqlCommand cmd = new MySqlCommand(SELECT

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.