| View previous topic :: View next topic |
| Author |
Message |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue May 01, 2012 9:06 pm Post subject: [Java] Is there some type of sizeOf() function for Java? |
|
|
I'm converting a text file with records to a RandomAccessFile and I want each record to be uniform length, so I need to add some garbage/filler bytes to the end of my shorter records. In that case, I need to figure out what the max possible byte size/length of the record could be. Each record consists of ints, strings, and doubles. Is there some simple way to figure out the size of variables in Java?
Alternatively, I could just take the whole record as a string....but my problem is still figuring out how many bytes are in a string.
_________________
|
|
| Back to top |
|
 |
661089799107 Expert Cheater
Reputation: 3
Joined: 25 Jan 2009 Posts: 186
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue May 01, 2012 10:54 pm Post subject: |
|
|
Ok, actually using .SIZE did help me find the number of bits in an int (and double also). However I'm realizing that I only need to find the amount of bytes in a string.
This might be a dumb question, but can different strings of the same length have different byte size values?
If not, then my problem is almost solved...
_________________
|
|
| Back to top |
|
 |
661089799107 Expert Cheater
Reputation: 3
Joined: 25 Jan 2009 Posts: 186
|
Posted: Tue May 01, 2012 11:00 pm Post subject: |
|
|
| Quote: |
This might be a dumb question, but can different strings of the same length have different byte size values?
If not, then my problem is almost solved...
|
A byte is always 8-bits. Max value of 0xFF (255)
| Quote: |
Ok, actually using .SIZE did help me find the number of bits in an int (and double also). However I'm realizing that I only need to find the amount of bytes in a string.
|
The number of bytes in a string can be obtained with the length function.
Last edited by 661089799107 on Tue May 01, 2012 11:15 pm; edited 1 time in total |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue May 01, 2012 11:15 pm Post subject: |
|
|
I appreciate the help man. However, someString.length() will return the length of the string in unicode characters, not the bytes. This could still be useful though.... If all strings of the same length require the same amount of bytes than I can just add spaces to make all my strings the same size, and therefore all my records will have uniform byte size.
So, again, do all strings of the same length require the same amount of bytes?
For example, would "436fjg937t" require the same amount of bytes as "m'9shqIdm0" ? They're both 10 characters long, but the content is different.
_________________
|
|
| Back to top |
|
 |
661089799107 Expert Cheater
Reputation: 3
Joined: 25 Jan 2009 Posts: 186
|
Posted: Tue May 01, 2012 11:16 pm Post subject: |
|
|
| Quote: | | I appreciate the help man. However, someString.length() will return the length of the string in unicode characters, not the bytes. |
The below code would output the same result.
| Code: |
String s = "abc";
System.out.println(s.length());
System.out.println(s.getBytes().length);
|
| Quote: |
So, again, do all strings of the same length require the same amount of bytes?
For example, would "436fjg937t" require the same amount of bytes as "m'9shqIdm0" ? They're both 10 characters long, but the content is different.
|
436fjg937t=52 51 54 102 106 103 57 51 55 116 (10 bytes)
m'9shqIdm0=109 39 57 115 104 113 73 100 109 48 (10 bytes)
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue May 01, 2012 11:37 pm Post subject: |
|
|
That about answers my question.
Srsly though, I appreciate it <3
Actually I have more unrelated questions, but rather than make a new thread:
If I have strings : "4563 654564 Smith Will ...etc" , "324 235 Jones Mary..etc"
How could I get the first *actual* word? I basically want to parse from the 2nd space to the third space regardless of the lengths of the stuff in between them. (i.e. I just want Smith and Jones)
_________________
|
|
| Back to top |
|
 |
NotReallySureWhatGoesHere Expert Cheater
Reputation: -1
Joined: 20 Feb 2012 Posts: 110
|
Posted: Wed May 02, 2012 9:27 pm Post subject: |
|
|
| manc wrote: | That about answers my question.
Srsly though, I appreciate it <3
Actually I have more unrelated questions, but rather than make a new thread:
If I have strings : "4563 654564 Smith Will ...etc" , "324 235 Jones Mary..etc"
How could I get the first *actual* word? I basically want to parse from the 2nd space to the third space regardless of the lengths of the stuff in between them. (i.e. I just want Smith and Jones) |
Have some type of separator (|, ::, etc.) and http://www.rgagnon.com/javadetails/java-0438.html. Use an index of the new String array to find a particular String.
|
|
| Back to top |
|
 |
661089799107 Expert Cheater
Reputation: 3
Joined: 25 Jan 2009 Posts: 186
|
Posted: Thu May 03, 2012 1:17 am Post subject: |
|
|
| manc wrote: |
Actually I have more unrelated questions, but rather than make a new thread:
If I have strings : "4563 654564 Smith Will ...etc" , "324 235 Jones Mary..etc"
How could I get the first *actual* word? I basically want to parse from the 2nd space to the third space regardless of the lengths of the stuff in between them. (i.e. I just want Smith and Jones) |
By using regular expressions.
| Code: |
String s = "4563 654564 Smith Will";
Matcher matcher = Pattern.compile("\\s[A-Za-z]+\\s").matcher(s);
if(matcher.find())
System.out.println(matcher.group().trim());
|
|
|
| Back to top |
|
 |
|