Article View: be.comp.programming
Article #3665Re: binary string als int afdrukken in C
From: staf wagemakers
Date: Sat, 29 Nov 2003 16:24
Date: Sat, 29 Nov 2003 16:24
29 lines
1621 bytes
1621 bytes
On 2003-11-29, Gunter Schelfhout <nospam@please.com> wrote:
> Hoe kan een string (bv "1100100101001001") afdrukken als een int in C met
> behulp van de functies in glibc?
> Met printf?
er bestaat hievoor geen standaard functie voor in libc
...of ik ken hem niet ;-)
het is echter niet zo moeilijk om van een "binstring" een integer te
maken, enkel karakter is bit*2^plaats waard.
char str[] = "1111111111111111";
int i=0;
int shift=0;
char *c;
for (c=str+strlen(str)-1;*c!='\0';c--) {
int bit=0;
if(*c!='0') bit = 1;
i+=(bit<<shift++);
}
printf("i = %d\n",i);
--
Staf Wagemakers
email: staf at patat.org
homepage: http://staf.patat.org
Technical Details
Message-ID:
<slrnbshhuh.3sc.staf_nospam_@localhost.staflaptop>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.wirehub.nl!skynet.be!skynet.be!louie!tjb!not-for-mailReferences:
<td1yb.50435$xX1.1629315@phobos.telenet-ops.be>Show Full Headers
Complete Article Headers
X-Google-Language: DUTCH,ASCII-7-bit X-Google-Thread: fcea4,12763d3d6faef1bf X-Google-Attributes: gidfcea4,public X-Google-ArrivalTime: 2003-11-29 08:24:52 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.wirehub.nl!skynet.be!skynet.be!louie!tjb!not-for-mail Newsgroups: be.comp.programming From: staf wagemakers <staf_nospam_@patat.org> Subject: Re: binary string als int afdrukken in C References: <td1yb.50435$xX1.1629315@phobos.telenet-ops.be> Organization: stafnet Message-ID: <slrnbshhuh.3sc.staf_nospam_@localhost.staflaptop> User-Agent: slrn/0.9.8.0 (Linux) Date: 29 Nov 2003 16:24:51 GMT Lines: 29 NNTP-Posting-Host: 81.240.203.16 X-Trace: 1070123091 reader3.news.skynet.be 28942 81.240.203.16:34327 X-Complaints-To: usenet-abuse@skynet.be