Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8692981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:26:16+00:00 2026-06-13T00:26:16+00:00

I just started my path on Erlang and I’m facing a problem I can’t

  • 0

I just started my path on Erlang and I’m facing a problem I can’t sort out a solution about:

I wrote a metod to take a domain espressed as a binary string, i.e. <<“www.404pagenotfound.com”>> and convert it in the domain format as required for DNS protocol, so: <<3,”www”,15,”pagenotfound”,3,”com”>>.

In the following the code (I rewroted it many times in different ways):

domainbyte(Bin) ->
    if byte_size(Bin) > 0 ->
        Res =  binary:split(Bin, <<".">>),
        [Chunk|[RestList]] = Res, 
        ChunkSize = byte_size(Chunk),   
        if length(RestList) > 0 -> 
            Rest = domainbyte(RestList),  %% <- Got "bad argument" here!
            <<ChunkSize/binary,Chunk,Rest>>;
        true ->
            <<ChunkSize/binary,Chunk>>
        end
    end.

Thx in advance for any clues.

PS.

Thx to comments I’ve found the error in the code abobe:

if length(RestList) > 0 -> %% here RestList is binary data so length throw "bad argument" error.

I’ve rewroted the method this way, but still with no luck:

**NOTE: I was able to fix the code in the following, problem is that if you have a binary chunk and you want to use it in another binary string, you must specify /binary on it: something not obvious to me.

I.e.: consider this small code snip:

**

TT = <<“com”>>,
SS = <<3, TT, 0>> %% <- you get error: bad argument

**
must be fixed this way:
**

TT = <<“com”>>,
SS = <<3, TT/binary, 0>>

domainbyte(Bin) ->
    if byte_size(Bin) > 0 ->
        Res =  binary:split(Bin, <<".">>),
                if length(Res) > 1 -> 
                    [Chunk|[RestList]] = Res, 
                    ChunkSize = byte_size(Chunk),
                    Rest = domainbyte(RestList),
                    <<ChunkSize,Chunk,Rest>>;
                true -> 
                    [Chunk] = Res,
                    ChunkSize = byte_size(Chunk), 
                    <<ChunkSize,Chunk>>
                end
    end.

MdP

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:26:17+00:00Added an answer on June 13, 2026 at 12:26 am

    I think the easiest solution is to define the function using a binary comprehension:

    domainbyte(Bin) ->
        Chunks = binary:split(Bin, <<".">>, [global]),      %A list of all the chunks
        << <<byte_size(C),C/binary>> || C <- Chunks >>.     %Build output binary
    

    It might be slightly faster to build the output binary as a list of segments in a separate function then put them together in an iolist_to_binary/1. Note that if a ‘.’ occurs outermost in the binary then this code will take that as an empty segment of length 0. If these should be discarded then you nee to add the option trim to binary:split/3. Note also that the size will occupy only one byte.

    @Alnitak has the separate function but builds the binary one segment at time so it is not more efficient than the binary comprehension which does the same thing.

    N.B. that if you have a binary segment Chunk/binary when constructing a binary this means that Chunk IS a binary, not that it should become one. Binaries are flat structures, think byte arrays, so everything becomes a binary. Or rather the binary.

    EDIT: Though I see I missed the 0 which should be at the end. That is left as an exercise to the reader.

    EDIT: Being in teaching mode, apart from the constructing binaries, a key to writing good Erlang code is understanding pattern matching. You use a bit but could do it more:

    domainbyte(Bin) ->
        case binary:split(Bin, <<".">>) of
            [Chunk,Rest] ->                       %There was a '.'
                RestBin = domainbyte(Rest),
                Size = byte_size(Chunk),
                <<Size,Chunk/binary,RestBin/binary>>;
            [Chunk] ->                            %This was the last chunk
                Size = byte_size(Chunk),
                <<Size,Chunk/binary,0>>           %Add terminating 0
    end.
    

    This is basically doing the same as your code but we are using pattern matching to select a clause, not only to pull apart a known structure. Pattern matching is the basic method for control, not just in case as here but also in functions and receive. This results in if being used quite sparingly.

    Enough from me for now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning Flex this week, and I cannot get an image path
Just started working with .NET and MVC(1). I'm having a problem wherein in my
I just started having a problem where Apache will automatically restart after every few
I just started writing Applets and I had some questions about the HTML applet
I just started coding a gtalk chat bot using libjingle. I'm having a problem
I have just started learning java and i have about 8 months time. My
i've just started learning winapis and c++ programming .. i was thinking about starting
I've just started using git in Vista, with my repository under /path/to/project/git repo .
just started building out a rails project from scratch after months of re-purposing of
I'm just getting started with git and am running into a problem when attempting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.