Erlang 进制转换
http://www.cnblogs.com/me-sa/archive/2012/03/20/erlang0047.html
| bnot | unary bitwise not | integer |
| div | integer division | integer |
| rem | integer remainder of X/Y | integer |
| band | bitwise and | integer |
| bor | bitwise or | integer |
| bxor | arithmetic bitwise xor | integer |
| bsl | arithmetic bitshift left | integer |
| bsr | bitshift right | integer |
如果人类像卡通人物那样,每只手上只有 4个手指会怎样呢?我们可能永远都不会想到要发明一种以10为基础的数字系统的问题, 取而代之的是我们可能会认为数字系统基于 8是正常、自然、合理、必然的,是毫无疑问的,是非常合适的。这时,就不能称之为十进制了,得将它称作为以8为基础的数字系统或八进制。... ...龙虾根本没有手指,但它两只前爪的末端都有螯。适合于龙虾的数字系统是四进制数字系统或称为基于4的数字系统.
Convert.ToInt32 Method (String, Int32)The base of the number in value, which must be 2, 8, 10, or 16.
Converts the string representation of a number in a specified base to an equivalent 32-bit signed integer.
Erlang

Eshell V5.9 (abort with ^G)
1> 2#1011.
11
2> 2#1111.
15
3> 16#12.
18
4> 16#1F.
31
5> 8#121.
81
6> 7#11.
8
7>


Eshell V5.9 (abort with ^G)
1> integer_to_list(1234,8).
"2322"
2> integer_to_list(1234,16).
"4D2"
3> list_to_integer("2322",8).
1234
4> list_to_integer("4d2",16).
1234

integer_to_list(Integer, Base) -> string()
Types:
Returns a string which corresponds to the text representation of Integer in base Base.
list_to_integer(String, Base) -> integer()
Types:
群里面有人问如何把"abc"转换成 "110000111000101100011"的形式,即逐字符转成二进制,不管他转成这样的格式做什么用,实现起来还是比较简单的:lists:concat([erlang:integer_to_list(Item,2) || Item<-"abc" ]).

%%Code Path:\erl5.9\lib\erts-5.9\src\erlang.erl
integer_to_list(I0, Base, R0) ->
D = I0 rem Base,
I1 = I0 div Base,
R1 = if D >= 10 ->
[D-10+$A|R0];
true ->
[D+$0|R0]
end,
if I1 =:= 0 ->
R1;
true ->
integer_to_list(I1, Base, R1)
end.


%% @spec hexdigit(integer()) -> char()
%% @doc Convert an integer less than 16 to a hex digit.
hexdigit(C) when C >= 0, C =< 9 ->
C + $0;
hexdigit(C) when C =< 15 ->
C + $a - 10. to_hex_int(0, Acc) ->
Acc;
to_hex_int(I, Acc) ->
to_hex_int(I bsr 4, [hexdigit(I band 15) | Acc]).


de2bin(0,Acc)->
Acc;
de2bin(N,Acc) ->
de2bin(N bsr 1,[((N band 1)+$0)|Acc]). de2oct(0,Acc) ->
Acc;
de2oct(N,Acc) ->
de2oct(N bsr 3 ,[((N band 7)+$0)|Acc]).


Eshell V5.9 (abort with ^G)
1> zen:de2oct(1234,[]).
"2322"
2> 8#2322.
1234
3> zen:de2oct(1,[]).
"1"
4> zen:de2bin(1234,[]).
"10011010010"
5> list_to_integer(zen:de2oct(1234,[]),8).
1234
6>


