erlang converted to Chinese url

Today when using http get method parameters have Chinese and cause an error.

For example http://abc.com/abc?arg= Chinese, use http in erlang: request method fails.

Then check the specification of the url, url ascii characters to be used, thus wrote the following methods:

lists:append([io_lib:format("%~.16B", [E]) || E <- binary_to_list(unicode:characters_to_binary("中文"))])。

Character conversion above appended to the end url on it.

Post method can refer to:

how to support chinese in http request body? erlang


Generating a random string of characters and numerals comprising:

randchar(N) ->
	List = randchar(N, []),
	lists:foldr(fun(X, Acc) ->
						case X > 95 of
							true ->
								[X] ++ Acc;
							false ->
								integer_to_list(X) ++ Acc
						end
				end, [], List).




randchar(0, Acc) ->
	Acc;
randchar(N, Acc) ->
	randchar(N - 1, [randstr() | Acc]).
randstr() ->
	case rand:uniform(9) > 5 of
		true ->
			rand:uniform(26) + 96;
		false ->
			rand:uniform(9)
	end.



Optimize it, or not good enough.

randchar(N) ->
    lists:flatten(randchar(N, [])).

randchar(0, Acc) ->
    Acc;
randchar(N, Acc) ->
    randchar(N - 1, [randstr() | Acc]).
randstr() ->
    case rand:uniform(9) > 5 of
        true ->
            [rand:uniform(26) + 96];
        false ->
            integer_to_list(rand:uniform(9))
    end.

Up in the morning, I suddenly thought, the letter is represented by the ascii code ascii code numbers Why not say then?

Test it, and she can do it, the code is as follows:

randchar(N) ->
	randchar(N, []).

randchar(0, Acc) ->
	Acc;
randchar(N, Acc) ->
	randchar(N - 1, [randstr() | Acc]).

randstr() ->
	case rand:uniform(9) > 5 of
		true ->
			rand:uniform(26) + 96;
		false ->
			rand:uniform(10) + 47
	end.

Because ascii 0 is 48, rand: uniform (10) is a minimum, the number 47 increments.

Reproduced in: https: //my.oschina.net/u/191928/blog/618642

Guess you like

Origin blog.csdn.net/weixin_34008784/article/details/91987022