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 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.
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.
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.
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
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
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
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
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
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: TheKeith |
last post by:
can you convert a number into a string by simply adding a set of quotes like
this: 5 + ""
Thanks
|
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 );
|
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...
|
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 ***
|
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 =...
|
by: ern |
last post by:
Does a function exist to convert a 128-bit hex number to a string?
|
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....
|
by: Mike |
last post by:
Why is this converted to a string???
var newidNumber =((idNumber/2).toFixed())+1;
Thanks
Mike
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |