# Receive 3 char and a integer value via i2c

**URL:** <https://forum.arduino.cc/t/receive-3-char-and-a-integer-value-via-i2c/381790>\
**Category:** Programming\
**Created:** [April 24, 2016, 4:38pm UTC](https://forum.arduino.cc/t/receive-3-char-and-a-integer-value-via-i2c/381790 "2016-04-24T16:38:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aldo666](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@Aldo666](https://forum.arduino.cc/u/Aldo666)\
**Post date:** [April 24, 2016, 4:38pm UTC](https://forum.arduino.cc/t/receive-3-char-and-a-integer-value-via-i2c/381790/1 "2016-04-24T16:38:29Z")

</div>

Hello,

i want to receive 3 chars and a integer value via i2c.  
The problem occours at the end of the last char.

Sender:

```cpp
  int value=1;
  Wire.beginTransmission(8);
  Wire.write("MAG");
  Wire.write(value);
  Wire.endTransmission();

```

Receiver:

```cpp
void receiveEvent(int howMany) {
  int cnt=0;
  int value=0;
  char cmd[3];

  while (1 < Wire.available()) { // loop through all but the last
  char c = Wire.read();
  if (cnt<4){
    cmd[cnt]=c;
    cnt++;
  }

 // next an int
  value=Wire.read();

//debug
Serial.println("Wert");  
Serial.println(value);
Serial.println("Befehl");  
Serial.println((const char *)cmd);

```

The console shows me:  
Wert  
1  
Befehl  
MAG BA• µ

But why?  
Is there a char 13 at the end? I don't think so.

Should i use a own seperator like ";" to set the end?

The variable "howMany" shows me 4 bytes.  
Three for the "MAG" and one for the int.

Where does the " BA• µ" comes from???

greetings, Andreas

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 24, 2016, 4:42pm UTC](https://forum.arduino.cc/t/receive-3-char-and-a-integer-value-via-i2c/381790/2 "2016-04-24T16:42:18Z")

</div>

You're treating cmd like a C string, but I don't see where you terminate it

---

<div class="post-metadata">

**Author:** ![Aldo666](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@Aldo666](https://forum.arduino.cc/u/Aldo666)\
**Post date:** [April 24, 2016, 5:06pm UTC](https://forum.arduino.cc/t/receive-3-char-and-a-integer-value-via-i2c/381790/3 "2016-04-24T17:06:09Z")

</div>

Omg, thats it. I forgot the 0.  
Now it works.

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:51pm UTC](https://forum.arduino.cc/t/receive-3-char-and-a-integer-value-via-i2c/381790/4 "2021-05-05T21:51:41Z")

</div>


