PDA

View Full Version : int to string


slacktony
25-04-2005, 17:39
Ciao a tutti sono un nuovo utente di questo forum, e vi pongo subito un quesito che non riesco a risolvere da tempo! :cry:
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 :)

DanieleC88
25-04-2005, 18:53
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")

slacktony
25-04-2005, 19:32
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 :muro: :mc: :cry:

Marinelli
25-04-2005, 22:14
Potresti usare la funzione "itoa":

http://www.cplusplus.com/ref/cstdlib/itoa.html

Ciao :)

slacktony
26-04-2005, 09:14
Ciao Marinelli davvero grande la lib che mi hai segnalato :eek:
purtroppo non è standard ansi-c , e non è supportata da gcc(io uso linux) :mad:
Non esiste un'alternativa standard supportata da gcc?
:confused:

Gica78R
26-04-2005, 09:30
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:

char buffer[DIM];
sprintf(buffer,"%d",p_start);e in buffer ti trovi la stringa che volevi...
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

slacktony
26-04-2005, 11:46
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
:confused:
per il resto funziona... :)
grazie ragazzi

Blackat
26-04-2005, 12:52
Ehm....ci mostri la dichiarazione di buffer? :D

slacktony
26-04-2005, 13:08
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;
}

Gica78R
26-04-2005, 13:17
Togli l'asterisco dalla dichiarazione di buffer!
char buffer[6] invece di char *buffer[6]

Ciao!

Blackat
26-04-2005, 13:19
...
char *buffer[6];
...



Riga 15, Colonna 6

Togli l'asterisco e aumenta un pò il buffer ( portalo da 6 a 16 ). Vedrai che funziona. ;)

Codice corretto


...
char buffer[16];
...

slacktony
26-04-2005, 14:15
In effetti non ci avevo fatto caso all'asterisco :doh:
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
:read:
cmq di nuovo grazie

slacktony
26-04-2005, 14:16
In effetti non ci avevo fatto caso all'asterisco :doh:
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
:read:
cmq di nuovo grazie :)