0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

plunkerでieee754 その2

Posted at

#概要

plunkerでieee754やってみた。
dataview使って、浮動小数点と整数を変換してみた。

#サンプルコード

var src = document.getElementById("src"); 
var out = document.getElementById("out"); 

function h2b(str) {
  var buffer = new ArrayBuffer(4);
  var bytes = new Uint8Array(buffer);
  var v = new DataView(buffer);  
  bytes[0] = parseInt(str[0] + str[1], 16);
  bytes[1] = parseInt(str[2] + str[3], 16);
  bytes[2] = parseInt(str[4] + str[5], 16);
  bytes[3] = parseInt(str[6] + str[7], 16);
  return v.getFloat32(0);
}
function h2i(str) {
  var buffer = new ArrayBuffer(4);
  var bytes = new Uint8Array(buffer);
  var v = new DataView(buffer);  
  bytes[0] = parseInt(str[0] + str[1], 16);
  bytes[1] = parseInt(str[2] + str[3], 16);
  bytes[2] = parseInt(str[4] + str[5], 16);
  bytes[3] = parseInt(str[6] + str[7], 16);
  return v.getInt32(0);
}
function run() {
  var str = src.value;
  var codes = str.split("\n");  
	for (var i = 0; i < codes.length; i++)
	{
    var line = codes[i].split(" ");
    var v = h2b(line[0]);
    out.value += v + "\t";
    v = h2i(line[1]);
    out.value += v + "\n";
	}
}




#投入したソース

浮動小数点と整数

c0490fdb ffffcdbc
c03ba866 ffffd116
c02e40f1 ffffd470
c020d97c ffffd7ca
c0137207 ffffdb23
c0060a92 ffffde7d
bff1463a ffffe1d7
bfd67750 ffffe531
bfbba866 ffffe88b
bfa0d97c ffffebe5
bf860a92 ffffef3f
bf567750 fffff299
bf20d97c fffff5f2
bed67750 fffff94c
be567750 fffffca6
a6000000 00000000
3e567750 0000035a
3ed67750 000006b4
3f20d97c 00000a0e
3f567750 00000d67
3f860a92 000010c1
3fa0d97c 0000141b
3fbba866 00001775
3fd67750 00001acf
3ff1463a 00001e29
40060a92 00002183
40137207 000024dd
4020d97c 00002836
402e40f1 00002b90
403ba866 00002eea
40490fdb 00003244

#結果

-3.1415927410125732	-12868
-2.9321532249450684	-12010
-2.7227137088775635	-11152
-2.5132741928100586	-10294
-2.3038346767425537	-9437
-2.094395160675049	-8579
-1.884955644607544	-7721
-1.675516128540039	-6863
-1.4660766124725342	-6005
-1.2566370964050293	-5147
-1.0471975803375244	-4289
-0.8377580642700195	-3431
-0.6283185482025146	-2574
-0.41887903213500977	-1716
-0.20943951606750488	-858
-4.440892098500626e-16	0
0.20943951606750488	858
0.41887903213500977	1716
0.6283185482025146	2574
0.8377580642700195	3431
1.0471975803375244	4289
1.2566370964050293	5147
1.4660766124725342	6005
1.675516128540039	6863
1.884955644607544	7721
2.094395160675049	8579
2.3038346767425537	9437
2.5132741928100586	10294
2.7227137088775635	11152
2.9321532249450684	12010
3.1415927410125732	12868
0	

#成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?