{"id":403,"date":"2020-01-28T12:08:10","date_gmt":"2020-01-28T12:08:10","guid":{"rendered":"https:\/\/www.weeklywhinge.com\/?p=403"},"modified":"2020-01-28T13:21:38","modified_gmt":"2020-01-28T13:21:38","slug":"one-liner-change-binary-constants-in-c-source-file-to-hex","status":"publish","type":"post","link":"https:\/\/www.weeklywhinge.com\/?p=403","title":{"rendered":"one-liner: change binary constants in C source file to hex"},"content":{"rendered":"\n<p>If you have an old C compiler that won&#8217;t work with binary constants like 0b10101010 you&#8217;ll end up with a fatal compile error:<br><br>invalid suffix &#8220;b10101010&#8221; on integer constant<br><br>One-liner:<br><\/p>\n\n\n\n<p style=\"text-align: left\"><code>grep -l '\\b0b[01][01]' . -r | while read f; do perl -np -e 's\/\\b0b([01]+)\/sprintf(\"0x%X\\\/*%s*\\\/\", oct(\"0b$1\"), $1)\/eg' &lt; $f &gt; $f.new &amp;&amp; mv $f.new $f; done<\/code><br><\/p>\n\n\n\n<p>This replaces all binary values with hex equivalents, with the binary value in C comments after.<br><br>This will fail if you have binary values inside \/* comments *\/; you can remove the comment value from the replacement string if  you prefer:<\/p>\n\n\n\n<p style=\"text-align: left\"><code>grep -l '\\b0b[01][01]' . -r | while read f; do perl -np -e 's\/\\b0b([01]+)\/sprintf(\"0x%X\", oct(\"0b$1\"))\/eg' &lt; $f &gt; $f.new &amp;&amp; mv $f.new $f; done<\/code><br><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have an old C compiler that won&#8217;t work with binary constants like 0b10101010 you&#8217;ll end up with a fatal compile error: invalid suffix &#8220;b10101010&#8221; on integer constant One-liner: grep -l &#8216;\\b0b[01][01]&#8217; . -r | while read f; do perl -np -e &#8216;s\/\\b0b([01]+)\/sprintf(&#8220;0x%X\\\/*%s*\\\/&#8221;, oct(&#8220;0b$1&#8221;), $1)\/eg&#8217; &lt; $f &gt; $f.new &amp;&amp; mv $f.new $f; done [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-403","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts\/403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=403"}],"version-history":[{"count":13,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts\/403\/revisions"}],"predecessor-version":[{"id":417,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts\/403\/revisions\/417"}],"wp:attachment":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}