%% @author Bob Ippolito <bob@mochimedia.com>
%% @copyright 2006 Mochi Media, Inc. %% @doc Utilities for working with hexadecimal strings. -module(mochihex).
-author('bob@mochimedia.com'). -export([test/0, to_hex/1, to_bin/1, to_int/1, dehex/1, hexdigit/1]). %% @type iolist() = [char() | binary() | iolist()]
%% @type iodata() = iolist() | binary() %% @spec to_hex(integer | iolist()) -> string()
%% @doc Convert an iolist to a hexadecimal string.
to_hex(0) ->
"0";
to_hex(I) when is_integer(I), I > 0 ->
to_hex_int(I, []);
to_hex(B) ->
to_hex(iolist_to_binary(B), []). to_hex(<<>>, Acc) ->
lists:reverse(Acc);
to_hex(<<C1:4, C2:4, Rest/binary>>, Acc) ->
to_hex(Rest, [hexdigit(C2), hexdigit(C1) | Acc]). %% @spec hexdigit(integer()) -> char()
%% @doc Convert an integer less than 16 to a hex digit.
hexdigit(C) when C >= 0, C =< 9 ->
C + $0;
hexdigit(C) when C =< 15 ->
C + $a - 10. to_hex_int(0, Acc) ->
Acc;
to_hex_int(I, Acc) ->
to_hex_int(I bsr 4, [hexdigit(I band 15) | Acc]). %% @spec to_bin(string()) -> binary()
%% @doc Convert a hexadecimal string to a binary.
to_bin(L) ->
to_bin(L, []). to_bin([], Acc) ->
iolist_to_binary(lists:reverse(Acc));
to_bin([C1, C2 | Rest], Acc) ->
to_bin(Rest, [(dehex(C1) bsl 4) bor dehex(C2) | Acc]). %% @spec dehex(char()) -> integer()
%% @doc Convert a hex digit to its integer value.
dehex(C) when C >= $0, C =< $9 ->
C - $0;
dehex(C) when C >= $a, C =< $f ->
C - $a + 10;
dehex(C) when C >= $A, C =< $F ->
C - $A + 10. %% @spec to_int(string()) -> integer()
%% @doc Convert a hexadecimal string to an integer.
to_int(L) ->
erlang:list_to_integer(L, 16). %% @spec test() -> ok
%% @doc Test this module.
test() ->
"ff000ff1" = to_hex([255, 0, 15, 241]),
<<255, 0, 15, 241>> = to_bin("ff000ff1"),
16#ff000ff1 = to_int("ff000ff1"),
"ff000ff1" = to_hex(16#ff000ff1),
ok.


hexdigit(C) when C < 10 -> $0 + C;
hexdigit(C) when C < 16 -> $A + (C - 10). unhexdigit(C) when C >= $0, C =< $9 -> C - $0;
unhexdigit(C) when C >= $a, C =< $f -> C - $a + 10;
unhexdigit(C) when C >= $A, C =< $F -> C - $A + 10. quote_plus([], Acc) ->
lists:reverse(Acc);
quote_plus([C | Rest], Acc) when ?QS_SAFE(C) ->
quote_plus(Rest, [C | Acc]);
quote_plus([$\s | Rest], Acc) ->
quote_plus(Rest, [$+ | Acc]);
quote_plus([C | Rest], Acc) ->
<<Hi:4, Lo:4>> = <<C>>,
quote_plus(Rest, [hexdigit(Lo), hexdigit(Hi), ?PERCENT | Acc]). qs_revdecode(S) ->
qs_revdecode(S, []). qs_revdecode([], Acc) ->
Acc;
qs_revdecode([$+ | Rest], Acc) ->
qs_revdecode(Rest, [$\s | Acc]);
qs_revdecode([Lo, Hi, ?PERCENT | Rest], Acc) when ?IS_HEX(Lo), ?IS_HEX(Hi) ->
qs_revdecode(Rest, [(unhexdigit(Lo) bor (unhexdigit(Hi) bsl 4)) | Acc]);
qs_revdecode([C | Rest], Acc) ->
qs_revdecode(Rest, [C | Acc]).

IP地址和整形互转

4> integer_to_list(16711680,2).
"11111111,00000000,00000000"
6> integer_to_list(65280,2).
"1111111100000000"
7> integer_to_list(16777216,2).
"1000000000000000000000000"
8> integer_to_list(65536,2).
"10000000000000000"
9> integer_to_list(256,2).
"100000000"


10> Ip_to_integer=fun({A,B,C,D}) -> (A*16777216)+(B*65536)+(C*256)+D end.
#Fun<erl_eval.6.111823515>
11> Ip_to_integer({192,168,0,188}).
3232235708
12> Integer_to_ip=fun(Ip)-> {Ip bsr 24, (Ip band 16711680) bsr 16, (Ip band 65280) bsr 8, Ip band 255} end.
#Fun<erl_eval.6.111823515>
14> Integer_to_ip(3232235708).
{192,168,0,188}

附:
| bnot | unary bitwise not | integer |
| div | integer division | integer |
| rem | integer remainder of X/Y | integer |
| band | bitwise and | integer |
| bor | bitwise or | integer |
| bxor | arithmetic bitwise xor | integer |
| bsl | arithmetic bitshift left | integer |
| bsr | bitshift right | integer |
sort([Pivot|T]) ->
sort([ X || X <- T, X < Pivot]) ++
[Pivot] ++
sort([ X || X <- T, X >= Pivot]);
sort([]) -> [].
perms([]) -> [[]];
perms(L) -> [[H|T] || H <- L, T <- perms(L--[H])].

pyth(N) ->
[ {A,B,C} ||
A <- lists:seq(1,N),
B <- lists:seq(1,N),
C <- lists:seq(1,N),
A+B+C =< N,
A*A+B*B == C*C
]. %%优化版本
pyth1(N) ->
[{A,B,C} ||
A <- lists:seq(1,N-2),
B <- lists:seq(A+1,N-1),
C <- lists:seq(B+1,N),
A+B+C =< N,
A*A+B*B == C*C ].


12> [io:format("abc")|| _<-lists:seq(1,10)].
abcabcabcabcabcabcabcabcabcabc[ok,ok,ok,ok,ok,ok,ok,ok,ok,ok]
13> E=34.
34
14> [io:format("abc")|| E>35].
[]
15> [io:format("abc")|| E>32].
abc[ok]
16>

Erlang 进制转换的更多相关文章
- SQL Server 进制转换函数
一.背景 前段时间群里的朋友问了一个问题:“在查询时增加一个递增序列,如:0x00000001,即每一个都是36进位(0—9,A--Z),0x0000000Z后面将是0x00000010,生成一个像下 ...
- [No000071]C# 进制转换(二进制、十六进制、十进制互转)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- JS中的进制转换以及作用
js的进制转换, 分为2进制,8进制,10进制,16进制之间的相互转换, 我们直接利用 对象.toString()即可实现: //10进制转为16进制 ().toString() // =>&q ...
- 结合stack数据结构,实现不同进制转换的算法
#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...
- 进制转换( C++字符数组 )
注: 较为简便的方法是用 整型(int)或浮点型(long.double 注意:该类型不一定能够准确存储数据) 来存放待转换的数值,可直接取余得到每一位数值 较为稳定的方法是用 字符数组储存待转换的数 ...
- JS 进制转换
十进制转换成其他进制 objectname.toString([radix]) objectname 必选项.要得到字符串表示的对象. radix 可选项.指定将数字值转换为字符串时的进制. 例如 ...
- php的进制转换
学习了php的进制转换,有很多的知识点,逻辑,也有最原始的笔算,但是我们还是习惯使用代码来实现进制的转换,进制的转换代码有如下:二进制(bin)八进制( oct)十进制( dec)十六进制( hex) ...
- C++ 中数串互转、进制转换的类
/******************************************************************** created: 2014/03/16 22:56 file ...
- 【String与基本类型之间的转换】以及【进制转换】
1. 基本数据类型---->字符串类型: 方法一:使用连接一个空字符串,例如 基本数据类型+“” : 方法二:静态方法 String.valueOf(),具体有: String.valueOf ...
随机推荐
- RFID的基本组织构成
RFID技术由3大组件构成, 包括: 阅读器.天线.标签三大组件. 阅读器 为RFID系统最重要也是最复杂的一个组件.因其工作模式一般是主动向标签询问标识信息, 所以有时又被称为询问器(Interro ...
- 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给 ...
- 数学定理证明机械化的中国学派(II)
所谓"学派"是指:存在一帮人,具有同样或接近的学术观点或学术立场,採用某种特定的"方法"(或途径),在一个学术方向上共同开展工作.而且做出了相当有迎影响的学术成 ...
- 看好腾讯,鄙视百度(腾讯的核心竞争力,不是超过10亿的QQ的注册用户,也不是某一项产品、技术方面优势,而是“耐心”:懂得在合适的时间推出合适的产品。”)
百度,自始至终只是一个低劣的模仿者,且一切向前看,完全违背了一个搜索引擎所应该遵循的基本原则.谁给的钱多就能搜着谁,这跟贩毒有什么区别? 腾讯也在模仿别人,但是,它是模仿然后超越.在中国互联网发展历史 ...
- progerssbar-style 属性分析
先看如下代码 <ProgressBar android:id="@+id/stateProgressBar" android:orientation="horizo ...
- 关于React中,map出来的元素添加事件问题
用es6 map 的写法 直接绑定一个onTouchStart 事件不会报错. 用es5的map写法 如果不加上this 会报这个错误 无法读取未定义的属性 解决的方法是 绑定this 就可以了
- datetime小练习
题目: 1.计算你的生日比如近30年来(1990-2019),每年的生日是星期几,统计一下星期几出现的次数比较多2,生日提醒,距离生日还有几天 # !/usr/bin/env python # -*- ...
- 小贝_redis web管理界面工具安装
RedisWEB管理界面工具安装 一.概述 二.文件下载 三.安装过程 一.概述 1.因为redis是基于C/S的方式开发.也就是说,仅仅要满足于redis的client通信要求的,都能够作为redi ...
- StackExchange.Redis 官方文档(六) PipelinesMultiplexers
原文:StackExchange.Redis 官方文档(六) PipelinesMultiplexers 流水线和复用 糟糕的时间浪费.现代的计算机以惊人的速度产生大量的数据,而且高速网络通道(通常在 ...
- 使用SystemC进行硬件仿真
使用SystemC进行硬件仿真 环境 linux-x86-64 bash g++ 下载解压SystemC SystemC下载地址 解压下载的包 tar zxvf systemc-2.3.3.tar.g ...
