一个简单的获取字符ASCII码值的程序,支持多字符
一个简单的获取字符ASCII码值的程序作者:AlimysoYang
#include <gtk/gtk.h>
GtkWidget *wnd;
GtkWidget *hbox1,*hbox2,*hbox3,*vbox;
GtkWidget *lbchars,*lbasciivalue;
GtkWidget *etchars,*etasciivalue;
GtkWidget *btnascii;
void ui_init()
{
wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(wnd),"ASCII转换");
gtk_window_set_position(GTK_WINDOW(wnd),GTK_WIN_POS_CENTER);
lbchars = gtk_label_new("请输入");
etchars = gtk_entry_new();
hbox1 = gtk_hbox_new(FALSE,2);
gtk_box_pack_start(GTK_BOX(hbox1),lbchars,FALSE,TRUE,2);
gtk_box_pack_start(GTK_BOX(hbox1),etchars,TRUE,TRUE,2);
lbasciivalue = gtk_label_new("ASCII值");
etasciivalue = gtk_entry_new();
hbox2 = gtk_hbox_new(FALSE,2);
gtk_box_pack_start(GTK_BOX(hbox2),lbasciivalue,FALSE,TRUE,2);
gtk_box_pack_start(GTK_BOX(hbox2),etasciivalue,TRUE,TRUE,2);
btnascii = gtk_button_new_with_label("ASCII码转换");
hbox3 = gtk_hbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox3),btnascii,TRUE,TRUE,0);
vbox = gtk_vbox_new(TRUE,2);
gtk_box_pack_start(GTK_BOX(vbox),hbox1,TRUE,TRUE,2);
gtk_box_pack_start(GTK_BOX(vbox),hbox2,TRUE,TRUE,2);
gtk_box_pack_start(GTK_BOX(vbox),hbox3,TRUE,TRUE,2);
gtk_container_add(GTK_CONTAINER(wnd),vbox);
gtk_widget_show_all(wnd);
}
int chartoascii(gchar c)
{
int i = c;
return i;
}
void btnascii_clicked(GtkButton *button,gpointer user_data)
{
gtk_entry_set_text(GTK_ENTRY(etasciivalue),"");
const gchar *str = gtk_entry_get_text(GTK_ENTRY(etchars));
if (str!=NULL)
{
gchar *c;
c = &(*str);
const gchar *result = "";
int i,ir,len;
len = strlen(str);
for (i = 0;i<len;i++)
{
ir = chartoascii(*c);
c++;
result = g_strdup_printf("%d ",ir);
gtk_entry_append_text(GTK_ENTRY(etasciivalue),result);
}
}
}
int main(int argc,char *argv[])
{
gtk_init(&argc,&argv);
ui_init();
g_signal_connect(G_OBJECT(wnd),"destroy",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(G_OBJECT(btnascii),"clicked",G_CALLBACK(btnascii_clicked),NULL);
gtk_main();
return 0;
} 嗯,这个东西很不错,赞一个~ 新手学习啦。。。谢谢LZ。 好
页:
[1]