convert string to 1-dimensional array of byte

Posted on 2004-11-29
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
Question by:ardixiv
    LVL 28

    Accepted Solution

    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];

    LVL 15

    Expert Comment

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

    Expert Comment

    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);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Normally a window is moved by clicking on the caption bar and dragging. You may want your user to be able to move borderless forms or move a form by clicking anywhere in the form without the limitation to the caption bar. There are many ways to do i…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now