LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   converting integer value to hexadecimal string in C - any suggestions?? (https://www.linuxquestions.org/questions/programming-9/converting-integer-value-to-hexadecimal-string-in-c-any-suggestions-174140/)

woodywellhung 04-24-2004 04:30 PM

converting integer value to hexadecimal string in C - any suggestions??
 
hi everyone

I am having a go at GTK programming in Linux using C.

I have decided to write a simple program that converts between decimal and hex and vice versa.

I need some ideas as to how I can convert an integer to a string containing its hexadecimal equivalent.

I have seen the sprintf() function suggested but this is no good as I need an actual string so that I can use it to set the text on a GTK widget.

thanks in advance

woodywellhung 04-24-2004 04:37 PM

heres my code so far:

/*
*File name: hexcon.c
*/

#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <stdlib.h>

//global boolean says dec yes no
gboolean decimal=TRUE;

//global widgets
/*-- Declare the GTK Widgets used in the program --*/
GtkWidget *window;
GtkWidget *button;
GtkWidget *label;
GtkWidget *entry;
GtkWidget *hbox;

/*-- This function allows the program to exit properly when the window is closed --*/
gint destroyapp (GtkWidget *widget, gpointer gdata)
{

gtk_main_quit();
return (FALSE);
}
//function responds to button click
void button_clicked(GtkWidget *widget, gpointer gdata)
{
if(decimal)
{
gtk_label_set_text( GTK_LABEL(label),"HEX." );
decimal=FALSE;
gchar* oldText=g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
gchar* newText=(gchar*)g_malloc(256);
guint64 value=g_ascii_strtoull(oldText,NULL,10);
//now convert to hexadecimal
//????????
gtk_entry_set_text(GTK_ENTRY(entry),newText);



}
else
{
gtk_label_set_text( GTK_LABEL(label),"DEC." );
decimal=TRUE;
gchar* oldText=g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
gchar* newText=(gchar*)g_malloc(256);




}
}

int main (int argc, char *argv[])
{


/*-- Initialize GTK --*/
gtk_init (&argc, &argv);

/*-- Create the new window --*/
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

/*-- Create the hbox --*/
hbox = gtk_hbox_new(FALSE, 0);

/*-- Connect the window to the destroyapp function --*/
gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(destroyapp), NULL);


/*-- Create the first button with a label --*/
button = gtk_button_new_with_label ("CONVERT");
//connect button to button function
gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(button_clicked), NULL);
/*--create entry field --*/
entry=gtk_entry_new();
gtk_entry_set_text( GTK_ENTRY(entry),"0");
//create label
label=gtk_label_new("DEC.");

/*-- Pack all the radio buttons into the hbox --*/
gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 2);



/*-- Add the hbox to the window --*/
gtk_container_add(GTK_CONTAINER(window), hbox);

/*-- Add a border to the window to give the button a little room --*/
gtk_container_border_width (GTK_CONTAINER (window), 15);

/*-- Display the widgets --*/
gtk_widget_show(entry);
gtk_widget_show(label);
gtk_widget_show(button);

gtk_widget_show(hbox);
gtk_widget_show(window);

/*-- Start the GTK event loop --*/
gtk_main();

/*-- Return 0 if exit is successful --*/
return 0;
}

itsme86 04-24-2004 05:04 PM

Why won't sprintf() work?

sprintf(buffer, "%X", my_int);

Now buffer contains a string that shows my_int in hex.

woodywellhung 04-24-2004 05:27 PM

aahhh thats how

I was thinking it only printed to the screen ha ha!!

many thanks


All times are GMT -5. The time now is 09:12 PM.