How to let GtkTextView show the binary data(maybe the binary text)?

I notice that GtkTextBuffer will show nothing while assign a binary buffer, because it will call the function g_utf8_validate to make a valid UTF-8 string, so how to show the binary data like this:

gtk_text_buffer_set_text (GtkTextBuffer *buffer,  const char *text, int len)
  GtkTextIter start, end;

  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (text != NULL);

  if (len < 0)
    len = strlen (text);

  gtk_text_history_begin_irreversible_action (buffer->priv->history);
  gtk_text_buffer_get_bounds (buffer, &start, &end);
  gtk_text_buffer_delete (buffer, &start, &end);

  if (len > 0)
      gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
      gtk_text_buffer_insert (buffer, &start, text, len);

  gtk_text_history_end_irreversible_action (buffer->priv->history);

GTK supports only UTF-8 for text displayed in widgets.

If you want to display some UTF-16 encoded text you will need to convert it to UTF-8 using a function like g_utf16_to_utf8().

I want to show the binary buffer first, then could convert it to UTF-8, but now it’s show nothing, the data has been thrown away.

Do you need a kind of hex viewer? I don’t think that the GTK API offers a solution “out of the box”. You can convert the (binary) byte stream into a symbolic representation and then insert the latter into the text buffer. E.g.: 0x45 → 0x34 0x35 (also probably: 0x20).

I think the solution is that GtkTextView(Pango) just shows the buffer data, try best to show the data like binary, do not validate(g_utf8_validate) the data or stop at middle position.

No, that’s not a “solution” at all, and it won’t happen.

GTK requires everywhere that text is encoded in UTF-8; that has been a requirement since GTK 2.0, and it won’t change.

If you want to display binary data, you’ll have to find a way to encode it in such a way that it validates as ASCII or UTF-8; the recommendation to convert each byte it to hexadecimal, octal, or decimal representation is a well-established convention.

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