• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java & C++ pro question

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me how to equate a C/C++ struct formed message in java, eg,
C/C++ (excuse my C/C++ lingo)
struct {
char parm1,
char parm2,
int parm3 }

I need to send the equivalent message in Java over a socket connection.
Thanks.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to determine the data size that is being used for char and int in the C/C++ version you are using. Then it's simply a matter of casting your java types to the right size and placing them into a byte array to be sent over the socket.
From C the compiler I used in the past, char had a size of 8 bits and int had a size of 16 bits. This will give you an array of 4 bytes so you can use something like this to send a, b, 256.
char a = 'a';
char b = 'b';
int c = 256;
byte b[] = new byte[4];
b[0] = (byte)a;
b[1] = (byte)b;
b[2] = (byte)(c >> 8);
b[3] = (byte)c;
Send the byte array b and your there.
 
Gregory Garrison
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carl - thanks so much!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic