Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-24-2004, 05:30 PM   #1
Registered: Feb 2004
Location: Lincoln, UK
Distribution: red hat 9
Posts: 67

Rep: Reputation: 15
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
Old 04-24-2004, 05:37 PM   #2
Registered: Feb 2004
Location: Lincoln, UK
Distribution: red hat 9
Posts: 67

Original Poster
Rep: Reputation: 15
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)

return (FALSE);
//function responds to button click
void button_clicked(GtkWidget *widget, gpointer gdata)
gtk_label_set_text( GTK_LABEL(label),"HEX." );
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_label_set_text( GTK_LABEL(label),"DEC." );
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 --*/
gtk_entry_set_text( GTK_ENTRY(entry),"0");
//create label

/*-- 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 --*/


/*-- Start the GTK event loop --*/

/*-- Return 0 if exit is successful --*/
return 0;
Old 04-24-2004, 06:04 PM   #3
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Why won't sprintf() work?

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

Now buffer contains a string that shows my_int in hex.
Old 04-24-2004, 06:27 PM   #4
Registered: Feb 2004
Location: Lincoln, UK
Distribution: red hat 9
Posts: 67

Original Poster
Rep: Reputation: 15
aahhh thats how

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

many thanks


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ string to integer conversion fatman Programming 6 06-18-2006 09:17 AM
C how to convert an integer to string? totti10 Programming 10 11-10-2005 06:49 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 05:46 AM
How do you convert a string to an integer? wbdune Linux - Newbie 11 11-01-2003 03:01 PM
Converting integer types... JStew Programming 3 12-03-2002 09:53 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration