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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:37:38+00:00 2026-05-15T15:37:38+00:00

I need to write an integer to a byte array such that leading zeros

  • 0

I need to write an integer to a byte array such that leading zeros are omitted and the bytes are written in big endian order.

Example:

int    original = 0x00123456;

byte[] encoded  = Encode(original);  //  == new byte[] { 0x12, 0x34, 0x56 };

int    decoded  = Decode(encoded);   //  == 0x123456

My Decode method:

private static int Decode(byte[] buffer, int index, int length)
{
    int result = 0;
    while (length > 0)
    {
        result = (result << 8) | buffer[index];
        index++;
        length--;
    }
    return result;
}

I’m struggling to come up with an Encode method that doesn’t require a temporary buffer or reverses the bytes after writing them in little endian order. Can anyone help?

private static int Encode(int value, byte[] buffer, int index)
{

}
  • 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-15T15:37:39+00:00Added an answer on May 15, 2026 at 3:37 pm

    As per OP’s request, here is a version without loops for a 32-bit number:

    private static int Encode(int value, byte[] buffer, int index)
    {
        byte temp;
        bool leading = true;
    
        temp = (value >> 24) & 0xFF;
        if (temp > 0) {
          buffer[index++] = temp;
          leading = false;
        }
    
        temp = (value >> 16) & 0xFF;
        if (temp > 0 || leading == false) {
          buffer[index++] = temp;
          leading = false;
        }
    
        temp = (value >> 8) & 0xFF;
        if (temp > 0 || leading == false) {
          buffer[index++] = temp;
          leading = false;
        }
    
        temp = value & 0xFF;
        buffer[index++] = temp;
    
        return index;
    }
    

    Version using a loop for 32-bit numbers:

    private static int Encode(int value, byte[] buffer, int index)
    {
        int length = 0;
    
        for (int i = 3; i >= 0; i++) {
          byte temp = (byte)(value >> (8 * i));
          if (temp > 0 || length > 0) {
            buffer[index++] = temp;
            length++;
          }
        }
    
        return length;
    }
    

    Note that this version doesn’t write anything if the input is just 0.

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

Sidebar

Related Questions

I am new to Prolog. I need to write an integer adder that will
I need write a set of clauses that take a list of integer lists
I need to write Domain class constraint in Grails which says that one integer
I need to write an algorithm that takes an integer and returns all possible
Possible Duplicate: using bash: write bit representation of integer to file I need to
I need to write a C++ code coverage program that takes in another C++
I need to write a program that prints 0.(03) for input 1 and 33.
I need to write a method that prints a binary tree by using recursion.
I need to write a method that returns the day of the month as
I need to write a function that returns the first perfect square that is

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.