PC Review


Reply
Thread Tools Rate Thread

How to determine the number of bytes in a string

 
 
Ole
Guest
Posts: n/a
 
      14th Jun 2006
By converting a string in a textbox to a byte array I'll need the byte size
of the string - how to determine that?

Thanks
Ole


 
Reply With Quote
 
 
 
 
Bruno Jouhier
Guest
Posts: n/a
 
      14th Jun 2006
Encoding.GetByteCount(str) should do the job.

For example, if you chose the UTF8 encoding for the byte representation (the
result depends on the encoding), you can write:

int len = System.Text.Encoding.UTF8.GetByteCount(str);

Bruno

"Ole" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> By converting a string in a textbox to a byte array I'll need the byte
> size of the string - how to determine that?
>
> Thanks
> Ole
>



 
Reply With Quote
 
Barry Kelly
Guest
Posts: n/a
 
      14th Jun 2006
"Ole" <(E-Mail Removed)> wrote:

> By converting a string in a textbox to a byte array I'll need the byte size
> of the string - how to determine that?


What method are you using to convert? Every char in the string is two
bytes in size, so if you are just converting the chars directly and not
using any particular encoding, the answer is string.Length * 2.

However, if you are using an encoding, you can either call
Encoding.GetByteCount() or simply call one of the Encoding.GetBytes()
overloads which returns an array of the right length. E.g.:

---8<---
byte[] bytes = Encoding.UTF8.GetBytes("foo");
--->8---

-- Barry

--
http://barrkel.blogspot.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I determine is string is number? Saga Microsoft VB .NET 8 10th Mar 2010 03:37 PM
how to find out number of bytes in a string joyce Microsoft C# .NET 4 13th Jun 2007 04:50 PM
Get number of actual bytes in a string TW Microsoft Dot NET Compact Framework 2 18th Sep 2006 03:29 PM
Counting the number of bytes used by characters of a String Nicolas Microsoft C# .NET 6 12th Dec 2004 09:23 PM
How do you get the number of bytes in a string george r smith Microsoft C# .NET 12 16th Feb 2004 01:17 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:55 PM.