Decimal to ASCII conversion

Yep very likely. But yes of your data of 123 34 50 48 49 57 45 49 49 45 50 50 32 49 54 58 53 52 58 49 51 34 58 34 48 48 48 48 48 34 125 0 195 191 195 191 195 191 195 191 that 0 there is most definitely the end of string character, I.E. a standard null-terminated string. The ‘space’ at the beginning is fine and is allowed json. So what you should probably do is just take your packet and parse out a null-terminated string, here’s a function (entirely untested and typed in-post, but it ‘should’ work well):

def parse_null_terminated_utf8_string(bin), do: parse_null_terminated_utf8_string(bin, 0, byte_size(bin))
defp parse_null_terminated_utf8_string(bin, count, size) do
  case bin do
    <<_::size(count)-binary, 0, _::binary>> ->
      <<string::size(count)-binary, 0, rest::binary>> = bin
      {string, rest}
    _ when size > count ->
      parse_null_terminated_utf8_string(bin, count + 1, size)
    _ ->
      {bin, ""}
  end
end

It returns a tuple of the parsed string and the ‘rest’ of the binary, so use it like:

{packet_str, _rest} = parse_null_terminated_utf8_string(packet)

Or however you want to use it, can add it to your StringUtil module or something. ^.^

4 Likes