• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Can I map a string buffer to a java class?

 
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like in C++...

struct DATA{
char companyID[10];
char filler1[10];
char InvoiceDate[10];
}data;

char buf[]= {"ccccccccccffffffffffiiiiiiiiii"};

memcpy(&data, buf, sizeof DATA); //map the buffer to the struct
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The state variables of a StringBuffer object are about what you would expect:

You will note the access modifier private. That means that you can't copy or change the state of a StringBuffer object without using the public methods and constructors documented in the API.

What would happen if you accessed these private members directly and Sun changed them next year to add features or take advantage of better hardware?

So the short answer to your question is no.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember, Java doesn't really allow you to work closely with memory. What memcpy() essentially does is accepts two pointers to memory addresses and then copies memory starting from address b, over to an area starting at address a. Since Java is so high level, there isn't a way to manipulate memory like this. Hence why pointers in C are so powerful and yet, also become such a problem to people who really don't know what they are.
[ January 05, 2005: Message edited by: Dean Joness ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you would have to do, of course, is write the method to do it yourself:


[ January 06, 2005: Message edited by: Joel McNary ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another version:
 
M Burke
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I ended up doing a bunch of string copies. To bad there is no memcpy() in Java like in C
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a System.arraycopy method that takes two arrays, a start and end point for each and copies the data over. I'm not sure if this is whay you're looking for, but It would be better than looping through a array.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Burke:
Yes, I ended up doing a bunch of string copies. To bad there is no memcpy() in Java like in C



If that's really a problem then you're using the wrong language: use C.

I suspect however it's more the case that you're not yet up to speed with Java and you haven't taken a couple of steps back and asked "what am I actually trying to achieve here" and from there "what's the best way to do that in Java that fits Java's paradigm: not C's".
 
reply
    Bookmark Topic Watch Topic
  • New Topic