gtk+: g_base64_encode vs g_base64_decode

This is a discussion on gtk+: g_base64_encode vs g_base64_decode within the C Programming forums, part of the General Programming Boards category; These two functions aren't "the same, only oposite", it seems? In the following example, two strings are picked from two ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    gtk+: g_base64_encode vs g_base64_decode

    These two functions aren't "the same, only oposite", it seems?

    In the following example, two strings are picked from two gtktentry fields:
    Code:
    const gchar *plaintext;
    const gchar *b64text;
           	
    plaintext = gtk_entry_get_text(GTK_ENTRY(cwidgets->plainentry));
    b64text = gtk_entry_get_text(GTK_ENTRY(cwidgets->b64entry));
    
    gtk_entry_set_text(GTK_ENTRY(cwidgets->b64entry), g_base64_encode(plaintext, g_utf8_strlen(plaintext, 256)));
    
    gtk_entry_set_text(GTK_ENTRY(cwidgets->plainentry), g_base64_decode(b64text, g_utf8_strlen(b64text, 256)));
    The g_base64_encode line compiles fine, without warnings.

    However, the g_base64_decode line gives this warning:
    Code:
    warning: passing argument 2 of ‘g_base64_decode’ makes pointer from integer without a cast
    The data for the two functions are the same, so they must be expecting different data formats?

    Any hints?

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Base64 Encoding

    One takes a gchar pointer and the other a guchar pointer.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    According to this, the second parameter to that function is where the length of the decoded data is written to. This is basically an out parameter and needs to be a pointer to a location where the length is going to be stored. You don't pass in a length, you're supposed to be getting a length back after the function call.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Sorry, I'm new and struggling with some of this. (But I THINK I'm learning :-)

    When all I want to do is to take one string and decode it, how am I going to do this? If this function only returns a length, where's the decoding?

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    The return value of g_base64_decode is the decoded text (guchar is a typedef for unsigned char). Note that you are responsible for freeing the returned memory.

    Code:
    guchar *result;
    gsize result_len;
    result = g_base64_decode(encoded_text, &result_len);

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Thanks.

    In you example above, it cannot take a a pointer to the widget as the "encoded_text" ?
    Code:
    guchar *result;
    gsize result_len;
    result = g_base64_decode(cwidgets->plainentry, &result_len);
    This gives "warning: passing argument 1 of ‘g_base64_decode’ from incompatible pointer type"

    However, the exact same line with encode works fine. So, g_base64_encode is able to take the gtkentry, but g_base64_decode isn't?

    I see that g_base64_decode can take a gchar og guchar. However, then I need to convert the text in the GtkEntry to gchar/guchar.
    Code:
    gchar *enc = gtk_entry_get_text(GTK_ENTRY(cwidgets->b64entry));
    This naturally dosen't work. It gives: "warning: assignment discards qualifiers from pointer target type"
    Last edited by cnewbie1; 01-13-2010 at 01:31 PM.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    This just gets more and more confusing.

    This code works (gives no warnings):
    Code:
    const gchar *enc[32];			// WHY MUST I USE const? Gives warning if not used.
    enc[32] = gtk_entry_get_text(GTK_ENTRY(cwidgets->b64entry));
    It seems to work like I want: Take the text from the GtkEntry and put it into the gchar variable.

    However, when I try to put the gchar variable into another GtkEntry, then it's suddenly incompatible:
    Code:
    const gchar *enc[32];			// WHY MUST I USE const? Gives warning if not used.
    enc[32] = gtk_entry_get_text(GTK_ENTRY(cwidgets->b64entry));	
    gtk_entry_set_text(GTK_ENTRY(cwidgets->plainentry), enc);
    Then it suddenly gives this error:
    Code:
    warning: passing argument 2 of ‘gtk_entry_set_text’ from incompatible pointer type

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Never mind. I must be drunk.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-22-2008, 05:49 AM
  2. GTK troubles...again...
    By cornholio in forum Linux Programming
    Replies: 4
    Last Post: 01-16-2006, 01:37 AM
  3. Replies: 1
    Last Post: 10-24-2005, 07:35 AM
  4. GTK Help
    By Cronkilla in forum C Programming
    Replies: 2
    Last Post: 08-24-2004, 07:23 PM
  5. QT or GTK
    By mart_man00 in forum Linux Programming
    Replies: 4
    Last Post: 03-24-2003, 10:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21