How do you get the number of bytes in a string

  • Thread starter Thread starter george r smith
  • Start date Start date
G

george r smith

Greetings,

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

32313131353131333432317E547E3132357E5553447E467E3536363031303030343835323632
7E307E56

It should be 80 I think.
thanks
grs
 
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)
 
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
 
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.
 
george r smith said:
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?
 
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
 
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
 
string value =
"32313131353131333432317E547E3132357E5553447E467E353636303130303034383532363
27E307E56";
byte[] data = System.Text.Encoding.GetBytes(value);
int length = data.Length;

????
 
george r smith said:
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.)
 
test account said:
string value =
"32313131353131333432317E547E3132357E5553447E467E353636303130303034383532363
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.
 
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 said:
test account said:
string value =
"32313131353131333432317E547E3132357E5553447E467E353636303130303034383532363
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.
 
test account said:
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.
 
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.
 
Back
Top