single byte characters ? how many bytes for 37 characters ?

B

bitshift

Im pulling a string out of a text field in sql and into a byte array in my
C# code. Help frefresh my memory here, a single asci character is 1 byte
correct ? So why is a string that is 37 characters long, result in a byte
array that is 2224 elements in length ?
 
N

Nicholas Paldino [.NET/C# MVP]

bitshift,

I don't know of any encoding that is going to produce those numbers of
bytes when converting.

Is the field in SQL Server a text field, or is it a char/varchar field?
If it is a text field, then you probably shouldn't have this problem.

My guess is that this is a char field which has a fixed length of 2224,
and that is why the string is that long. If you want the field in the
database to not have a fixed length, then the column has to be a varchar
field.
 
P

Peter Duniho

Im pulling a string out of a text field in sql and into a byte array in
my
C# code. Help frefresh my memory here, a single asci character is 1 byte
correct ? So why is a string that is 37 characters long, result in a
byte
array that is 2224 elements in length ?

Well, a "char" in C# is a Unicode character, with 2 bytes. That said,
that wouldn't explain a length of 2224 bytes for a 37 character string. I
agree with Nicholas that it's likely what you're getting from the SQL
database isn't really a character string only 37 characters long.

Pete
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

bitshift said:
Im pulling a string out of a text field in sql and into a byte array in my
C# code. Help frefresh my memory here, a single asci character is 1 byte
correct ? So why is a string that is 37 characters long, result in a byte
array that is 2224 elements in length ?

I addition to the other posts, post here your field definition and the code
you are using.
 
B

bitshift

Yes, the field this string is coming from is a text column. After stepping
through a few more times, I found out I was getting an exception. After
pulling the string from the row, I was trying to decode it from base64, but
if this was an invalid image data, it would bomb. So now im checking the
length of the string first, instead of simply checking my byte array length.


Nicholas Paldino said:
bitshift,

I don't know of any encoding that is going to produce those numbers of
bytes when converting.

Is the field in SQL Server a text field, or is it a char/varchar field?
If it is a text field, then you probably shouldn't have this problem.

My guess is that this is a char field which has a fixed length of 2224,
and that is why the string is that long. If you want the field in the
database to not have a fixed length, then the column has to be a varchar
field.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

bitshift said:
Im pulling a string out of a text field in sql and into a byte array in
my C# code. Help frefresh my memory here, a single asci character is 1
byte correct ? So why is a string that is 37 characters long, result in
a byte array that is 2224 elements in length ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top