Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

convert string to 1-dimensional array of byte

Posted on 2004-11-29
3
Medium Priority
?
1,451 Views
Last Modified: 2008-02-01
Hi all,

In order to set a property item of orientation in a Tiff file's header i need to convert a string to a 1 dimensional array of byte.  
Is there a class or a function ready to do such a conversion?

thanks in advance
ardi
0
Comment
Question by:ardixiv
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 2000 total points
ID: 12695322
Hi ardixiv,

i don't think that there is such function. you can do it manually

private byte[] ConvertStringToByte ( string s )
{
 if ( s != null && s.Length > 0)
 {
    byte[] b = new byte[s.Length];
    for ( int index=0; index<s.Length; index++ )
    {
        b[index] = Convert.ToByte ( s[index] );
    }

    return b;
 }

    return new byte[0];
}

Regards!
B..M
mmarinov
0
 
LVL 15

Expert Comment

by:SRigney
ID: 12697365
string test = "a test string to encode";
System.Text.UTF8Encoding ts = new System.Text.UTF8Encoding();
byte[] ba = ts.GetBytes(test);
0
 
LVL 15

Expert Comment

by:SRigney
ID: 12697375
You may want Ascii Encoding instead of Unicode though.

string test = "a test string to encode";
System.Text.ASCIIEncoding ts = new System.Text.ASCIIEncoding();
byte[] ba = ts.GetBytes(test);
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question