[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] char til int




On 2003-01-14 09:09 sslug@sslug wrote:
Kære c-liste,

Hvordan kan jeg convertere enkelte char i et char-array til integers
(0
eller 1)? -Eller kan man erklære et bit-array i c?

Nedestående løsning giver ASCII koden, der så vidt jeg har forstået er
maskinafhængig.

void insert_int(int i);

main(){
char* s= "0110"

while(*s)
 insert_int(*s);
}


Det er vigtigt få både 0 og 1 taller med i converteringen.




Der er mange muligheder, når man ønsker at lagre bit og mit valg afhænger af formålet:


1) Strukturer med bitfields
2) Arrays, hvor jeg masker enkelte bit
3) konvertering af binær streng til integer, base 2

Umiddelbart lyder det som om du er mest interesseret i 3)

Prøv noget i stil med:
char * endptr;
int my_bitstring;

my_bitstring = strtol(s, &endptr, 2),

'my_bitstring' indeholder nu den konverterede streng - der kan dog være problemer med om de også står på de pladser du ønsker, ellers må du lave en shift-operation.
'endptr' kan anvendet til fejldetektion.


/Egon

 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *