简体   繁体   中英

Erlang: Error in converting back from base 2 to decimal using # notation

To get the decimal equivalent of a number N in base B, I can do B#N , so 16#F outputs 15 , and 2#1111 outputs 15 too.

To convert a number (say 15) from decimal to binary, I can do integer_to_list(15,2) which outputs "1111" , which can be made into a number by doing list_to_integer . So, doing list_to_integer(integer_to_list(15,2)) + 1. is perfectly valid and outputs 1112 .

I check that is_integer and is_number checks out to true for list_to_integer(integer_to_list(15,2)) and I am also able to convert back doing list_to_integer(integer_to_list(15,2), 2) and get back 15 .

But when I try to do 2#list_to_integer(integer_to_list(15,2)) , why does it give me an error *1: illegal integer instead of 15 ?

Same thing here:

2> Name = "Yogesch".
"Yogesch"

3> "Name".
"Name"
4> 

8> "list_to_integer(integer_to_list(10))".
"list_to_integer(integer_to_list(10))"

Every computer programming language has syntax rules. The syntax 2#10101 is for integer literals . Notably, 2# is not the name of a function, so you can't write 2#("1010") nor:

10> X = "1010".
"1010"

11> 2#(X).
* 1: illegal integer

2# and a space are the delimiters for an integer literal just like quotes are the delimiters for a string (which is then interpreted as a list of integers).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM