# How do I write a (uint8\_t) data array via serial

**URL:** <https://discourse.processing.org/t/how-do-i-write-a-uint8-t-data-array-via-serial/30543>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [June 6, 2021, 10:03pm UTC](https://discourse.processing.org/t/how-do-i-write-a-uint8-t-data-array-via-serial/30543 "2021-06-06T22:03:15Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 6, 2021, 11:32pm UTC](https://discourse.processing.org/t/how-do-i-write-a-uint8-t-data-array-via-serial/30543/2 "2021-06-06T23:32:05Z")

</div>

> [@gerrikoio](#):
>
> I am learning the hard way that in Processing a “byte” data type is -128 to 127 and that if dealing with unsigned data types like “uint8\_t” I need to use the signed integer data type.

That is how Java handles a byte. Once you send the byte out the serial port it is just data and the receiving end then has to deal with it.

What is the device that is receiving?

> [@gerrikoio](#):
>
> I see that the Serial library only offers byte arrays and it’s asking me to cast the integer array to byte in order to write the data via serial.

Take another look at the description and parameters:  
_[write() / Libraries / Processing.org](https://processing.org/reference/libraries/serial/Serial_write_.html)_

You can convert your int to a byte and send the byte or you can send each element of your int individually in a loop.

If you are sending an integer it will only send the LSB (least significant byte which is 0 to 255):

> **Sending integers**
>
> ```auto
> import processing.serial.*;
> 
> void setup()
> {
> // The serial port:
> Serial myPort;
> 
> // List all the available serial ports:
> printArray(Serial.list());
> 
> // Open the port you are using at the rate you want:
> myPort = new Serial(this, Serial.list()[2], 9600);
> 
> // Send a capital "A" out the serial port
> 
> for(int i=0; i< 300; i++)
> {
> myPort.write(i);
> }
>   
> myPort.stop();  
> }  
> 
> ```

> **Sending byte array**
>
> ```auto
> import processing.serial.*;
> 
> byte [] b = new byte [300];
> 
> void setup()
> {
> // The serial port:
> Serial myPort;
> 
> // List all the available serial ports:
> printArray(Serial.list());
> 
> // Open the port you are using at the rate you want:
> myPort = new Serial(this, Serial.list()[2], 9600);
> 
> // Send a capital "A" out the serial port
> 
> for(int i=0; i< 300; i++)
> {
> //myPort.write(i);
> b[i] = byte(i);
> }
>   
> myPort.write(b);
>   
> myPort.stop();  
> } 
> 
> ```

This is the same data received on a terminal for both of the code examples:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/75ed2d443612b9cf841c94ef0a82f64b64fc9659.png)

Take a look at the last few rows and numbers over 255 = 0xFF !

I suggest exploring this a bit more and it will make sense at some point.

Take a look at this:

```auto
void setup()
  {
  for(int i=0; i<256; i++)
    {
    byte b = byte(i);
    println(b, '\t', char(b),'\t', (int(b)), '\t', hex(b), '\t', byte(b), '\t', int(b));
    }
  }   

```

The output:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/57d0da6ab3742286ca3be58841c76864b445f4b1.png)

`:)`

---

_[View the full topic](https://discourse.processing.org/t/how-do-i-write-a-uint8-t-data-array-via-serial/30543)._
