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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:27:03+00:00 2026-05-23T22:27:03+00:00

I want to encode tags that will contain a field number and a wire

  • 0

I want to encode tags that will contain a field number and a wire type for the purpose of protocol buffers. The problem that I am having now is that whenever my value for ‘tag’ is below ‘8’ fprintf writes additional values next to the correct ones. i.e. instead of 38 it prints 38c0 3. If the value of tag is 8 or higher the script works fine. Below is the code with irrelavent lines omitted.

int uint32_pack (uint8_t *fieldnumber, uint32_t value, uint8_t *out);

int main(){

uint32_t initvalue = 2;
int return_rv;
uint8_t *tag = (uint8_t *) malloc(sizeof(uint8_t));    
uint8_t *tempout= (uint8_t *) malloc(sizeof(uint32_t));
*tag = 7; //value to be encoded (won't work for values less than 8)

return_rv = uint32_pack (tag, initvalue, tempout);

free(tempout);

    }

/* === pack() === */
/* Pack an unsigned 32-bit integer in base-128 encoding, and return the number
 of bytes needed: this will be 5 or less. */

int uint32_pack (uint8_t *fieldnumber, uint32_t value, uint8_t *out)
{
  unsigned rv = 0;
  FILE *wiretypetag;
  int secondaryvalue;

  wiretypetag = fopen("wiretype.txt","w");


    //encodes wire type and the field number
    if (*fieldnumber <16){
       *fieldnumber <<= 3;
       fprintf(wiretypetag,"%x",fieldnumber[0]);
       }
    if (*fieldnumber < 32 && *fieldnumber > 15){
       *fieldnumber <<= 3;
       secondaryvalue = 0x01;
       fprintf(wiretypetag,"%x %x",fieldnumber[0],secondaryvalue);
           }
    if (*fieldnumber < 48 && *fieldnumber > 31){
        *fieldnumber += 0x10;
        *fieldnumber &= 0x1F;
        *fieldnumber <<= 3;
        secondaryvalue = 0x02;
        fprintf(wiretypetag,"%x %x",fieldnumber[0], secondaryvalue);
        }       
    if (*fieldnumber < 64 && *fieldnumber > 47){
        *fieldnumber &= 0x1F;
        *fieldnumber <<= 3;
        secondaryvalue = 0x03;
        fprintf(wiretypetag,"%x %x",fieldnumber[0], secondaryvalue);
        }       

  /* assert: value<128 */   
    out[rv++] = value;


    if (rv == 1){         
           fprintf(outfile,"%x",out[0]);
           }
    if (rv == 2){
           fprintf(outfile,"%x %x",out[0], out[1]);
           }    
    if (rv == 3){
           fprintf(outfile,"%x %x %x",out[0],out[1],out[2]);
           }
    if (rv == 4){
           fprintf(outfile,"%x %x %x %x",out[0],out[1],out[2],out[3]);
           }
    if (rv == 5){
           fprintf(outfile,"%x %x %x %x %x",out[0],out[1],out[2],out[3],out[4]);
           }

    fclose(wiretypetag);

    return rv;
}
  • 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-23T22:27:04+00:00Added an answer on May 23, 2026 at 10:27 pm

    You can simplify the code by using else at:

    //encodes wire type and the field number
    if (*fieldnumber <16){
       *fieldnumber <<= 3;
       fprintf(wiretypetag,"%d",fieldnumber[0]);
       }
    if (*fieldnumber < 32 && *fieldnumber > 15){
       *fieldnumber <<= 3;
    

    Replace with:

    //encodes wire type and the field number
    if (*fieldnumber < 16) {
       *fieldnumber <<= 3;
       fprintf(wiretypetag, "%d", fieldnumber[0]);
       }
    else if (*fieldnumber < 32) {
       *fieldnumber <<= 3;
    ...
    

    This is a common idiom and one you should use when appropriate – as now. It will improve the legibility of the code.

    That may not be the whole problem; it probably isn’t.

    Explanation of Problem

    In fact, using an else if chain corrects the code. Because the first if modifies *fieldnumber with the <<= operator, when the second condition is evaluated, *fieldnumber is no longer 7 but 56, so the condition for

    if (*fieldnumber < 64 && *fieldnumber > 47){
    

    is also executed, printing out more information.

    The else if chain will resolve that by ensuring that only one alternative is executed.


    Since you have:

    unsigned rv = 0;
    
    [...]
    /* assert: value<128 */   
    out[rv++] = value;
    
    if (rv == 1){         
           fprintf(outfile,"%x",out[0]);
           }
    

    This fprintf() should be executed, but the following ones for rv equal to 2, 3, 4, 5 should never be executed.

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

Sidebar

Related Questions

i have a long string dat can contain html tags applying htmlencode will encode
I want to encode DNS protocol header using C and create a UDP datagram.
I have user submitted tags that can be any type of (valid) UTF-8 string.
In PHP, I want to encode ampersands that have not already been encoded. I
i have a string that contains a bunch of html. i want to html-encode
I want to encode ( htmlentities ) or disable all tags except for <a>
As the title mentioned, I want to encode a Image Obj into some kind
In Java, I have a String and I want to encode it as a
In my java app I'm preventing XSS attacks. I want to encode URL and
I want to do encode the data before saving it to a database table

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.