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

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

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

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

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

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

在很多图片字库的游戏里用到了浮点数,这个转换工具也许会派上用处。
) {4 A$ Y, [, E% U工具是我用C代码自己编译的。
& a, z% q) Z8 M
2 A" L. Y0 P( k! c* N原文3 a, ?0 E0 O1 O( l% S8 ^( }# [! Y. }
http://www.cs.cornell.edu/~tomf/notes/cps104/floating.html; U1 T& |" s/ s: R5 e
' ~8 {- |4 k$ s0 }7 x
Helper Software
% V1 ]0 U6 k1 E+ \+ v  k, FIf 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.
& ^1 A/ c) w' I  K( `* M2 t- s* K3 w
Hex 2 Float6 Z+ J; f: c8 V0 n  F/ t
#include <stdio.h>
# w4 e' r1 C& y! n; R! l' E+ O7 P# \
0 J* K' c) N  `int main()
0 B: g" Y4 ?1 k1 T1 C{
& u7 {+ T: g3 _3 i) B  float theFloat;* `  g( X$ S# u4 z1 `  p
  while (1) {
4 a7 {7 v* }5 E7 c) o4 h+ m    scanf("%x", (int *) &theFloat);; e7 d# K/ `6 Z( B0 R! @
    printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);4 T! f& o$ n! D' s+ g0 b
  }: [$ o" X* E( i, ]- u6 y, j0 s
  return 0;4 q) ^4 X- ~8 Z" c4 W
}0 f2 u) L1 R/ e- O) j* A

* Z( ]& X4 b4 S5 \1 B- hThis 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.
/ r1 i, G' B" z8 x4 ~# b$ K' i0 v# }8 X
I show here a sample run of the program. Notice the special case floating point quantities (0, infinity, and not a number).
2 y3 J5 C" N6 W, H3 c0 L
# k* f2 y) [8 g; Z, y( iFor 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.
# B4 x% x0 j6 U
, W. j; R  y3 _2 M4 G5 X2 q2 D- WC0B400007 x6 ]2 ~/ f& H0 ?/ s
0xC0B40000, -5.6250008 q6 `0 t6 L; N$ O$ J
43A4B200" t6 R# w; a8 T* z
0x43A4B200, 329.390625
8 L7 r# B0 T/ C00000000
9 b% s) W* E) l( g3 e9 f% V0x00000000, 0.0000001 O5 z! k  @/ n$ T1 f/ S
800000007 w7 [" V+ t0 }) C+ l5 ]# B
0x80000000, -0.000000
  g. L) S# u1 n/ L  ?' L9 E* k7f800000/ a, E, d! X) c( @
0x7F800000, inf
8 }2 d9 G( m  Rff800000
  e6 p2 _6 \, v- g0xFF800000, -inf
: i+ d4 T6 k2 h: h+ P7fffffff) D& W# T( |; w* d' n
0x7FFFFFFF, NaN) C( u* i! g, M5 d( N
ffffffff
) v2 ^6 A- ]( v4 `0xFFFFFFFF, NaN
) x) e4 i$ o9 e1 r& R0 u7f81A023
$ l# }& W. N+ b1 j- _- N0x7F81A023, NaN) @" V1 P" O' B7 p8 s; Q
, K/ x9 K) ?' C( y  b6 M9 g
Float 2 Hex: G) J0 S" B5 q  U7 b, ^
#include <stdio.h>8 j5 }% |1 A/ R

" U( o# L) R3 Z, j) H" ]/ `int main()
* k% g3 p, a1 m% @4 D- [{3 {3 ]6 W: s  K4 f& Y# @
  float theFloat;' y, X6 q+ `- r
  while (1) {. z7 b7 W( H" \) S+ h9 `
    scanf("%f", &theFloat);
) U$ Y5 {* j% T6 e( S    printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);
+ U; B7 b1 j/ C8 g( H' A, J  }+ U# p7 @3 ?" F' H0 P6 C
  return 0;
. r( D' @4 @) G, g2 f! {% `) K}
$ U& ?) B3 Y2 M
8 b9 S" a. ]5 xThis 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., }- K7 G9 H% C% l* y# G, k. Q' ^

' |  i1 t9 M' k$ I7 _) v7 ^! V  h-5.625& ?& v; E( \* |' Y7 f( c1 J
0xC0B40000, -5.6250002 Z) v5 o2 C# S( U2 E" V
329.390625
% b( k' e- ~$ o% g, ~' {0x43A4B200, 329.390625! |% e; E* t( Q  ?& n6 L
0" m  F; Y- t! v7 S0 S% X
0x00000000, 0.000000& S" q0 X( I; G  X! v! b
-0) m9 q- ]8 h0 Y5 ^
0x80000000, -0.000000
2 S' s6 F6 y! T+ [4 G.2* X7 I* ?& F! Y  I
0x3E4CCCCD, 0.200000/ G  b. V; v  J1 P: P5 Q1 O
.5
0 \5 V. ]% T3 F6 `* f, z4 f0x3F000000, 0.500000
, h: V4 ?5 I1 D/ \1/ V# [1 X5 g$ j1 e
0x3F800000, 1.000000

本帖子中包含更多资源

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

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

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

本版积分规则

冒险解谜游戏中文网 ChinaAVG

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

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

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

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