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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:20:09+00:00 2026-05-30T18:20:09+00:00

This is what I am trying to do. I am taking byte data from

  • 0

This is what I am trying to do. I am taking byte data from an incoming socket program. By default, the bytes passed in to be Encoded and appended to my string will be 1500 bytes since this is the size I defined for the bytes array. My question is, I would like to know how to pass in part of the byte array instead of the whole 1500 bytes.

IPAddress localAddr = IPAddress.Parse(args[0]);
        System.Console.WriteLine("The local IP is {0}", localAddr);
        Int32 port = int.Parse(args[1]);
        System.Console.WriteLine("The port is {0}", port);
        TcpListener myListener = new TcpListener(localAddr, port);
        byte[] bytes = new byte[1500];
        string sem = "";
        do
        {
            int flag = 0;
            int rec = 1;
            Console.Write("Waiting");
            myListener.Start();
            Socket mySocket = myListener.AcceptSocket();
            // receiving the hl7 message  
            StringBuilder sbb = new StringBuilder();
            do{
            //    bytes = null;
           rec =  mySocket.Receive(bytes,SocketFlags.None);
      //     rec = mySocket.Receive(bytes);
            Console.WriteLine("rec = {0} ",rec);
            for (int i=0; i<bytes.Length; i++)
            {
                if (bytes[i]==0x1C)
                {
                flag = 1;
                }
            }
           sbb.Append(Encoding.ASCII.GetString(bytes));

            }while (flag == 0);
  • 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-30T18:20:10+00:00Added an answer on May 30, 2026 at 6:20 pm

    Firstly, let’s be clear about what the code shown does:

    • it creates an empty byte array
    • it decodes from this array using an encoding, creating a new string
    • it passes this string to append to a StringBuilder

    What it doesn’t do is “copy bytes to a string” – not least a string is essentially “char” data (16 bits each), not byte data. If you wanted to treat byte data as char data, it would just-about work for UTF-16 (depending on the endianness), but not ASCII.

    Re choosing how much to append:

    • Encoding.GetString has an overload to specify the offset and count of byte data to consider
    • StringBuilder.Append has an overload to specify the offset and count of char data to consider

    Either or both may be useful here; however, I don’t think the code does what you think it does; there are easier ways to initialise a StringBuilder

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

Sidebar

Related Questions

i've been messing around trying figure this out myself but its taking a while..
thank you for taking time to look at this. I am trying to code
I am trying to make Innodb as my default engine and I changed this
I stumbled upon this while trying to build an ActiveX dll from an old
I'm trying to log a UTF-8 encoded string to a file using Python's logging
I have been trying to make this search engine for a MySQL database. Taking
Thanks for taking time to review this question. I've been trying to fix a
I'm trying this with Oracle SQL Developer and am on an Intel MacBook Pro.
I am trying this in my Form Load Event cmdCancel.Attributes.Add(onClick, document.forms[0].reset();return false;) but it
I'm trying this: Type ThreadContextType = typeof(Application).GetNestedType(ThreadContext, System.Reflection.BindingFlags.NonPublic); MethodInfo FDoIdleMi = ThreadContextType.GetMethod(FDoIdle, BindingFlags.NonPublic |

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.