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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:52:30+00:00 2026-06-18T12:52:30+00:00

I want to convert a string into hex, then into a byte array. Here

  • 0

I want to convert a string into hex, then into a byte array. Here is my code so far:

public static void findKey(){

    Cipher cipher;
    SecretKeySpec key;
    byte [] keyBytes;
    byte [] pt;
    byte [] ct;

    String plaintext = "Plaintxt";
    ct = new byte [] {(byte)0x4A, (byte)0xC4, (byte)0x55, (byte)0x3D, (byte)0xB3, (byte)0x37, (byte)0xCA, (byte)0xB3};
    String ptHex = asciiToHex(plaintext);
    System.out.println(ptHex);
    pt = ptHex.getBytes();
    printByteArray(pt);
}

My method to convert to hex works fine, but when I use getBytes, it obviously turns it to 16 bytes which is not what I want. That was just an attempt. Here is the output from just printing my string to make sure it worked and then printing the byte array which is incorrect:

506c61696e747874
[  35  30  36  63  36  31  36  39  36  65  37  34  37  38  37  34  ]
-------------Key  Found-------------

I want to take the 50, the 6c, the 61 etc., and put it into a byte array like I did for ct like 0x50, 0x6c, and so on.

Is this even possible?

  • 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-18T12:52:31+00:00Added an answer on June 18, 2026 at 12:52 pm

    try

    byte[] getBytes(String s) {
        byte[] a = new byte[s.length() / 2];
        for (int i = 0; i < a.length; i++) {
            a[i] = Byte.parseByte(s.substring(i * 2, i * 2 + 2), 16);
        }
        return a;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a string into a byte array. (Yes) I have seen
I want to convert a C-style string into a byte-vector. A working solution would
i want to convert string into array when comma or semicolon is encountered. for
I want to build a function to easily convert a string containing hex code
I want to convert a string into an array of bytes. When I try
I have a StringBuffer, strbuf=161656070200000000202020202001000000009E52; . I want to convert into a byte array
i want to convert the string into nsdate format. here is my string NSString
Question: I need to convert a string into hex and then format the hex
i want to convert this string into DateTime. Tue Aug 19 15:05:05 +0000 2008
I want to convert all texts in a string into html entities but preserving

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.