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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:17:56+00:00 2026-06-16T13:17:56+00:00

I have a function that is supposed to read a hex number, but it

  • 0

I have a function that is supposed to read a hex number, but it is not reading it correctly. The polynomial function is reading the string as ASCII instead of hex.

Here is the portion of code that is doing the work:

JButton button = new JButton("Calculate");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str = textArea.getText();

                      int crc = 0xFFFF;        
                     int polynomial = 0x1021;  

                        byte bytes[] = str.getBytes();
                     for (byte b : bytes) {
                         for (int i = 0; i < 8; i++) {
                             boolean bit = ((b   >> (7-i) & 1) == 1);
                             boolean c15 = ((crc >> 15    & 1) == 1);
                             crc <<= 1;
                             if (c15 ^ bit) crc ^= polynomial;
                          }
                     }
                     crc &= 0xFFFF;
                textField.setText(""+Integer.toHexString(crc));
            }
        });
        button.setBounds(10, 245, 90, 25);
        panel.add(button);
  • 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-16T13:17:57+00:00Added an answer on June 16, 2026 at 1:17 pm

    String.getBytes gives you the characters with the default character encoding. It is not recomended evenm if you want to encode a String as bytes (it is suggest you provide the encoding you want)

    In this pase you want to parse your hexidecimal string into bytes. A simple way to do this is to use BigInteger.

    String hex = "CAFEBABE";
    byte[] bytes = new BigInteger(hex, 16).toByteArray();
    if (bytes.length > 0 && bytes[0] == 0)
        bytes = Arrays.copyOfRange(bytes, 1, bytes.length);
    System.out.println(Arrays.toString(bytes));
    

    prints

    [-54, -2, -70, -66]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two functions that are supposed to encrypt and decrypt a string but
I have a C++ File class with read function, that is supposed to read
I have a function that is supposed to read from a file into a
I have this function that iterates through a directory and is supposed to read
I have the following function that is supposed to trigger anytime one of the
I have a function in a powershell script that is supposed to untar my
I have a function that reads an input file and is supposed to modify
I have the following function which is supposed to read a .html file and
I have a script that is supposed to read from the database and return
I have an assignment that is supposed to be written in C (not C++),

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.