# Binary data output produces junk

**URL:** <https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877>\
**Category:** Programming\
**Created:** [February 14, 2025, 9:45am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877 "2025-02-14T09:45:09Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![dthomp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dthomp/32/1126465_2.png) [@dthomp](https://forum.arduino.cc/u/dthomp)\
**Post date:** [February 14, 2025, 9:45am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/1 "2025-02-14T09:45:09Z")

</div>

I cannot find a way of writing data in binary that works. I am not trying to do anything complicated just write out 4 numbers. This is simplified sample of my code:

```arduino
#include <Arduino.h>
#define SQUARK 1000 //The Squark rate must be much bigger than Update rate

struct myData {
  long A;
  long B;
  double S;
  double D;
};

void setup() {
  //Start the serial
  Serial.begin(9600);
}

void loop() {

  Printit();
  delay(SQUARK);
}

void Printit() {

  struct myData Data;

  Data.A = 1; //countA;
  Data.B = 2; //countB;
  Data.S = 3.0; //Speed;
  Data.D = 4.0; //countA*Cal;

  Serial.write((char *) &Data, sizeof(Data));
}

```

I log the data in Tera Term and the display it in Hex Editor (neither if which I know how to use properly) and all I get is junk. Can anyone tell me what's going on?

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [February 14, 2025, 10:19am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/2 "2025-02-14T10:19:16Z")

</div>

How are you expecting it to be displayed ?

---

<div class="post-metadata">

**Author:** ![bobcousins](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bobcousins/32/221530_2.png) [@bobcousins](https://forum.arduino.cc/u/bobcousins)\
**Post date:** [February 14, 2025, 10:42am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/3 "2025-02-14T10:42:16Z")

</div>

> [@dthomp](#):
>
> Can anyone tell me what's going on?

Sure, you are writing data in binary. I don't have Hexeditor, but in my editor is looks like this

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/6/3/b/63b553f8a8d53f774ac3890342405082b296c2ea.png)

Which is what I would expect to see.

The first 4 bytes "01 00 00 00" are the value of Data.A (1). AVR stores data LSB, so the bytes are "backwards". As a value it reads 0x00000001.

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [February 14, 2025, 10:51am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/4 "2025-02-14T10:51:57Z")

</div>

Do these values print any better?

```cpp
  Data.A = 0x0A313030; //countA;
  Data.B = 0x0A323030; //countB;
  Data.S = 1.31545142219689917925e-259; //Speed;
  Data.D = 1.31545266271702834126e-259; //countA*Cal;

```

Some of those won't work on platforms like AVR. Here's [one clue](https://en.wikipedia.org/wiki/ASCII#Character_set) and [another](https://float.exposed/0x0a302e3320202020) to how it works.

You need to use `print` instead of `write`. And you can't do the whole struct in a single call: in order to work, the bytes must be interpreted as the proper type, to call the appropriate _overload_ of the `print` method.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [February 14, 2025, 10:55am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/5 "2025-02-14T10:55:13Z")

</div>

> [@dthomp](#):
>
> I cannot find a way of writing data in binary that works

output

```arduino
 00000000 00000000 00000000 00000001 A
 00000000 00000000 00000000 00000010 B
 01000000 01000000 00000000 00000000 S
 01000000 10000000 00000000 00000000 D

```

```arduino
struct myData {
    long A;
    long B;
    double S;
    double D;
}
data = { 1, 2, 3.0, 4.0 };

// -----------------------------------------------------------------------------
void prBin (
    byte *p,
    int nByte,
    const char *lbl )
{
    for (int n = nByte-1; n >= 0; n--) {
        Serial.print (" ");
        for (int i = 7; i >= 0; i--)
            Serial.print ((p [n] >> i) & 1);
    }

    Serial.print (" ");
    Serial.println (lbl);
}

void setup() {
    Serial.begin(9600);
    Serial.println ();

    prBin ((byte*) & data.A, sizeof(data.A), "A");
    prBin ((byte*) & data.B, sizeof(data.B), "B");
    prBin ((byte*) & data.S, sizeof(data.S), "S");
    prBin ((byte*) & data.D, sizeof(data.D), "D");
}

void loop () { }

```

---

<div class="post-metadata">

**Author:** ![dthomp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dthomp/32/1126465_2.png) [@dthomp](https://forum.arduino.cc/u/dthomp)\
**Post date:** [February 14, 2025, 11:09am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/6 "2025-02-14T11:09:38Z")

</div>

I was expecting something like post 3.

---

<div class="post-metadata">

**Author:** ![dthomp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dthomp/32/1126465_2.png) [@dthomp](https://forum.arduino.cc/u/dthomp)\
**Post date:** [February 14, 2025, 11:10am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/7 "2025-02-14T11:10:59Z")

</div>

Yes, this is about what i was expecting. What i get is lots of blanks, and random charaters like +@£%÷=

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [February 14, 2025, 11:15am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/8 "2025-02-14T11:15:16Z")

</div>

because you're printing the binary value, not an [ASCII](https://en.wikipedia.org/wiki/ASCII#) 0 or 1

---

<div class="post-metadata">

**Author:** ![dthomp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dthomp/32/1126465_2.png) [@dthomp](https://forum.arduino.cc/u/dthomp)\
**Post date:** [February 14, 2025, 11:16am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/9 "2025-02-14T11:16:16Z")

</div>

Sorry, I cant follow this.

---

<div class="post-metadata">

**Author:** ![dthomp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dthomp/32/1126465_2.png) [@dthomp](https://forum.arduino.cc/u/dthomp)\
**Post date:** [February 14, 2025, 11:18am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/10 "2025-02-14T11:18:24Z")

</div>

I can sort of follow this but cant understand why i would need to do it. Are you suggesting that the write method does not work and i need to make my own?

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [February 14, 2025, 11:23am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/11 "2025-02-14T11:23:56Z")

</div>

the serial monitor displays ASCII characters. not all ASCII characters are printable such as escape, linefeed or carrage return and ASCII characters are limited to 7-bit.

it displays odd symbols for these non-ASCII character values if use `write()` instead of `print()`.

i provided code to extract the individual bits of the variables and print either a '1' or '0'

---

<div class="post-metadata">

**Author:** ![bobcousins](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bobcousins/32/221530_2.png) [@bobcousins](https://forum.arduino.cc/u/bobcousins)\
**Post date:** [February 14, 2025, 11:25am UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/12 "2025-02-14T11:25:29Z")

</div>

I logged the data in Teraterm using File |Log and setting mode to binary. Then I opened the file teraterm.log in PsPad in hex mode, and set column size to 1 byte.

I did also find a macro to put Teraterm into debug mode so it displays hex:

```arduino
app_name = "Debug Mode Selector"

strdim options 4
options[0] = "Off (normal)"
options[1] = "How non-printable characters"
options[2] = "All bytes as hex"
options[3] = "Disable output completely"

listbox "Select Debug Mode" app_name options
debug_mode = result

setdebug debug_mode

```

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/3/0/030946ff97ca31bdb8460ada8508076508f3ff03.png)

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [February 14, 2025, 12:12pm UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/13 "2025-02-14T12:12:30Z")

</div>

> [@dthomp](#):
>
> I was expecting something like post 3

Here's you data displayed in CoolTerm's HEX format, exactly as expected. Just change the view to Hex, no need to log to a file, it prints the Hex representation directly.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/9/2/9/929d2516708f9d6bbb44e10667f4f2805f9f9a1e.png)

---

<div class="post-metadata">

**Author:** ![dthomp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dthomp/32/1126465_2.png) [@dthomp](https://forum.arduino.cc/u/dthomp)\
**Post date:** [February 14, 2025, 1:49pm UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/14 "2025-02-14T13:49:46Z")

</div>

Thanks everyone for your help. It looks like it is working, its just my poor understanding of what I am actually writing, and how to view it that is causing the problem.

---

<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:** [August 13, 2025, 1:50pm UTC](https://forum.arduino.cc/t/binary-data-output-produces-junk/1353877/15 "2025-08-13T13:50:23Z")

</div>

Closed on August 13, 2025, 1:50pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
