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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:22:09+00:00 2026-06-04T09:22:09+00:00

The question is in comments in the code , I thought that’d be an

  • 0

The question is in comments in the code, I thought that’d be an easier way to ask…

Easy question, but I can’t seem to find an answer. I want to convert a String to it’s byte[] (easy, String.getBytes()). Then I want to convert a String of bytes (101011010101001 for example) to a byte[] and get the String value of that (that’s easy too: new String(byte[]))

Here’s what I’ve got so far:

Scanner scan = new Scanner(System.in);
String string = scan.nextLine();
String byteString = "";
for (byte b : string.getBytes()) {
  byteString += b;
}
System.out.println(byteString);

//This isn't exactly how it works, these two parts in separate methods, but you get the idea...

String byteString = scan.nextLine();
byte[] bytes = byteString.literalToBytes() //<== or something like that...
//The line above is pretty much all I need...
String string = new String(bytes);
System.out.println(string);
  • 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-04T09:22:11+00:00Added an answer on June 4, 2026 at 9:22 am

    Alright, because the commenter who pointed me to this question (which lead me to this answer) isn’t going to answer, I’ll just post the solution here:

    Scanner scan = new Scanner(System.in);
    String pass = scan.nextLine();
    StringBuilder byteString = new StringBuilder();
    for (byte b : pass.getBytes()) {
      b = (byte) (b);
      byteString.append(b).append(","); //appending that comma is what does the trick.
    }
    System.out.println(byteString);
    //
    String[] split = byteString.toString().split(","); //splitting by that comma is what does the trick... too...
    byte[] bytes = new byte[split.length];
    for (int i = 0; i < split.length; i++) {
      bytes[i] = (byte) (Byte.valueOf(split[i]).byteValue());
    }
    System.out.println(new String(bytes));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question that has been viewed, but with no answers or comments
QUESTION ANSWERED IN COMMENTS Because of my reputation, I can't answer it in the
This question arose from the discussion in the comments of this answer . First,
Well, I tried to ask this question as a comment on this question, but
there were a few question similar to mine on the stack but none that
I asked similar question before and got some nice code, but i'm concerned with
( This question about refactoring F# code got me one down vote, but also
This may be a inappropriate question for SO, but I thought lets see :)
Question: why can't it find -lGL? Info: I wrote a program as guided by
This was originally a question I wanted to ask, but while researching the details

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.