以十六进制格式存储数字 [英] Storing number in Hex format

查看:129
本文介绍了以十六进制格式存储数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将十六进制格式的long long数字的值存储到缓冲区中。

什么是正确的&这样做的最佳方式是什么?


例如:如果数字为:374643194001883136,则存储的值应为

0x0533000000000000

我目前的情况是这样的:


long long Id = 374643194001883136LL;

char IdInHexBuf [50] = {0};

sprintf(IdInHexBuf,"%#018llx",Id);


改进此代码的任何评论?

I have to store the value of a ''long long'' number in Hex format into a buffer.
What is the correct & best way of doing it so?

E.g.: If the number is: 374643194001883136, the stored value should be
0x0533000000000000
What I have currently is something like this:

long long Id = 374643194001883136LL ;
char IdInHexBuf[50]= {0} ;
sprintf( IdInHexBuf , "%#018llx" , Id ) ;

Any comments to improve this code?

推荐答案



" qazmlp" < QA ******** @ rediffmail.com> écritdansle message de

news:db ************************** @ posting.google.c om ...

"qazmlp" <qa********@rediffmail.com> a écrit dans le message de
news:db**************************@posting.google.c om...
我必须将十六进制格式的long long数字的值存储到
缓冲区中。什么是正确的&这样做的最好方法是什么?

例如:如果数字是:374643194001883136,存储的值应该是
0x0533000000000000

我现在的东西是这样的:

long long Id = 374643194001883136LL;
char IdInHexBuf [50] = {0};
sprintf(IdInHexBuf,"%#018llx",Id);

任何改进此代码的评论?
I have to store the value of a ''long long'' number in Hex format into a buffer. What is the correct & best way of doing it so?

E.g.: If the number is: 374643194001883136, the stored value should be
0x0533000000000000
What I have currently is something like this:

long long Id = 374643194001883136LL ;
char IdInHexBuf[50]= {0} ;
sprintf( IdInHexBuf , "%#018llx" , Id ) ;

Any comments to improve this code?




没关系!


如果它是'a' 不要破坏它!



It is OK!

"If it ain''t broken do not fix it!"


qazmlp写于26/07/04:
qazmlp wrote on 26/07/04 :
我必须存储一个值将十六进制格式的long long数字转换为
缓冲区。
我现在所拥有的是这样的:

long long Id = 374643194001883136LL;
char IdInHexBuf [50] = {0};
sprintf(IdInHexBuf," ;%#018llx",Id);

任何改进此代码的评论?
I have to store the value of a ''long long'' number in Hex format into a
buffer. What I have currently is something like this:

long long Id = 374643194001883136LL ;
char IdInHexBuf[50]= {0} ;
sprintf( IdInHexBuf , "%#018llx" , Id ) ;

Any comments to improve this code?




- 请确保< stdio.h>包括在内。

- ''= {0};''的东西没用但是无害。

- 我不确定''的含义格式字符串中的#''。

- "%llX"期待''无条件长长''。

- 你的间距可以改善......(风格问题)


-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html


C是一个尖锐的工具



- Be sure that <stdio.h> is included.
- The ''= {0} ;'' thing is useless but harmless.
- I''m not sure about the meaning of the ''#'' in the format string.
- "%llX" expects ''an unsigned long long''.
- Your spacing could be improved... (style-matter)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"


qazmlp写道:

我必须将''long long''的值存储在Hex中格式化为缓冲区。
什么是正确的&这样做的最好方法是什么?

例如:如果数字是:374643194001883136,存储的值应该是
0x0533000000000000

我目前的东西是这样的:

long long Id = 374643194001883136LL;
char IdInHexBuf [50] = {0};
sprintf(IdInHexBuf,"%#018llx",Id);

任何改进此代码的评论?

I have to store the value of a ''long long'' number in Hex format into a buffer.
What is the correct & best way of doing it so?

E.g.: If the number is: 374643194001883136, the stored value should be
0x0533000000000000

What I have currently is something like this:

long long Id = 374643194001883136LL ;
char IdInHexBuf[50]= {0} ;
sprintf( IdInHexBuf , "%#018llx" , Id ) ;

Any comments to improve this code?




在这种情况下,使用sprintf是可以的,但通常你应该使用
snprintf:


snprintf(IdInHexBuf,sizeof(IdInHexBuf),"%#018llx",Id);


可以防止beuffer溢出。


Erik

-

+ ----------------- ------------------------------------------ +

Erik de Castro Lopo no****@mega-nerd.com (是的,它是有效的)

+ ------------------------------------------- ---------------- +

事实上,谷歌运行给我留下了深刻的印象一个8,000个节点的Linux

集群,5个数据中心,一个广泛的网络,以及一个快速的b
不断发展的应用程序,所有人员均为12人。

- http://research.microsoft.com/~灰色/ ... FAAMs_HPTS.doc



In this case, using sprintf is OK, but normally you should
use snprintf:

snprintf ( IdInHexBuf , sizeof ( IdInHexBuf ), "%#018llx" , Id ) ;

which can prevent beuffer overflows.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it''s valid)
+-----------------------------------------------------------+
"Indeed, I am impressed that Google runs an 8,000 node Linux
cluster, 5 data centers, an extensive network, and a rapidly
evolving application all with a staff of 12."
-- http://research.microsoft.com/~gray/...FAAMs_HPTS.doc


这篇关于以十六进制格式存储数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