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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:52:28+00:00 2026-06-13T22:52:28+00:00

Through a socket I am sending information from a program written in C to

  • 0

Through a socket I am sending information from a program written in C to a program written in Java.

Through the program in C, I am sending two bytes through a char array (using an internet socket), and the received information in Java is stored in a char array also.

My main problem is that the received information in the Java array does not correspond properly to the transmitted information from the C program.

I have read that the chars in Java are 16 bits long, and the chars in C are 8 bits long. That may be the problem, but I do not know how to handle/solve that.

The C code to send information is the following:

char buffer[256];
bzero(buffer,256);
n = read(fd, buffer, 255);  // getting info from an uart port, which works properly
n = write(sockfd,buffer,3); // send the information through the socket

Part of the Java code (for an Android app) is the following:

char[] buffer = new char[256];
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int readX = in.read(buffer, 0, 3);
if (readX > 0) { // I am using a handler to manipulate the info
    Message msg = new Message();
    msg.obj = buffer;
    mHandler.sendMessage(msg);
} 
....
// Part of the handler is the following:
mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
            char[] buffer;
            buffer = (char[])msg.obj; // the information here is different from the one sent from the C program
            ....
    }
}

Any suggestion to solve this problem I would really appreciate it.

Thanks in advance, Gus.

  • 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-13T22:52:28+00:00Added an answer on June 13, 2026 at 10:52 pm

    In C and C++ the char data type is 8-bit characters, corresponding roughly to the Java byte type. In Java, the fundamental char type is a 16-bit Unicode character. When you convert from bytes to characters (or vice-versa) in Java, a mapping has to occur, depending on the character encoding of the byte stream (UTF-8, ISO-8859-1, etc), so you have to know how the C byte stream is encoded. In your case I’d guess it’s ISO-8859-1. If it’s really binary data, then you should use the Java byte type.

    EDIT:

    You have to know whether the data being sent from C is character or binary, and if character, how the C program is encoding the data (ISO-8859-1, UTF-8, etc).

    If the data is binary then you must use BufferedInputStream to read bytes, not BufferedReader, which decodes bytes into characters, which you don’t want since the data is binary.

    If the data is character, then you can use the 2-argument constructor

    InputStreamReader(InputStream in, String charSetName)
    

    or one of the other 2-arg constructors that let you specify how to decode bytes into characters.

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

Sidebar

Related Questions

I'm sending through a socket from java to flex client some data and the
When sending information from a java application to a C# application through sockets, is
I am reading a java program, when it sending messages through socket, it aways
I recently learned about sockets in Java and sending information back and forth from
I'm sending a byte array through a socket and I'm receiving it through a
I am going through a socket program. In it, printStackTrace is called on the
I'm working through Beej's socket tutorial . Using the sample code I've created a
I am writing a little socket based program. I am using a class ModelEvent
I'm sending a ICMP packet through a socket in iOS: struct sockaddr hostAddress; self->hostAddress.sa_family
I'm creating a socket program to transfer data from one pc to another, but

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.