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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:05:55+00:00 2026-06-17T09:05:55+00:00

Possible Duplicate: Java integer to byte array Here’s another question still related to the

  • 0

Possible Duplicate:
Java integer to byte array

Here’s another question still related to the original below:

If I have a hex number that takes up 4 characters (2-bytes) saved as an integer.
Example:

int value = 0xFFFF;

This in java will make it a 4-byte hex with 2 bytes being completely empty.

How am I able to grab only the two bytes that have data?

Help is greatly appreciated! 🙂

Answer to my second question:

Better answer posted below by md_5.

So to get the SINGLE byte that has data you can simply do: byte single
= (byte) i & 0xFF If you had a bigger number up to 65535 (0xFFFF) you can do:

byte b1 = (byte) ((i >> 8) & 0xFF); 
byte b2 = (byte) i & 0xFF 

If you had an even larger number in the integer range you just do b1 =
i >> 24, b2 = i >> 16, etc etc etc. Subtracting 8 from the shift as
that is the size of a byte.

An alternate method uses the answer to the original question:

int hex = 0xF15E;
byte[] bytes = ByteBuffer.allocate(4).putInt(hex).array();
for(int i = 0; i < 4; i++) {
    System.out.format("0x%X ", bytes[i]);
}

Output:

0x0 0x0 0xF1 0x5E

So then take the important bytes:

byte[] importantBytes = {bytes[2], bytes[3]};

Original Question:

This question may be obsolete due to being able to converting an Integer into a byte[] but here goes:

How does one convert an integer into a hexidecimal that has leading 0’s?

So if I have the integer: 4096, in hex would be 00001000.

The overall purpose was to then convert this formatted hex string into a byte[] (which would then be saved), but I think I can do this by the means above.

But still… How do we convert this integer into a formatted hex string? Is this even practical?

The language of preference is Java, and I looked at Integer.toHexString().

Working solution for what I originally needed:

int value = 2147483647;
byte[] bytes = ByteBuffer.allocate(4).putInt(value).array();

for(int i = 0; i < 4; i++)
System.out.format("0x%X ", bytes[i]);
  • 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-17T09:05:56+00:00Added an answer on June 17, 2026 at 9:05 am

    In regards to your new question (getting the bytes that have data), you are wrong in saying that it takes up 2 bytes, 0xFF can in fact be packed into one byte.
    Long/Double – 8 bytes
    Integer/Float – 4 bytes
    Short – 2 bytes
    Byte – 1 byte

    So to get the SINGLE byte that has data you can simply do:
    byte single = (byte) i & 0xFF
    If you had a bigger number up to 65535 (0xFFFF) you can do:
    byte b1 = (byte) ((i >> 8) & 0xFF); byte b2 = (byte) i & 0xFF
    If you had an even larger number in the integer range you just do b1 = i >> 24, b2 = i >> 16, etc etc etc. Subtracting 8 from the shift as that is the size of a byte.

    Hope this answers your question.

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

Sidebar

Related Questions

Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList
Possible Duplicate: What’s the reason I can’t create generic array types in Java? HashSet<Integer>[]
Possible Duplicate: Java Ordered Map I have list of product object in the HashMap<Integer,Product>
Possible Duplicate: Java Generics: Array containing generics I have a Java class which contains
Possible Duplicate: Java question about autoboxing and object equality / identity Integer i1 =
Possible Duplicate: Java String.equals versus == I know it' a dumb question but why
Possible Duplicate: Java: recommended solution for deep cloning/copying an instance I have an object
Possible Duplicate: Java 1.6: Creating an array of List<T> How can I initialize this
Possible Duplicate: Java Integer Division, How do you produce a double? double wang =
Possible Duplicate: Java Integer division: How do you produce a double? When I'm dividing

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.