LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 04-24-2004, 04:30 PM   #1
woodywellhung
Member
 
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, 04:37 PM   #2
woodywellhung
Member
 
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)
{

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;
}
 
Old 04-24-2004, 05:04 PM   #3
itsme86
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, 05:27 PM   #4
woodywellhung
Member
 
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
 
  


Reply



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 08:17 AM
C how to convert an integer to string? totti10 Programming 10 11-10-2005 05:49 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM
How do you convert a string to an integer? wbdune Linux - Newbie 11 11-01-2003 02:01 PM
Converting integer types... JStew Programming 3 12-03-2002 08:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 PM.

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