{"id":249,"date":"2023-02-22T01:47:57","date_gmt":"2023-02-22T09:47:57","guid":{"rendered":"http:\/\/bitas.net.cn\/?page_id=249"},"modified":"2026-04-07T03:13:11","modified_gmt":"2026-04-07T11:13:11","slug":"039-2","status":"publish","type":"page","link":"http:\/\/bitas.net.cn\/index.php\/sourcecode\/039-2\/","title":{"rendered":"039 bindump"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n\nvoid dump_bits(unsigned char *addr, size_t size)\n{\n  char buf&#91;256];\n  int j, buflen;\n  while (size > 0)\n  {\n    int head = size % 4;\n    if (head == 0)\n    {   \n      \/\/ print Bit position, like B32 B31 B30 ...\n      buflen = 0;\n      for (j = size*8; j > size*8-32; j--)\n        buflen += snprintf(buf+buflen, sizeof(buf), \"|B%2d\", j-1);\n      printf(\"%s\\n\", buf);\n      \/\/ print upper seperator(-) with same length with Bit position\n      for (j = buflen; j > 0; j--) printf(\"-\");\n      printf(\"\\n\");\n      \/\/ print Bit value\n      j = 32; \n      while (j--) printf(\"|% 3d\", ((*(addr+size-4+j\/8) >> j%8) &amp; 0x01));\n      printf(\"\\n\");\n      \/\/ print lower seperator(-) with same length with Bit position\n      for (j = buflen; j > 0; j--) printf(\"-\");\n      printf(\"\\n\");\n      size -= 4;\n    }   \n    else \/\/ the size of object is not times of 4 bytes, print the head\n    {   \n      \/\/ print Bit position, like B32 B31 B30 ...\n      buflen = 0;\n      for (j = size*8; j > 0; j--)\n        buflen += snprintf(buf+buflen, sizeof(buf), \"|B%2d\", j-1);\n      printf(\"%s\\n\", buf);\n      \/\/ print upper seperator(-) with same length with Bit position\n      for (j = buflen; j > 0; j--) printf(\"-\");\n      printf(\"\\n\");\n      \/\/ print Bit value\n      j = head * 8;\n      while (j--) printf(\"|% 3d\", ((*(addr+size-head+j\/8) >> j%8) &amp; 0x01));\n      printf(\"\\n\");\n      \/\/ print lower seperator(-) with same length with Bit position\n      for (j = buflen; j > 0; j--) printf(\"-\");\n      printf(\"\\n\");\n      size -= head;\n    }   \n  }\n}\n\nint main(int argc, char *argv&#91;])\n{\n  size_t size;\n\n  char c = 'a';\n  size = sizeof(c);\n  printf(\"&lt;&lt; char(%ld bytes, value %d) >>\\n\", size, c);\n  dump_bits((unsigned char *)&amp;c, size);\n\n  int i = 2;\n  size = sizeof(i);\n  printf(\"&lt;&lt; int(%ld bytes, value %d) >>\\n\", size, i);\n  dump_bits((unsigned char *)&amp;i, size);\n\n  short s = 10;\n  size = sizeof(s);\n  printf(\"&lt;&lt; short(%ld bytes, value %d) >>\\n\", size, s);\n  dump_bits((unsigned char *)&amp;s, size);\n\n  long int l = 65L;\n  size = sizeof(l);\n  printf(\"&lt;&lt; long(%ld bytes, value %ld) >>\\n\", size, l);\n  dump_bits((unsigned char *)&amp;l, size);\n\n  long long ll = 130LL;\n  size = sizeof(ll);\n  printf(\"&lt;&lt; long long(%ld bytes, value %lld) >>\\n\", size, ll);\n  dump_bits((unsigned char *)&amp;ll, size);\n\n  float f = 1.5;\n  size = sizeof(f);\n  printf(\"&lt;&lt; float(%ld bytes, value %.2f) >>\\n\", size, f);\n  dump_bits((unsigned char *)&amp;f, size);\n\n  double d = 9.9;\n  size = sizeof(d);\n  printf(\"&lt;&lt; double(%ld bytes, value %.2f) >>\\n\", size, d);\n  dump_bits((unsigned char *)&amp;d, size);\n\n  return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":127,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-249","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/comments?post=249"}],"version-history":[{"count":1,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/249\/revisions"}],"predecessor-version":[{"id":250,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/249\/revisions\/250"}],"up":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/127"}],"wp:attachment":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/media?parent=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}