设为首页收藏本站官方微博

汉化工具 浮点数和16进制的相互转换工具

[复制链接]
查看: 3061|回复: 2
打印 上一主题 下一主题

[汉化工具] 浮点数和16进制的相互转换工具

跳转到指定楼层
楼主
发表于 2010-2-8 11:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

浮点数和16进制的相互转换工具

在很多图片字库的游戏里用到了浮点数,这个转换工具也许会派上用处。
: b7 t- J7 c+ ~5 j9 X工具是我用C代码自己编译的。$ q5 l8 x2 ]- V/ J& M0 n# Z4 p# `, Q6 a
1 x% {( \+ U5 `! I2 g
原文
* i! L* I6 e6 u8 Y0 r8 v0 fhttp://www.cs.cornell.edu/~tomf/notes/cps104/floating.html$ K# z. D6 l( X* W: [+ t6 |4 \3 \
2 e1 J% Z2 E& O7 }
Helper Software/ m% F2 z9 P. P- e: R( Q
If you're interested in investigating further, I include two programs for which I provide the C code that you can run to gain a greater understanding of how floating point works, and also to check your work on various assignments.
  u. Q+ r) ]& V$ \9 b
0 H7 K5 I' G5 E- d3 LHex 2 Float
7 L: H2 C1 A$ N#include <stdio.h>
& ~- a+ D* C& {
: t6 {# I( ~2 @% H0 j( u3 Oint main(), v% n, a7 Q8 p. Q3 K& B2 [
{
3 x! c/ S/ b5 P  float theFloat;2 x6 n" k  ?0 o6 N% r2 M( A! ~1 o
  while (1) {( Y: p6 K( ?; t9 ^2 J
    scanf("%x", (int *) &theFloat);: G! Q8 P' T5 h+ M; ]9 v
    printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);; u% Z3 H5 l0 v
  }) M; F! ~. x, s1 v
  return 0;
  `/ U* |8 [9 z& @: Y, R3 p4 j}
' y+ U5 b  i; p6 h# P
0 e8 j8 l1 g! n3 Z4 g+ _& w! zThis program accepts as input a hexadecimal quantity and reads it as raw data into the variable "theFloat." The program then outputs the hexadecimal representation of the data in "theFloat" (repeating the input), and prints alongside it the floating point quantity that it represents.
: ~+ a% }1 x  [$ `7 O: B. H5 X" g: D) |8 q" I8 u
I show here a sample run of the program. Notice the special case floating point quantities (0, infinity, and not a number).# x4 c; _" o& R7 ]8 t
" g* V  V% z) c9 p6 M; C# |# W% `
For the denormalized but nonzero numbers, this program will display zero even though the number is not really zero. If you want to get around this problem, replace the %f in the formatting string of the printf function with %e, which will deplay the number to great precision with scientific notation. I did not have it as %e because I find scientific notation extremely annoying.0 z: \5 p8 _# d- B; e; ~6 W
$ G" {& }+ w: S9 n! }' m5 n
C0B40000
) U3 [: D9 C+ }0 l7 w5 [% R) V0xC0B40000, -5.625000* |5 T! i. E' o8 c
43A4B200
  L' a/ Y+ |  c' K  _  S+ [0x43A4B200, 329.390625
0 O8 ]) d% a1 a5 |5 [' h  u000000001 t8 x1 i% F" u  [
0x00000000, 0.000000  I$ x# e- B0 J
80000000+ r' V5 O5 B$ W8 g( e; N2 S3 c: P* ~* L
0x80000000, -0.000000# o, @/ N" Q( z" C7 {
7f800000$ R$ r% c' o, R
0x7F800000, inf
* l2 A$ t  A( R# j# o) C7 H# ~; Q+ Lff800000
3 T9 o: G$ u5 |  f3 e# V0xFF800000, -inf
5 D& x/ ]- u/ R4 B- k7fffffff# ]! a, d7 W* c* j1 o4 Q! L
0x7FFFFFFF, NaN& e9 g8 f, i+ u* o1 _
ffffffff
' z5 m0 ]# G' _, n) F& @- U# V0xFFFFFFFF, NaN6 g3 O' q! v# ]4 \
7f81A023
  \8 G+ l0 G6 z& ?* i$ k0x7F81A023, NaN
: R8 u1 K  ]$ B% U/ n4 j! Y
! `& U0 v. p& ^( s/ {Float 2 Hex
, R* {, y5 v7 V! U#include <stdio.h>
. Q; |% T+ g3 `$ s* x# Y& ^- h# ]: m+ m$ @$ H! b% W# o
int main()
. H$ B* O+ {/ L{1 Y4 R) r  ?' R, s) g
  float theFloat;
8 s8 d+ a( M8 x8 z1 l2 k) m  while (1) {
7 @, F( C! O: ?* n9 w4 U    scanf("%f", &theFloat);
4 j  [2 k2 X' ?    printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);  B* b6 h" t8 W
  }  m8 U( F" P' Y
  return 0;2 z4 u$ n7 X- @
}+ S( {! f3 \& T9 q1 F7 s, x6 W, m0 `
2 \2 O& B& h& p0 G- b" E
This is a slight modification of the "Hex 2 Float" program. The exception is it reads in a floating point number. Just like and outputs the hexadecimal form plus the floating point number. Again I include a sample run of this program, confirming the results of the example problems I covered earlier in this text, along with some other simple cases. Notice the hexadecimal representation of 0.2.
5 l+ H. W! _- H6 T) ^9 l7 z2 A' Q1 {5 l* b+ |. M% p
-5.625! [& R% k9 Q5 m1 _7 T* B
0xC0B40000, -5.625000
9 D$ Y" K: }% @. e329.390625, F+ t  A2 E* j( O
0x43A4B200, 329.390625
# [7 Z1 e' A& K; d, m- {9 E3 `; }01 ~' N' T7 o- f% J- w( G% k
0x00000000, 0.000000/ l8 |$ e4 m) L: g2 B$ B
-0; ]5 [2 K" D' P% P; D/ U( q6 k
0x80000000, -0.000000% Y0 ?" Z* |# n  @! D
.2) R2 W3 t% b' p0 P- u, N! {: a
0x3E4CCCCD, 0.200000
# b( P  |, ^/ @.5
" Q1 a, J4 L: a$ o1 {9 A0x3F000000, 0.500000
5 Y$ j1 o: ?1 d, \  S1
0 k# X9 @3 a: j0x3F800000, 1.000000

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 很美好很美好 很差劲很差劲
回复

使用道具 举报

沙发
发表于 2010-2-16 01:36 | 只看该作者
多谢楼主,我来看看 [s:45]
回复 支持 反对

使用道具 举报

板凳
发表于 2010-2-16 01:37 | 只看该作者
正需要 [s:42]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

冒险解谜游戏中文网 ChinaAVG

官方微博官方微信号小黑屋 微信玩家群  

(C) ChinaAVG 2004 - 2019 All Right Reserved. Powered by Discuz! X3.2
辽ICP备11008827号 | 桂公网安备 45010702000051号

冒险,与你同在。 冒险解谜游戏中文网ChinaAVG诞生于2004年9月9日,是全球华人共同的冒险解谜类游戏家园。我们致力于提供各类冒险游戏资讯供大家学习交流。本站所有资源均不用于商业用途。

快速回复 返回顶部 返回列表