TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Convert hex to binary faster than xxd part 2/2

1 pointsby textmodeabout 4 years ago
<p><pre><code> static int hexdigit(char x) { if (x &gt;= &#x27;0&#x27; &amp;&amp; x &lt;= &#x27;9&#x27;) return x - &#x27;0&#x27;; if (x &gt;= &#x27;a&#x27; &amp;&amp; x &lt;= &#x27;f&#x27;) return 10 + (x - &#x27;a&#x27;); if (x &gt;= &#x27;A&#x27; &amp;&amp; x &lt;= &#x27;F&#x27;) return 10 + (x - &#x27;A&#x27;); return -1; } int hexparse(unsigned char *y,long long len,const char *x) { if (!x) return 0; while (len &gt; 0) { int digit0; int digit1; digit0 = hexdigit(x[0]); if (digit0 == -1) return 0; digit1 = hexdigit(x[1]); if (digit1 == -1) return 0; *y++ = digit1 + 16 * digit0; --len; x += 2; } if (x[0]) return 0; return 1; } #define BLOCK 4096 static unsigned char buf[BLOCK + 1]; int main(int argc, char **argv) { long long r,w; for (;;) { r = readblock(0, buf, BLOCK); if (r == -1) _exit(0); for (;;) { if (r &lt;= 0) goto end; if (buf[r - 1] == &#x27;\n&#x27;) { --r; continue; } if (buf[r - 1] == &#x27;\r&#x27;) { --r; continue; } break; } buf[r] = 0; if (r % 2) _exit(0); w = r &#x2F; 2; if (!hexparse(buf, w, (char *)buf)) break; if (writeall(1, buf, w) == -1) break; if (r != BLOCK) break; } end: if (fsyncfd(1) == -1) _exit(0); _exit(0); }</code></pre>

no comments

no comments