question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Erroneous string format using mosquitto_sub/_pub

See original GitHub issue

Steps to reproduce

Run the following *.ps1 script in a text file with UTF-8 ecoding:

while ($true)
{ 
  $date = Get-Date -Format "o"
  $rand = Get-Random -Minimum -10 -Maximum 40
  $message = '{"time":' + '"' + "$date" + '"' + ', "value":' + "$rand" + ', "label":"ºC"}'
  #echo "$message"
  mosquitto_pub -h test.mosquitto.org -t tofol/test -m "$message"
  Start-Sleep -s 1
}

Now in another terminal window, subscribe to the mosquitto topic:

mosquitto_sub -h test.mosquitto.org -t tofol/test

The output is unexpected:

{time:2021-03-30T12:30:24.0266957+02:00, value:3, label:║C}

Expected behavior

Expected behavior is seem by running the following script:

while ($true)
{ 
  $date = Get-Date -Format "o"
  $rand = Get-Random -Minimum -10 -Maximum 40
  $message = '{"time":' + '"' + "$date" + '"' + ', "value":' + "$rand" + ', "label":"ºC"}'
  echo "$message"
  #mosquitto_pub -h test.mosquitto.org -t tofol/test -m "$message"
  Start-Sleep -s 1
}

This outputs: {"time":"2021-03-30T12:31:48.2728626+02:00", "value":33, "label":"ºC"}

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.3
PSEdition                      Core
GitCommitId                    7.1.3
OS                             Microsoft Windows 10.0.19041
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:36 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
mklement0commented, Apr 8, 2021

My pleasure, @chris-steema; I certainly learned a few things myself.

1reaction
chris-steemacommented, Apr 7, 2021

However, I wonder if these programs, like Python, offer an opt-in mechanism for specifying the desired encoding.

Good point. I’ll see if I can find anything. Thanks.

By contrast, character-encoding issues do come into play when PowerShell captures stdout output from mosquitto_sub, which invariably involves decoding (in order to convert the raw byte output to .NET strings), which PowerShell bases on [Console]::OutputEncoding

Yes, the conversion of byte streams to .NET strings I can imagine very clearly. Great stuff, thanks to your patient explanations I think I now have a pretty clear idea of what’s going on. Powershell’s inability to escape embedded quotes was a red herring for me, as I had imagined that that and what turned out to be an encoding issue were related, which they aren’t. As far as I’m concerned we can close this issue, but I won’t do so now just in case you’d prefer to keep it open for whatever reason.

Read more comments on GitHub >

github_iconTop Results From Across the Web

String.format is creating error - java
This only happened when I use String.format() , When I was using StringBuilder to return in the toString method, the codes run just...
Read more >
error ring "format into string" - NI Community
I'm not sure how the "Error Code Database.vi" is organized, but I suspect the database lookups are what drags it into the mud...
Read more >
Format string attack
The Format String exploit occurs when the submitted data of an input string is evaluated as a command by the application. In this...
Read more >
StringFormatMatches: String.format string doesn't match ...
Id. StringFormatMatches ; Summary. String.format string doesn't match the XML format string ; Severity. Error ; Category. Correctness: Messages ; Platform. Android.
Read more >
stringFormat error Perspective - Ignition
Hi, I want to copy my labels into an email and I want to transfer integer into string, so I used stringFormat. However,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found