By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,460 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,460 IT Pros & Developers. It's quick & easy.

How do you get the number of bytes in a string

P: n/a
Greetings,

What is the C# code that returns the number of bytes in a string such as

32313131353131333432317E547E3132357E5553447E467E35 36363031303030343835323632
7E307E56

It should be 80 I think.
thanks
grs
Nov 15 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

Well, there is nothing that would return the number of bytes, but
String.Length would return the number of characters.
A char would be unicode, which is 16 bits, so the string would be at least
160 bytes (actually I counted 83 characters, so 166+ bytes)
--
The hotmail account will most likely not be read, so please respond only
to the news group.
Nov 15 '05 #2

P: n/a
Morten,
thanks
I was afraid of that I would like a "sizeof".

The packet.length return 162, so I guess we subtract 2 for the 'x0' end of
string giving 160 and 160 / 2 gives us 80 bytes which is what the specs say
the number of bytes is.

Does this seem correct.
grs

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op**************@msnews.microsoft.com...

Well, there is nothing that would return the number of bytes, but
String.Length would return the number of characters.
A char would be unicode, which is 16 bits, so the string would be at least
160 bytes (actually I counted 83 characters, so 166+ bytes)
--
The hotmail account will most likely not be read, so please respond only
to the news group.

Nov 15 '05 #3

P: n/a
There is a sizeof operator but I think that would only show you the
class/structure size.
sizeof(String);
Also, sizeof only works with the 'unsafe' code blocks.
--
The hotmail account will most likely not be read, so please respond only
to the news group.
Nov 15 '05 #4

P: n/a
george r smith <gs****@budgetext.com> wrote:
What is the C# code that returns the number of bytes in a string


There's no such thing as the number of bytes in a string. There's the
number of bytes in an encoded version of a string, or the number of
characters in a string.

What are you really trying to measure?

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

P: n/a
even in unsafe mode, you cannot take the sizeof of a reference type. it only works with value types

----- Morten Wennevik wrote: ----

There is a sizeof operator but I think that would only show you the
class/structure size
sizeof(String)
Also, sizeof only works with the 'unsafe' code blocks
--
The hotmail account will most likely not be read, so please respond only
to the news group

Nov 15 '05 #6

P: n/a
Jon,

In our first project with c# (we used to use Delphi) I have a spec that
calls for the total number of bytes of a socket transaction bo be inserted
into a field.

I also have to round up a section of the packet that is to be encrypted to a
multiple of 16 bytes so I will need know the number of bytes there.

I have been watching this newsgroup so I know I will get the answer from you
:).

thanks
grs
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
george r smith <gs****@budgetext.com> wrote:
What is the C# code that returns the number of bytes in a string


There's no such thing as the number of bytes in a string. There's the
number of bytes in an encoded version of a string, or the number of
characters in a string.

What are you really trying to measure?

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

Nov 15 '05 #7

P: n/a
string value =
"32313131353131333432317E547E3132357E5553447E467E3 53636303130303034383532363
27E307E56";
byte[] data = System.Text.Encoding.GetBytes(value);
int length = data.Length;

????

"george r smith" <gs****@budgetext.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
Greetings,

What is the C# code that returns the number of bytes in a string such as

32313131353131333432317E547E3132357E5553447E467E35 36363031303030343835323632 7E307E56

It should be 80 I think.
thanks
grs

Nov 15 '05 #8

P: n/a
george r smith <gs****@budgetext.com> wrote:
In our first project with c# (we used to use Delphi) I have a spec that
calls for the total number of bytes of a socket transaction bo be inserted
into a field.

I also have to round up a section of the packet that is to be encrypted to a
multiple of 16 bytes so I will need know the number of bytes there.

I have been watching this newsgroup so I know I will get the answer from you
:).


So you're transmitting the string in this packet, yes? If so, what
encoding are you using? (Given that it does need to be encoded into
bytes.) If you know the encoding, just convert the string into a byte
array using that encoding and find the length of the array. (You'll
need to convert it into the array anyway in order to send it,
probably.)

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

P: n/a
test account <bi**********@hax0r.dyndns.org> wrote:
string value =
"32313131353131333432317E547E3132357E5553447E467E3 53636303130303034383532363
27E307E56";
byte[] data = System.Text.Encoding.GetBytes(value);
int length = data.Length;

????


That won't work quite as you've posted it because you need to say
*which* encoding to use - Encoding.GetBytes is an instance method, not
a static method.

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

P: n/a
damn, you're right.

I meant to type System.Text.Encoding.Ascii.GetBytes(value)

I guess you can do
int length = value.Length * 2;
since a char is 2 bytes, and a string stores chars only.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
test account <bi**********@hax0r.dyndns.org> wrote:
string value =
"32313131353131333432317E547E3132357E5553447E467E3 53636303130303034383532363 27E307E56";
byte[] data = System.Text.Encoding.GetBytes(value);
int length = data.Length;

????


That won't work quite as you've posted it because you need to say
*which* encoding to use - Encoding.GetBytes is an instance method, not
a static method.

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

Nov 15 '05 #11

P: n/a
test account <bi**********@hax0r.dyndns.org> wrote:
damn, you're right.

I meant to type System.Text.Encoding.Ascii.GetBytes(value)
That still wouldn't work because Ascii should be ASCII, but I get the
idea :)
I guess you can do
int length = value.Length * 2;
since a char is 2 bytes, and a string stores chars only.


But that gets the number of bytes in the internal representation - it
doesn't get the number of bytes written when the data is encoded.

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

P: n/a

Hi george,

Is your problem resolved?

If you still have anything unclear, please feel free to tell me, I will
help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.