|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Apr 2005
Messaggi: 7
|
int to string
Ciao a tutti sono un nuovo utente di questo forum, e vi pongo subito un quesito che non riesco a risolvere da tempo!
Cercando di essere chiaro: sto scrivendo un programma in cui ho una variabile "int p_start" che viene incrementata in un loop a seconda delle condizioni imposte. il valore di questa variabile deve essere messo in output. Se il programma fosse da terminale allora non ci sarebbero problemi basterebbe printf("%d\n",p_start); Ma io devo passarlo come valore ad un oggetto "gtk_text_view" che richiede una stringa (gtk_text_view_set_buffer(GTK_TEXT_VIEW(textview),"??"); ). Come posso trasformare il valore di int p_start in un char? Attendo una valanga di risposte ringraziandovi in anticipo, ciao Ultima modifica di slacktony : 25-04-2005 alle 18:09. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Jun 2002
Città: Dublin
Messaggi: 5989
|
Uhm... usare un GtkTextView non è semplicissimo, è stato progettato per supportare anche contenuti complessi, ed altrettanto complesso. Prova a vederti il codice di esempio contenuto in gtk-demo (lancialo, ci sono esempi col relativo codice).
Per la conversione, puoi usare sprintf(). (vedi "man 3 sprintf")
__________________
C'ho certi cazzi Mafa' che manco tu che sei pratica li hai visti mai! |
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: Apr 2005
Messaggi: 7
|
Scusa, potrei anche usare una gtk_entry o gtk_label per mostrare il risultato ma il problema è che tutti questi widget si aspettano una stringa non un valore int . Poi non ho capito come potrei usare sprintf() per convertire la variabile visto che è una funzione di output. sprintf("%d",p_start); stampa nel terminale il valore della variabile p_start ma a me serve passarla come argomento per il widget che come già detto si aspetta una stringa
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Aug 1999
Città: Tolmezzo (UD) - Milano
Messaggi: 13744
|
__________________
...to go where no one has gone before. One ring to rule them all, one ring to find them, one ring to bring them all and in darkness bind them. Caron, non ti crucciare: vuolsi così colà dove si puote ciò che si vuole, e più non dimandare. |
|
|
|
|
|
#5 |
|
Junior Member
Iscritto dal: Apr 2005
Messaggi: 7
|
Ciao Marinelli davvero grande la lib che mi hai segnalato
purtroppo non è standard ansi-c , e non è supportata da gcc(io uso linux) Non esiste un'alternativa standard supportata da gcc? |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Mar 2005
Messaggi: 1653
|
Ciao!
Premetto che sono inesperto di C e soprattutto di GTK, pero' la soluzione di DanieleC88, anche se non troppo elegante, funziona... Usando la sprintf() puoi mettere il tuo intero in un array di caratteri: Codice:
char buffer[DIM]; sprintf(buffer,"%d",p_start); Ho un dubbio su come passare il primo parametro, non so se va bene cosi' come ho fatto o se e' meglio con &buffer[0]... ma forse e' la stessa cosa. Ciao |
|
|
|
|
|
#7 |
|
Junior Member
Iscritto dal: Apr 2005
Messaggi: 7
|
In effetti funziona sprintf (buffer,"%d",p_start) ma il compilatore da i seguenti warning:
$ cc scan.c -o scan $(pkg-config --cflags --libs gtk+-2.0) scan.c: In function `scan_connect': scan.c:48: warning: passing arg 1 of `sprintf' makes pointer from integer without a cast scan.c:50: warning: passing arg 2 of `gtk_entry_append_text' makes pointer from integer without a cast per il resto funziona... grazie ragazzi |
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Oct 2004
Città: Acireale
Messaggi: 447
|
Ehm....ci mostri la dichiarazione di buffer?
__________________
Ho concluso acquisti e/o vendite con : SHIVA>>LuR<<, TheGaiden, ArvMau |
|
|
|
|
|
#9 |
|
Junior Member
Iscritto dal: Apr 2005
Messaggi: 7
|
Vi pasto il codice completo
--------------------------------------------- #include<stdio.h> #include<string.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> #include<gtk/gtk.h> GtkWidget *window; GtkWidget *fixed; GtkWidget *button; GtkWidget *entryip; const char *host; GtkWidget *textview; char *buffer[6]; GtkWidget *label; int p_start=1; int p_end=65535; void esci (GtkWidget *window,gpointer data) { gtk_main_quit(); } void scan_connect(GtkWidget *button,gpointer data) { gtk_entry_set_text(GTK_ENTRY(textview),""); struct sockaddr_in sa; int s, port, check; int connection; while(p_start <= p_end){ s = socket(AF_INET, SOCK_STREAM, 0); if(s != -1){ memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(p_start); check = inet_pton(AF_INET, host, &sa.sin_addr); if (check<= 0){ gtk_entry_set_text(GTK_ENTRY(textview),"Impossibile connettersi all'indirizzo ip scelto"); break; } connection = connect(s, (struct sockaddr *) &sa, sizeof(sa)); if(connection == 0){ sprintf(buffer,"%d",p_start); gtk_entry_append_text(GTK_ENTRY(textview)," - "); gtk_entry_append_text(GTK_ENTRY(textview),buffer); }}else perror("socket()"); close(s); ++p_start; } p_start=1; } int main(int argc, char *argv[]) { gtk_init(&argc, &argv); window =gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window),argv[0]); gtk_window_set_resizable(GTK_WINDOW(window),FALSE); gtk_signal_connect(GTK_OBJECT(window),"destroy",GTK_SIGNAL_FUNC(esci),NULL); fixed=gtk_fixed_new(); gtk_container_add(GTK_CONTAINER(window),fixed); gtk_widget_set_usize(fixed,320,138); gtk_widget_show(fixed); entryip=gtk_entry_new(); gtk_fixed_put(GTK_FIXED(fixed),entryip,10,10); gtk_widget_set_usize(entryip,300,24); gtk_entry_set_max_length(GTK_ENTRY(entryip),15); gtk_entry_set_width_chars(GTK_ENTRY(entryip),15); gtk_widget_show(entryip); host=gtk_entry_get_text(GTK_ENTRY(entryip)); button=gtk_button_new_from_stock("gtk-execute"); gtk_fixed_put(GTK_FIXED(fixed),button,10,44); gtk_widget_set_usize(button,300,30); gtk_widget_show(button); gtk_signal_connect(GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(scan_connect),NULL); textview=gtk_entry_new(); gtk_entry_set_editable (GTK_ENTRY (textview), FALSE); gtk_fixed_put(GTK_FIXED(fixed),textview,10,98); gtk_widget_set_usize(textview,300,30); gtk_widget_show(textview); label=gtk_label_new("Connessione accettata sulle porte TCP:"); gtk_fixed_put(GTK_FIXED(fixed),label,10,74); gtk_widget_set_usize(label,300,24); gtk_widget_show(label); gtk_widget_show(window); gtk_main(); return 0; } |
|
|
|
|
|
#10 |
|
Senior Member
Iscritto dal: Mar 2005
Messaggi: 1653
|
Togli l'asterisco dalla dichiarazione di buffer!
char buffer[6] invece di char *buffer[6] Ciao! |
|
|
|
|
|
#11 | |
|
Senior Member
Iscritto dal: Oct 2004
Città: Acireale
Messaggi: 447
|
Quote:
Togli l'asterisco e aumenta un pò il buffer ( portalo da 6 a 16 ). Vedrai che funziona. Codice corretto Codice:
... char buffer[16]; ...
__________________
Ho concluso acquisti e/o vendite con : SHIVA>>LuR<<, TheGaiden, ArvMau |
|
|
|
|
|
|
#12 |
|
Junior Member
Iscritto dal: Apr 2005
Messaggi: 7
|
In effetti non ci avevo fatto caso all'asterisco
però visto che la variabile p_start può contenere un valore massino di 5 cifre penso sia inutile aumentare l'array char buffer[6]<--che come vedete può contenere 5 char più il carattere \0 cmq di nuovo grazie |
|
|
|
|
|
#13 |
|
Junior Member
Iscritto dal: Apr 2005
Messaggi: 7
|
In effetti non ci avevo fatto caso all'asterisco
però visto che la variabile p_start può contenere un valore massino di 5 cifre penso sia inutile aumentare l'array char buffer[6]<--che come vedete può contenere 5 char più il carattere \0 cmq di nuovo grazie |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 17:58.



















