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 1928690
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:00:36+00:00 2026-05-17T07:00:36+00:00

For a fun project I’m trying to implement the BitTorrent spec, and right now

  • 0

For a fun project I’m trying to implement the BitTorrent spec, and right now I’m working on the BEncoding portion of it.

The encoding basically can encode from int/string/dictionary -> string for transmission. I’ve got all of the different encodings written/tested/working as overloaded Encode(…) methods and I’ve got the individual decode methods written/tested/working as DecodeString(…), DecodeInt(…) etc.

I can’t figure out a way to have 1 Decode method for all decodings, in order to keep the API for the encoding/decoding as clean as possible (2 public methods, tops, for the time being).

Note that I have a method that can get the type of result that the decoded string will have.

Client code, right now would have to look something like this every time they want to decode a message:

string s = ...; // Encoded string
Type t = Encoder.GetDecodedType(s);
if (t == typeof(int))
    process(Encoder.DecodeInt(s));
else if (t == typeof(string))
    process(Encoder.DecodeString(s));
else if (t == typeof(Dictionary<string, string>))
    process(Encoder.DecodeStringDictionary(s));
else if (t == typeof(Dictionary<string, int>))
    process(Encoder.DecodeIntDictionary(s)):

and I’d like to be able to clean that up to be more like:

string s = ...; // Encoded string
process(Encoder.Decode(s));

where, in both cases the process(…) would likely be overloaded functions at the client end taking the 4 types of decoded values.

  • 1 1 Answer
  • 2 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-05-17T07:00:36+00:00Added an answer on May 17, 2026 at 7:00 am

    You could let the DLR do this for you.

    public static void Process(int i) { ... }
    public static void Process(string s) { ... }
    public static void Process(Dictionary<string, string> dic) { ... }
    public static void Process(Dictionary<string, int> dic) { ... }
    
    [...]
    
    public dynamic Decode(string input)     // or 'object' if you prefer
    {
        var t = GetDecodedType(input);
        if (t == typeof(int))
            return DecodeInt(input);
        else if (t == ...)
            // ...
    }
    
    [...]
    
    string s = ...; // Encoded string
    Process(Encoder.Decode(s));            // if you used 'dynamic' above
    Process((dynamic)Encoder.Decode(s));   // if you used 'object' above
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a little HTML/JavaScript/CSS3 project for fun. I'm basically trying to
I'm working on a little project for fun and ran into some trouble trying
I'm working on a for-fun-project that I will be doing some calculations with and
I am currently working on Project Euler for fun, and use Haskell for practicing.
I'm working with the C# managed wiimote library for a little fun project I'm
I am currently working on a fun project, which involves Ajax, HTML, JavaScript, PHP
I'm working on a fun project where I need a simple key/value store that
I'm an intermediate C#/ASP.net coder working on a fun MVC project. I'd like to
I'm trying to write a brainfuck interpreter in Haskell as an exercise/fun project, and
I'm working on a project for fun and I need an algorithm to do

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.