473,386 Members | 1,793 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,386 software developers and data experts.

how to find out number of bytes in a string

hi

How would I go abouts finding out how many bytes a string has. ( I
want to do this so that I can set the datatype on the database
approriately.)
Thanks
Joyce

Jun 13 '07 #1
4 2362
Joyce,

For a database, why would you want to know how many bytes are in a
string? If anything, you should be doing this on a character basis. I
can't imagine a database that doesn't support unicode characters, which is
what .NET natively stores its strings as.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"joyce" <jo***********@gmail.comwrote in message
news:11*********************@z28g2000prd.googlegro ups.com...
hi

How would I go abouts finding out how many bytes a string has. ( I
want to do this so that I can set the datatype on the database
approriately.)
Thanks
Joyce
Jun 13 '07 #2
Hi,

you can try to do something like
UnicodeEncoding.Unicode.GetByteCount("abc");

"joyce" <jo***********@gmail.comwrote in message
news:11*********************@z28g2000prd.googlegro ups.com...
hi

How would I go abouts finding out how many bytes a string has. ( I
want to do this so that I can set the datatype on the database
approriately.)
Thanks
Joyce

Jun 13 '07 #3
On Jun 13, 1:59 pm, joyce <joyce.sz.c...@gmail.comwrote:
How would I go abouts finding out how many bytes a string has. ( I
want to do this so that I can set the datatype on the database
approriately.)
Conceptually, a string doesn't have bytes, it has characters (which
you can find out with the Length property).

Now, a string can be *encoded* into a sequence of bytes, and you can
find out how many bytes would be involved for any particular encoding
using Encoding.GetByteCount(string).

Jon

Jun 13 '07 #4
On Jun 13, 2:36 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
For a database, why would you want to know how many bytes are in a
string? If anything, you should be doing this on a character basis. I
can't imagine a database that doesn't support unicode characters, which is
what .NET natively stores its strings as.
That doesn't mean it's the native storage of the database though. I
remember when I was coding against a Postgres database, it stored
character data in UTF-8 by default (on the platform I was running on).
Now an index could only take something like 2783 bytes per row, so I
had to truncate the string I was using to take only about 2700 bytes
*in UTF-8* without chopping it half way through a character or
anything like that.

Most amusing.

Jon

Jun 13 '07 #5

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

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
2
by: Vince | last post by:
I have a very specific problem to solve but I cannot find a data structure for it. I don't know if I am posting on the good newsgroup but I cannot find a software.design group. I would like to...
3
by: Dixie | last post by:
I am trying to create a registration system in which a unique text string accessible in a table of the program is turned into a unique number which will be matched with a number sent to the...
12
by: george r smith | last post by:
Greetings, What is the C# code that returns the number of bytes in a string such as 32313131353131333432317E547E3132357E5553447E467E3536363031303030343835323632 7E307E56 It should be 80 I...
8
by: Preben Randhol | last post by:
Hi If I use len() on a string containing unicode letters I get the number of bytes the string uses. This means that len() can report size 6 when the unicode string only contains 3 characters...
14
by: ern | last post by:
Does a function exist to convert a 128-bit hex number to a string?
6
by: androoo | last post by:
Hello all, I have a string for example : strTest = "a lineof text (60) witha number in it" I need to extract the number from between the brackets, the postion of the number in brackets is...
3
by: mouac01 | last post by:
Newbie here. How do I do a find and replace in a binary file? I need to read in a binary file then replace a string "ABC" with another string "XYZ" then write to a new file. Find string is the...
58
by: sh.vipin | last post by:
is there any way to find out number of bytes freed on a particular free() call in C
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.