Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add binary_to_(integer|float) to (integer|float)_to_binary

  • Loading branch information...
commit 17b5ede88f6f983b10c4e10e3e0d882008f6cb77 1 parent ab4c932
Devin Torres authored
4  lib/elixir/lib/binary/inspect.ex
@@ -233,10 +233,6 @@ defimpl Binary.Inspect, for: BitString do
233 233
     integer_to_binary(h) <> "::size(" <> integer_to_binary(size) <> ")"
234 234
   end
235 235
 
236  
-  defp integer_to_binary(integer) do
237  
-    integer /> integer_to_list /> list_to_binary
238  
-  end
239  
-
240 236
   defp decrement(:infinity), do: :infinity
241 237
   defp decrement(counter),   do: counter - 1
242 238
 end
94  lib/elixir/lib/kernel.ex
@@ -2349,9 +2349,96 @@ defmodule Kernel do
2349 2349
   end
2350 2350
 
2351 2351
   @doc """
  2352
+  Returns a integer whose text representation is `some_binary`.
  2353
+
  2354
+  ## Examples
  2355
+
  2356
+      binary_to_integer "123" #=> 123
  2357
+
  2358
+  """
  2359
+  defmacro binary_to_integer(some_binary) do
  2360
+    quote do
  2361
+      list_to_integer(binary_to_list(unquote(some_binary)))
  2362
+    end
  2363
+  end
  2364
+
  2365
+  @doc """
  2366
+  Returns an integer whose text representation in base `base`
  2367
+  is `some_binary`.
  2368
+
  2369
+  ## Examples
  2370
+
  2371
+      binary_to_integer("3FF", 16) #=> 1023
  2372
+
  2373
+  """
  2374
+  defmacro binary_to_integer(some_binary, base) do
  2375
+    quote do
  2376
+      list_to_integer(binary_to_list(unquote(some_binary)), unquote(base))
  2377
+    end
  2378
+  end
  2379
+
  2380
+  @doc """
  2381
+  Returns a float whose text representation is `some_binary`.
  2382
+
  2383
+  ## Examples
  2384
+
  2385
+      binary_to_float "2.2017764e+0" #=> 2.2017764
  2386
+
  2387
+  """
  2388
+  defmacro binary_to_float(some_binary) do
  2389
+    quote do
  2390
+      list_to_float(binary_to_list(unquote(some_binary)))
  2391
+    end
  2392
+  end
  2393
+
  2394
+  @doc """
  2395
+  Returns a binary which corresponds to the text representation
  2396
+  of `some_integer`.
  2397
+
  2398
+  ## Examples
  2399
+
  2400
+      integer_to_binary 123 #=> "123"
  2401
+
  2402
+  """
  2403
+  defmacro integer_to_binary(some_integer) do
  2404
+    quote do
  2405
+      list_to_binary(integer_to_list(unquote(some_integer)))
  2406
+    end
  2407
+  end
  2408
+
  2409
+  @doc """
  2410
+  Returns a binary which corresponds to the text representation
  2411
+  of `some_integer` in base `base`.
  2412
+
  2413
+  ## Examples
  2414
+
  2415
+      integer_to_binary 77 #=> "77"
  2416
+
  2417
+  """
  2418
+  defmacro integer_to_binary(some_integer, base) do
  2419
+    quote do
  2420
+      list_to_binary(integer_to_list(unquote(some_integer), unquote(base)))
  2421
+    end
  2422
+  end
  2423
+
  2424
+  @doc """
  2425
+  Returns a binary which corresponds to the text representation
  2426
+  of `some_float`.
  2427
+
  2428
+  ## Examples
  2429
+
  2430
+      float_to_binary 7.0 #=> "7.00000000000000000000e+00"
  2431
+
  2432
+  """
  2433
+  defmacro float_to_binary(some_float) do
  2434
+    quote do
  2435
+      list_to_binary(float_to_list(unquote(some_float)))
  2436
+    end
  2437
+  end
  2438
+
  2439
+  @doc """
2352 2440
   Returns the atom whose text representation is
2353 2441
   `some_binary` in UTF8 encoding.
2354  
-  Allowed in guard clauses.
2355 2442
 
2356 2443
   ## Examples
2357 2444
 
@@ -2366,7 +2453,6 @@ defmodule Kernel do
2366 2453
 
2367 2454
   @doc """
2368 2455
   Works like `binary_to_atom` but the atom must exist.
2369  
-  Allowed in guard clauses.
2370 2456
 
2371 2457
   ## Examples
2372 2458
 
@@ -2382,7 +2468,7 @@ defmodule Kernel do
2382 2468
 
2383 2469
   @doc """
2384 2470
   Returns a binary which corresponds to the text representation
2385  
-  of `some_atom` in UTF8 encoding. Allowed in guard clauses.
  2471
+  of `some_atom` in UTF8 encoding.
2386 2472
 
2387 2473
   ## Examples
2388 2474
 
@@ -2396,7 +2482,7 @@ defmodule Kernel do
2396 2482
   end
2397 2483
 
2398 2484
   @doc """
2399  
-  Concatenates two binaries. Allowed in guard clauses.
  2485
+  Concatenates two binaries.
2400 2486
 
2401 2487
   ## Examples
2402 2488
 
26  lib/elixir/test/elixir/kernel_test.exs
@@ -30,6 +30,32 @@ defmodule KernelTest do
30 30
   defmodule Conversions do
31 31
     use ExUnit.Case, async: true
32 32
 
  33
+    test :binary_to_integer do
  34
+      assert binary_to_integer("123") == 123
  35
+    end
  36
+
  37
+    test :binary_to_integer_with_base do
  38
+      assert binary_to_integer("755", 8) == 493
  39
+      assert binary_to_integer("3FF", 16) == 1023
  40
+    end
  41
+
  42
+    test :binary_to_float do
  43
+      assert binary_to_float("2.2017764e+0") == 2.2017764
  44
+    end
  45
+
  46
+    test :integer_to_binary do
  47
+      assert integer_to_binary(77) == "77"
  48
+    end
  49
+
  50
+    test :integer_to_binary_with_base do
  51
+      assert integer_to_binary(493, 8) == "755"
  52
+      assert integer_to_binary(1023, 16) == "3FF"
  53
+    end
  54
+
  55
+    test :float_to_binary do
  56
+      assert float_to_binary(7.0) == "7.00000000000000000000e+00"
  57
+    end
  58
+
33 59
     test :atom_to_binary_defaults_to_utf8 do
34 60
       expected  = atom_to_binary :some_binary, :utf8
35 61
       actual    = atom_to_binary :some_binary

0 notes on commit 17b5ede

Please sign in to comment.
Something went wrong with that request. Please try again.