A clever way to receive infinite data within a limited range

Source: Internet
Author: User

1. assume that you want to receive a lot of (or even unlimited) data, but the data has a range: for example, if you want to read the data sent by the serial port, the data range is 0x00-0xff.

2. If an array or container is directly defined for receiving, unlimited memory is required. This method is not feasible.

3. an ingenious method is to define an array (data [0xff + 1]) that covers the size of the required data range, but when receiving a data, it is subscript, assign values to the corresponding position of the array (for example, if the received data is 0x0a, the value of data [0x0a] = 0 ), the second data received is 1 (data [0x1c] = 1), and so on. Each received data has its own value.

4. Then define an array showdata [500] for displaying data, and use the value of data [] as the subscript. In this case, when receiving data, store the data to showdata [0] with its data [] value as a subscript, the next received data is sequentially placed in showdata [I ++. (For example, if 3 knows its data [0x0a] = 0 after receiving data from 0x0a, the data is saved to showdata [0 ····, assume that after receiving data for several times, 0x0a data is received again. The data [0x0a] = 0, therefore, the data content is stored in showdata [0], and the previous data is overwritten. Therefore, a finite array can receive and display infinite data ).

This article is from the "whatever957" blog, please be sure to keep this source http://whatever957.blog.51cto.com/6835003/1560759

A clever way to receive infinite data within a limited range

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.