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

  • Home
  • SEARCH
  • 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 5931235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:39:57+00:00 2026-05-22T14:39:57+00:00

I need to create a byte array that is needed to be stream to

  • 0

I need to create a byte array that is needed to be stream to another device through UART. There are some fixed parameters that I can fill in before hand but variables such as string is dynamically sized. Right up till now, I’ve been doing:

unsigned char buffer[255];
unsigned char wr_head = 0;
buffer[wr_head++] = 0x01; // and so on
memcpy(&buffer[wr_head], &some_chararray, sizeof(some_chararray));
wr_head += some_chararray;

I’ve experimented with other methods like std::string and std::vector but I felt that there is much manageable way of writing byte array for streams. Suggestions?

edit: Please advice on performance as well because is threaded.

edit2: Sorry for lacking of details the first time around. The device is indeed an embedded device. Though some suggested some solution, its not really what I want. Maybe a snippet of my current implementation will clear some confusion:

unsigned char buffer[255];
unsigned char wr_head = 0;

buffer[wr_head++] = 0x01; // Set message type
buffer[wr_head++] = 0x30; // message length
memcpy(&buffer[wr_head], &some_chararray, sizeof(some_chararray));
wr_head += some_chararray;
buffer[wr_head++] = CalChecksum;
UartSend(&buffer, wr_head); // Send array to stream out from UART

The configuration and setting value is known before hand, provided by the device documentation. This question is related to what I’ve asked in here

Thanks for the effort so far.

  • 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-22T14:39:57+00:00Added an answer on May 22, 2026 at 2:39 pm

    A ring buffer is a typical solution for problems like these.

    I have no idea what kind of device you’re on, but I’ll just suppose that you’re writing for some kind of embedded device. Let’s assume that there’s some interrupt moving data from the ring buffer to the UART. This interrupt will call getc, other code will call putc and puts.

    class RingBuffer {
    private:
        static unsigned BUFSZ = 256;
        volatile unsigned char buf[BUFSZ];
        volatile unsigned char read, write;
    
    public:
        RingBuffer() : read(0), write(0) { }
    
        // Blocks until space is available
        void putc(unsigned int c) {
            while (((write - read) & (BUFSZ - 1)) == 1)
                sleep();
            buf[write++ & (BUFSZ - 1)] = c;
        }
    
        // Returns -1 if empty
        int getc() {
            if (read == write)
                return -1;
            return buf[read++ & (BUFSZ - 1)];
        }
    
        // There are faster ways to write this.
        void puts(char *str) {
            for (; *str; ++str)
                putc(*str);
        }
    };
    

    Typically, you don’t want to make the buffer dynamically grow for something like this. There’s lots of room for improvement in the above code, and there are also libraries available for this kind of thing.

    This particular implementation also never lets you fill the buffer completely, but the code is simpler as a result. I probably wouldn’t put this code in production, but hopefully it’s a step in the right direction.

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

Sidebar

Related Questions

I need to create an Objective-C method that converts an int into a byte
I need to create a file that embeds an image as text within some
So I have a gigantic byte-array that represents a data packet. There's various parts
I am creating several byte arrays that need to be joined together to create
I need a method that reads a file to a byte array asynchronously but
I need to create an UIImage from a byte array. Here is now I
I have a bin file that I need to convert to a byte array.
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need to create a backup of a SQL Server 2005 Database that's only
I need to create a 2D int array of size 800x800. But doing so

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.