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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:26:00+00:00 2026-06-18T04:26:00+00:00

Good day, I am having some trouble. I am trying to send a character

  • 0

Good day,

I am having some trouble. I am trying to send a character array over the network to a server.

here is my code:

char[] arr= {3,4};    
public void sendMessage(String message){
    if (out != null && !out.checkError()) {
        out.print(arr);
        out.flush();
    }
}

When I run that, lanshark detects that it recieved a packet with 2 bytes of data. ok good all is well.

Now when i run this:

 char[] arr= {3,160};      // notice 160 *****
public void sendMessage(String message){
    if (out != null && !out.checkError()) {
        out.print(arr);
        out.flush();
    }
}

lanshark says that the packet has 3 bytes of data? the exact data is :

03 c2 a0

Now why is it adding the c2 in there? i understand that it has some thing to do with the fact that my char is bigger than 127. But i need to send this value of 160 .

Please can you help me. Do i need to use another type of data, or send it in a different way? I know you can do this in C. How can i do it in java?

here is the code for my out object:

PrintWriter out;
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

Thanks

  • 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-18T04:26:02+00:00Added an answer on June 18, 2026 at 4:26 am

    In fact, the program is doing exactly what you told it to do. It is sending two characters; i.e. the unicode codepoints 3 (\u0003) and 160 (\u00a0). These characters are being encoded using your platform’s default character encoding … which appears to be UTF-8. The bytes c2 a0 are the UTF-8 encoding for the \u00a0 character.

    But what you are actually trying to do is send 2 bytes.

    In Java char is a 16 bit type, not an 8 bit type. If you want to send 8 bit values you need to use the byte type.

    The other mistake you are making is that you are trying to send (essentially) binary data using a Writer. The Writer interface is for (16-bit) character oriented data. You should be using the OutputStream API …

    Anyhow … here’s a code snippet to illustrate how you should send an array of bytes;

    byte[] arr = new byte[]{3, (byte) 160);
    OutputStream out = socket.getOutputStream();
    out.write(arr);
    

    I just changed it to that now, ” out.print(new String(arr).getBytes(Charset.forName(“UTF-8″))); ” and this is the data i get on wireshark: 11 bytes : 5b42403431653230396538 it should still be 2 bytes?

    You are making it worse!

    Lets take this apart:

    • new String(arr) gives you a 2 character String.
    • .getBytes(...) will turn that into a 3 byte array containing the bytes 03 c2 a0.
    • out.print(...) will attempt to call a print method on the PrintWriter API.

    But which one? Well you supplied an argument whose declared type is byte[]. And that will result in you calling print(Object).

    But wait a minute … what does PrintWriter.print(Object) do? Well the first thing is that it does is to call toString() on the argument.

    And what does that do? Well since the object is a byte[], this calls the default toString() method provided by java.lang.Object. And that gives you a String that looks like B[@xxxxxxxx where [B is the “class name” for a byte array, and the sequence of xs is a hexadecimal representation of the array object’s identity hashcode!

    And then you output that that String.

    And behold your 2 bytes (actually characters) have turned into 11 bytes.

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

Sidebar

Related Questions

Good day all, I am having some trouble with image permissions. I am loading
Good day. I'm having some issues with my android project specifically listview. I tried
Good day everyone, I'm having trouble with IE9 today, everything else works but IE9
Good day. Having read about Model 2 architecture I got confused about some points.
Good Day, I'm new to this Youtube GData api and i'm having trouble with
Good day. I am having trouble compiling pysqlite on windows. I have managed to
Good morning, I am having trouble with the following code <html> <head> <title>highlight date</title>
Good day! I am having errors in merging/combining/concatenating arrays. For example I have arrays,
Good day everyone. I have been having the same problem all day at work
First of all thank you for reading this. I am having some trouble fetching

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.