Printf output, with buffer with a value of 0?

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi All

 

I am working on a ASF example project, where the UART is outputted using the USB using Printf 

e.g.

 printf("%s\r\n", (char *)pstrRecv->pu8Buffer);			

 

One of the elements in the buffer is 0, and printf see its as end of line.

 

How can i still output the buffer value using printf?

 

Thanks

Thanks

Regards

DJ

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

djoshi wrote:
One of the elements in the buffer is 0, and printf see its as end of line.

 

Yes. It stops at '\0' because you asked it to by using `%s` format specifier.

 

djoshi wrote:
How can i still output the buffer value using printf?

 

What is "buffer value" in this case? You need to explain what it is you want to see printed. What is it you want to see?

 

A sequence of characters? A pre-defined number of characters, beyond and including that '\0'? But how do you expect that '\0' be printed then?

 

Or do you need something else?

Dessine-moi un mouton

Last Edited: Sat. Aug 7, 2021 - 05:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

djoshi wrote:
One of the elements in the buffer is 0, and printf see its as end of line.

Are you telling us you're "printing" a binary buffer using printf("%s\r\n", ...) ?

 

djoshi wrote:
How can i still output the buffer value using printf?

By iterating over the elements of that buffer and "printing" each byte (I'm assuming they are actually bytes; you didn't say) using printf("%02X ", ...)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a message from an external device, that gets filled into a buffer. This is a raw binary in terms of data. I would like to see this data in my terminal screen.

Thanks

Regards

DJ

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What you might do to convert a binary string to something printable is scan along and if you find any byte below 0x20 (and maybe above 0x80?) then print that as "[XX]" in Hex digits perhaps. Of course if 0x00 is in the data you can't use that as "end of string" marker so you'll need some other way to know how many printable bytes are I the buffer for the for() loop stopping condition. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

djoshi wrote:

I have a message from an external device, that gets filled into a buffer. This is a raw binary in terms of data. I would like to see this data in my terminal screen.

 

This still does not make sense. It is not possible to see data. "Data" is an immaterial imperceptible abstraction. In order to see your "data" you need to interpret it first. And there are coutnless ways to interpret it, to convert it to a specific notation.

 

How do you want to interpret your "data", again? As a sequence of characters (as your original post suggests)? As a sequence of one-byte integers? As as sequence of machine words? What do you want to see?

Dessine-moi un mouton

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you really have a buffer full of various binary values perhaps you want to cast a strut interpretation onto it (perhaps read through a strut pointer?) and then apply the relevant printf formats onto the individual members? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
to cast a strut interpretation onto it (perhaps read through a strut pointer?)

 

Pointers on how to do this strut routine?

<iframe src="https://giphy.com/embed/3iAdiEwP..." width="480" height="315" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/filmstruc...">via GIPHY</a></p>

 

https://media.giphy.com/media/3i...

 

I don't know how to embed a GIF here with the "strut pointer".

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Sat. Aug 7, 2021 - 11:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

fwrite does what you seem to want done.

The use of \r and \n make what you want unclear.

Can either of those occur in your binary buffer?

Moderation in all things. -- ancient proverb