473,394 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How do you get the number of bytes in a string

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
12 21425

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
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
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
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
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
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
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
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: TheKeith | last post by:
can you convert a number into a string by simply adding a set of quotes like this: 5 + "" Thanks
6
by: Nick | last post by:
Hi, How can I check if a number exists by itself in this string by using the RegExp object? --- var mystring = "11,111,01,011"; var match = "1"; var re = new RegExp( match );
1
by: Julie | last post by:
I have the need to preserve the precision of a user-entered number, but that needs to be stored in a (non-string) floating point variable (such as double or decimal). Due to the internal...
1
by: oviam packirisamy | last post by:
how to convert number to string in sqlserver eg:- 10 -> ten display *** Sent via Developersdex http://www.developersdex.com ***
6
by: Andrew Poulos | last post by:
If I have a string with numerals in it and I want to get each number in sequence. Is there a way to do it without parsing the string character by character? For example, str =...
14
by: ern | last post by:
Does a function exist to convert a 128-bit hex number to a string?
3
by: Todd_Goselin | last post by:
Hello, I've got a problem that I can't fix by myself, I'm fairly new to programming in C and in Unix and so I'm having a tough time doing a simple task of converting a binary number to a string....
11
by: Mike | last post by:
Why is this converted to a string??? var newidNumber =((idNumber/2).toFixed())+1; Thanks Mike
2
by: ghjk | last post by:
I want to split phone number and string. ex:94778485899 I want to Extract only 778485899 part from the phone number ex: START45.99 Want to remove START from the string and get the floating point...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...

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.