473,657 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

323131313531313 33432317E547E31 32357E5553447E4 67E353636303130 303034383532363 2
7E307E56

It should be 80 I think.
thanks
grs
Nov 15 '05 #1
12 21451

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.m icrosoft.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****@budgete xt.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
george r smith <gs****@budgete xt.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #7
string value =
"32313131353131 333432317E547E3 132357E5553447E 467E35363630313 030303438353236 3
27E307E56";
byte[] data = System.Text.Enc oding.GetBytes( value);
int length = data.Length;

????

"george r smith" <gs****@budgete xt.com> wrote in message
news:ey******** ******@TK2MSFTN GP10.phx.gbl...
Greetings,

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

323131313531313 33432317E547E31 32357E5553447E4 67E353636303130 303034383532363 2 7E307E56

It should be 80 I think.
thanks
grs

Nov 15 '05 #8
george r smith <gs****@budgete xt.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
test account <bi**********@h ax0r.dyndns.org > wrote:
string value =
"32313131353131 333432317E547E3 132357E5553447E 467E35363630313 030303438353236 3
27E307E56";
byte[] data = System.Text.Enc oding.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.GetByt es is an instance method, not
a static method.

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

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

Similar topics

2
9700
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
3113
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
3893
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 limitations of numerical precision of the double type, I'm presuming that the value will be stored in a decimal for the time being. Supposing the following: decimal d1 = 123.45; decimal d2 = 888.1234567;
1
1881
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
22078
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 = "abc13def345gh56.7fghdfgf" /* get number(1) returns 13 get number(2) returns 345 get number(3) returns 56.7 get number(4) returns undefined
14
11923
by: ern | last post by:
Does a function exist to convert a 128-bit hex number to a string?
3
6994
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. Can anyone help me with that? Here is the problem: vals2 = ldap_get_values(ld,e,b);
11
1419
by: Mike | last post by:
Why is this converted to a string??? var newidNumber =((idNumber/2).toFixed())+1; Thanks Mike
2
3484
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 number only.Floating point number vary from time to time. May be 23.88, 8.0. This is my code for splitting the floating point number. But it gets only 5 numbers. ex:When i have START888.66 It gets only 888.6.So could some one help me to do the...
0
8420
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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 we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.