English 中文(简体)
我怎么能具体说明通过变量进行借方的类型。
原标题:How can I specify the type of a bit string through a variable
  • 时间:2010-01-17 14:58:52
  •  标签:
  • erlang

让我们:

Type = bitstring. 
<<FirstPart:8/Type, Rest/bitstring>> = some_binary.

尽管我允许:

Size = 8. 
<<FirstPart:Size/bitstring, Rest/bitstring>> = some_binary.

因此,在缩略语中,虽然我可以通过一个变数通过大小,但我却想通过一个变数通过这一类型。 是否有解决办法?

最佳回答

你可以把案件陈述作为工作:

{FirstPart, Rest} = case Type of
                        { bitstring , Len} ->
                            <<A:Len/bitstring, B/bitstring>> = Bin,
                            {A,B};
                         integer  ->
                            <<A/integer, B/bitstring>> = Bin,
                            {A,B};
                        ...
问题回答

我在这里看不出任何非常的情况,使得处理不同的编码“类型”的条件不方便。

case StringType of
  byte_len ->
    <<Len:8, String:Len/binary>> = SomeBinary,
    String;
  word_len ->
    <<Len:32, String:Len/binary>> = SomeBinary,
    String;
  etc -> ...
end




相关问题
How big can Erlang DETS be and what to do if its too small?

All I need is a large persistent lookup table in Erlang and dets seems like just the thing though I need a definative answer to: just how big the total size of the binaries in the table can be. how ...

passing events from erlang to Clojure

I m looking for a way to pass events back and forth between Clojure and erlang. has someone done this before? how should I encode the (immutable) messages in a flaxable general way? Should IPC be ...

How to send a push notification using Erlang?

I m trying to send a push notification to APNs using Erlang. This is the code I came up with so far: -module(apnstest2). -export([connect/0]). connect() -> application:start(ssl), ssl:...

How do I build a DNS Query record in Erlang?

I am building a native Bonjour / Zeroconf library and need to build DNS query records to broadcast off to the other machines. I have tried looking thru the Erlang source code but as I am relatively ...

AccessViolation when calling unmanaged dll

When calling an unmanaged Dll from a c# application I get an AccessViolationException. The strange thing is that the exported function has no arguments, so the problem is not in the Marshalling of ...

How to enable active sockets in a Mochiweb application?

Does anyone know how to enable active instead of passive sockets in a Mochiweb application. Specifically, I am trying to adapt http://www.metabrew.com/article/a-million-user-comet-application-with-...

How to convert numbers to words in Erlang?

I found this interesting question about converting numbers into "words": Code Golf: Number to Words I would really like to see how you would implement this efficiently in Erlang.

热门标签