惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
J
Java Code Geeks
Martin Fowler
Martin Fowler
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
博客园 - 【当耐特】
Y
Y Combinator Blog
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog
G
Google Developers Blog
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 司徒正美

博客园 - Zero Lee

调用栈(call stack) 关于STL allocator Calculate maximum sum of any subarray set Calcuate power n of x recursively Convert one binary search tree to double-linked list 设计包含min函数的栈 类模板的模板友元函数定义 一道百度的面试题解答 一道腾讯面试题 (转)C++中extern “C”含义深层探索 selection algorithm to select nth small elements based on partition 删除与某个字符相邻且相同的字符 产生全排列的方法解析 一组数的全排列和组合程序实现 求一个正整数的平方根程序实现 [转]多线程队列的算法优化 [转载] STL allocator的介绍和一个基于malloc/free的allocator的简单实现 如何将一片内存链接成链表 One simple counted object pointer
非printf形式的十六进制和二进制打印(雅虎面试题)
Zero Lee · 2012-06-17 · via 博客园 - Zero Lee

编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列

 1 template <typename T>
 2 void displayHexBin(const T& v)
 3 {
 4     const unsigned char c2h[] = "0123456789ABCDEF";
 5     const unsigned char c2b[] = "01";
 6 
 7     unsigned char* p = (unsigned char*)&v;
 8     char* buf = new char [sizeof(T)*2+1];
 9     char* ptmp = buf;
10     p = p + sizeof(T)-1;
11     for (int i = 0; i < sizeof(T); i++, --p) {
12         *buf++ = c2h[*p >> 4];
13         *buf++ = c2h[*p & 0x0F];
14     }
15     *buf = '\0';
16     printf("hex format displayed as %s\n", ptmp);
17 
18     delete [] ptmp;
19     p = (unsigned char*)&v; p = p + sizeof(T)-1;
20     ptmp = buf = new char [sizeof(T)*8+1];
21     for (int i = 0; i < sizeof(T); i++, --p) {
22         for (int j = 0; j < 8; j++)
23             *buf++ = c2b[(*p >> (7-j)) & 0x1];
24     }
25     *buf = '\0';
26     printf("bin format displayed as %s\n", ptmp);
27     delete [] ptmp;
28 }
29