View Full Version : C e conversione da lettere a binario
devo sviluppare un programma che faccia la compressione dei dati...
mi spiego meglio...
ad esempio la lettera A in binario deve essere 00
la lettera B 01
la lettera C 10
ecc
ora io avrei bisogno di passare una stringa per esempio ABC
e convertirla in questo caso sarebbe 000110
come posso fare??esiste una funzione che mi permetta di tirare fuori le singole lettere da una stringa??
Non hai bisogno di una funzione...c'è già l'operatore []...
stringa[i] von i che va da 0 alla lunghezza della stringa-1 ti fornisce tutte le lettere di una stringa...
puoi usare queste:
NAME
fgetc, fgets, getc, getchar, gets, ungetc - input of characters and strings
SYNOPSIS
#include <stdio.h>
int fgetc(FILE *stream);
char *fgets(char *s, int size, FILE *stream);
int getc(FILE *stream);
int getchar(void);
char *gets(char *s);
int ungetc(int c, FILE *stream);
DESCRIPTION
fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.
getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more than once.
getchar() is equivalent to getc(stdin).
gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with '\0'.
No check for buffer overrun is performed (see BUGS below).
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops
after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the
buffer.
ungetc() pushes c back to stream, cast to unsigned char, where it is available for subsequent read operations. Pushed - back charac-
ters will be returned in reverse order; only one pushback is guaranteed.
Calls to the functions described here can be mixed with each other and with calls to other input functions from the stdio library for
the same input stream.
For non-locking counterparts, see unlocked_stdio(3)
mentre per il fatto di dover salvare in binario, premetto che qui posso dire un sazzo di cavolate ...
NAME
fread, fwrite - binary stream input/output
SYNOPSIS
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
DESCRIPTION
The function fread reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the loca-
tion given by ptr.
The function fwrite writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the
location given by ptr.
magari attendi qualcuno più esperto di me per maggiori spiegazioni e dettagli :D
Ciao
Arpeda
ci avevo pensato e ho fatto così,però mi da abnormal program termination...sono un pò arrugginito con il C non lo uso da un anno :muro:
char alfabeto[3]={'A','B','\0'};
int i;
void main(){
clrscr();
for(i=0;i<2;i++){
printf("vado a stampare l'array: %s",alfabeto[i]);
}
getch();
}
%c al posto di %s... alfabeto[i] è un carattere, non una stringa...
non riesco a trovare una soluzione per cui fare il programma...come potrei farlo??
datemi qualche dritta :)
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.