473,503 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting the number of bytes used by characters of a String

Hi,

How one can count the number of bytes used by all the characters of a
String ?

i.e. String.Length * 2 (a char is 2 bytes long), but using more "proper"
ways.

Thanks.
Nov 16 '05 #1
6 1705
you can use GetByteCount method of String Class

--
rajagopal

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #2
Rajagopal Pasupuleti <ra****************@yahoo.com> wrote:
you can use GetByteCount method of String Class


I think you mean the GetByteCount method of the Encoding class - there
is no such method in the String class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Nicolas <ni*****@serpe.org> wrote:
How one can count the number of bytes used by all the characters of a
String ?


In what encoding? In the CLR itself, or in a particular encoding?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon Skeet [C# MVP] wrote:
Nicolas <ni*****@serpe.org> wrote:
How one can count the number of bytes used by all the characters of a
String ?

In what encoding? In the CLR itself, or in a particular encoding?


Just as it is, I mean the space it takes on RAM.
I think a String class stores a string as an array of char, just want to
know what place this array occupies.
Nov 16 '05 #5
Nicolas wrote:
Jon Skeet [C# MVP] wrote:
Nicolas <ni*****@serpe.org> wrote:
How one can count the number of bytes used by all the characters of a
String ?


In what encoding? In the CLR itself, or in a particular encoding?


Just as it is, I mean the space it takes on RAM.
I think a String class stores a string as an array of char, just want to
know what place this array occupies.


To be very clear:

String str = "hi";

How can I find the size of str in mem ? (no matter if we do not consider
the size of the enclosing class itself)
Nov 16 '05 #6
Nicolas <ni*****@serpe.org> wrote:
In what encoding? In the CLR itself, or in a particular encoding?


Just as it is, I mean the space it takes on RAM.
I think a String class stores a string as an array of char, just want to
know what place this array occupies.


It's not actually an array of char itself - string is a very unusual
type, in that the size of the object itself depends on the contents.

I believe the size of the object is 20+(n/2)*4 where n is the number of
characters in the string. (The result of n/2 is rounded down.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3038
by: Srinath Avadhanula | last post by:
Hello, I am wondering if there is a way of counting graphemes (or glyphs) in python. For example, in the following string: u'\u0915\u093e\u0915' ( or equivalently, u"\N{DEVANAGARI LETTER...
122
5193
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
12
21439
by: george r smith | last post by:
Greetings, What is the C# code that returns the number of bytes in a string such as 32313131353131333432317E547E3132357E5553447E467E3536363031303030343835323632 7E307E56 It should be 80 I...
1
6908
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
8
20724
by: Preben Randhol | last post by:
Hi If I use len() on a string containing unicode letters I get the number of bytes the string uses. This means that len() can report size 6 when the unicode string only contains 3 characters...
4
8422
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
7
2087
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
4
2064
by: sumedh..... | last post by:
In a compiler there are 36bits for a word and to store a character 8bits are needed. In this to store a character two words appended. Then for storing k characters string,how many words are needed?
8
5252
by: xiaolim | last post by:
i making a simple program to count the different kinds of characters in a text file and then display them out, however i only manage to count the total numbers of characters. #include...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.