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

  • Home
  • SEARCH
  • 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
  • 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-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

For fun I am trying to see how far I can get at implementing
I'm working on a fun project where I need a simple key/value store that
I would like to start learning ASP.NET on a for-fun project. I'm hoping to
Microsoft Visual Studio (2005 and 2008) seems to have fun shuffling the Project IDs
I'm doing a personal, just for fun, project that is using screen scraping to
I'm having some fun trying to get my head around some MVP stuf, as
In my experience Meta-templates are really fun (when your compilers are compliant), and can
For a fun project I wanted to make a binary clock in PHP, so
Am learning C and thought that Project Euler problems would be a fun and
Got new project in my TODO and can't chose F# or Nemerle. I'm currently

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.