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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:04:08+00:00 2026-06-01T18:04:08+00:00

I have a hex string which looks like: String hexImage =0xFFD8FFE000104A46494600010200006400640000FFEC00114475636B79000100040000003C… Which I need

  • 0

I have a hex string which looks like:

String hexImage ="0xFFD8FFE000104A46494600010200006400640000FFEC00114475636B79000100040000003C..."

Which I need to convert to an image using Java. I’ve tried converting to a byte array first with org.apache.commons.codec.binary.Hex class. FileOutputStream later dumps this to a file.

char[] charArr= hexImage.toCharArray();
byte[] byteArray = Hex.decodeHex(charArr);

however the parser in the Hex class chokes, with an Decoder exception : Odd number of characters. No matter what method I use to convert to a byte array, it fails. The fileformat is definitely a JPEG.

I’ve tried removing this 0xFF from the string, but the image is corrupt at this point. 0xFF I assume is a the culprit – anyone any ideas on what do I need to do to correct this?

  • 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-01T18:04:09+00:00Added an answer on June 1, 2026 at 6:04 pm

    Normally, each byte is represented by 2 hex digits, therefore, if you have an odd number of digits in your HEX string, then something is wrong with it. You can try padding it with 0 in the beginning, such as this:

    String hexImage ="0xFFD8FFE000104A46494600010200006400640000FFEC00114475636B79000100040000003C...";
    if(hexImage.length()%2 == 1)
        hexImage = "0x0" + hexImage.substring(2);
    

    or at the end, such as this:

    String hexImage ="0xFFD8FFE000104A46494600010200006400640000FFEC00114475636B79000100040000003C...";
    if(hexImage.length()%2 == 1)
        hexImage += "0";
    

    However this is not guaranteed to produce a proper image.

    On the whole, you should check how you get your hex string. A proper byte sequence should always contain an even number of hex digits.

    EDIT: In addition, as Peter Lawrey indicated in his comment, you should check whether the decode method expects 0x in front of the string.

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

Sidebar

Related Questions

I have a string which is formatted like this: $20,$40,$AA,$FF. Basically, hex numbers and
I have a string 'RAJA' which should be written into a file as HEX
I have a NSString with string like hello. Now I want to convert the
I have a string of Hex values... String hexString = 8A65; I need to
I am trying to convert the input ascii string to hex format using the
This answer to what looks like the same question: Convert integer to hex and
I have a string representation of a MD5 hex digest for a file, that
I have two strings String s1=426F62; String s2=457665; The strings are in hex representation.
Let's say that I have a string 5a . This is the hex representation
I have a binary file that I would like to regex search/replace hex bytes

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.