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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:19:42+00:00 2026-05-30T08:19:42+00:00

I have a problem. I sent some data over a socket in string form.

  • 0

I have a problem.

I sent some data over a socket in string form. With some substringing, I managed to get the byte[] in String form [B@58596d12. Now, my question is, this is a string. How do I convert it back to a byte array after which I can use String result = new String(byteArray);?

I tried casting the String to byte[], but that’s not allowed.
Google only came up with the usual .getBytes() (which I can’t use since they already are bytes) and new String(someByteArray)

This is what’s being sent:

byte[] all = Files.readAllBytes(f.toPath());
    this.out.println(destinationOfFile + "/" + file.getName() + "*" + all);

And this is what’s being received:

private void writeToFile(String str) {
        String file = str.substring(0, str.indexOf("*"));
        String write = str.substring(str.indexOf("*")+1);

EDIT: I managed to do it in another way.
Instead of getting all the bytes from the file, I wen’t through it line by line, sending each line over the socket. In the client, I store the data in a HashMap and when it receives “Done sending File” it writes all the strings to the file.

  • 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-05-30T08:19:43+00:00Added an answer on May 30, 2026 at 8:19 am

    I’m using this code to convert String-Byte[], but I’m not sure if it will help you because I don’t know if you have rawBytes as source or a Java-String. Could you clarify?

    public static String byteToHexString(byte b) {
        String result = "";
        result += Integer.toHexString((int)(b >> 4) & 0xf);
        result += Integer.toHexString((int)(b) & 0xf);
        return result;
    }
    
    public static String bytesToHexString(final byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
        String result = "";
        for (int i = 0; i < byteArray.length; ++i) {
            result += byteToHexString(byteArray[i]);
        }
        return result;
    }
    
    public static byte[] hexStringToBytes(final String hexString) {
        if (hexString == null) {
            return null;
        }
        byte[] result = new byte[hexString.length() / 2];
        for (int idx = 0; idx < result.length; ++idx) {
            int strIdx = idx * 2;
            result[idx] = (byte) ((Character.digit(hexString.charAt(strIdx), 16) << 4)
                    + Character.digit(hexString.charAt(strIdx + 1), 16));
        }
        return result;
    }
    

    Edit:
    If you are converting String-byte[] and byte[]-String I would recommend using the charset or charsetname parameter for String.getBytes() and for the String-constructor. For example:

    byte[] bytes = "text".getBytes("UTF-8");
    String text = new String(bytes, "UTF-8");
    

    Remember not every platform or jvm may support the same charsets. For a list of charsets have a look here.

    If you are reading bytes from a File and want to interpret them as String, you also have to care about using the right charset.

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

Sidebar

Related Questions

I have a strange problem. I am trying to validate one form sent through
I want to encrypt some data in a form using jQuery before it's sent
i have problem to correctly bind data to WPF Chart. When i'm setting ItemsSource
I am currently re-designing an application and stumbled upon a problem serializing some data.
I have a system that needs to send and receive data over an insecure
I have a problem when I submit the form (pressing ENTER key or clicking
I have a problem with a form. I need it to perform two actions
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with fancybox. I want to write a function that will run
i have problem with autorotate on iphone i set up in all classes -

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.