Click here to Skip to main content
15,921,643 members
Home / Discussions / C#
   

C#

 
GeneralRe: byte[] cast to a string Pin
Jörgen Sigvardsson30-Oct-03 11:20
Jörgen Sigvardsson30-Oct-03 11:20 
GeneralRe: byte[] cast to a string Pin
Tym!30-Oct-03 12:28
Tym!30-Oct-03 12:28 
GeneralRe: byte[] cast to a string Pin
Jörgen Sigvardsson30-Oct-03 12:32
Jörgen Sigvardsson30-Oct-03 12:32 
GeneralRe: byte[] cast to a string Pin
Tym!30-Oct-03 12:41
Tym!30-Oct-03 12:41 
GeneralRe: byte[] cast to a string Pin
Jörgen Sigvardsson30-Oct-03 12:45
Jörgen Sigvardsson30-Oct-03 12:45 
GeneralRe: byte[] cast to a string Pin
Blake Coverett30-Oct-03 11:35
Blake Coverett30-Oct-03 11:35 
GeneralRe: byte[] cast to a string Pin
Tym!30-Oct-03 12:33
Tym!30-Oct-03 12:33 
GeneralRe: byte[] cast to a string Pin
Jeff Varszegi30-Oct-03 13:11
professionalJeff Varszegi30-Oct-03 13:11 
What about something like this? I didn't test, so it might have a typo, but would this sort of approach work? I don't know how you want the ordering. -Jeff

private const string blankString   = "";
public static string ConvertToString(byte[] bytes) {
    if (bytes == null) {
        return blankString;
    }
    int length = bytes.Length;
    if (length == 0) {
        return blankString;
    }
    else if ((length % 2) == 0) {
        length /= 2;
        char[] chars = new char[length];
        for(int charIndex = 0, byteIndex = 0; charIndex < length; charIndex++, byteIndex += 2) {
            chars[charIndex] = (char)((((int)bytes[byteIndex]) << 8) + ((int)bytes[byteIndex + 1]));
        }
        return new string(chars);
    }
    else {
        length /= 2;
        char[] chars = new char[length + 1];
        for(int charIndex = 0, byteIndex = 0; charIndex < length; charIndex++, byteIndex += 2) {
            chars[charIndex] = (char)((((int)bytes[byteIndex]) << 8) + ((int)bytes[byteIndex + 1]));
        }
        chars[length] = (char)(((int)bytes[bytes.Length - 1]) << 8);
        return new string(chars);
    }
}

GeneralRe: byte[] cast to a string Pin
leppie30-Oct-03 14:29
leppie30-Oct-03 14:29 
GeneralRe: byte[] cast to a string Pin
Tym!3-Nov-03 6:30
Tym!3-Nov-03 6:30 
Generalput codes into an event! Pin
Silly Boy30-Oct-03 6:19
Silly Boy30-Oct-03 6:19 
GeneralRe: put codes into an event! Pin
Heath Stewart30-Oct-03 9:09
protectorHeath Stewart30-Oct-03 9:09 
GeneralRe: put codes into an event! Pin
Judah Gabriel Himango30-Oct-03 9:12
sponsorJudah Gabriel Himango30-Oct-03 9:12 
GeneralRe: put codes into an event! Pin
Heath Stewart30-Oct-03 9:25
protectorHeath Stewart30-Oct-03 9:25 
GeneralRe: put codes into an event! Pin
Nick Parker30-Oct-03 13:28
protectorNick Parker30-Oct-03 13:28 
GeneralMoving a control Pin
mcgahanfl30-Oct-03 5:46
mcgahanfl30-Oct-03 5:46 
GeneralRe: Moving a control Pin
Mike Ellison30-Oct-03 6:21
Mike Ellison30-Oct-03 6:21 
GeneralRe: Moving a control Pin
mcgahanfl30-Oct-03 6:54
mcgahanfl30-Oct-03 6:54 
Generalpassing the datatable as a parameter 2 the SP Pin
venubabu30-Oct-03 3:26
venubabu30-Oct-03 3:26 
GeneralRe: passing the datatable as a parameter 2 the SP Pin
Paul Watson30-Oct-03 3:54
sitebuilderPaul Watson30-Oct-03 3:54 
GeneralLaunch a programm from an application Pin
Matthieu.C29-Oct-03 22:31
Matthieu.C29-Oct-03 22:31 
GeneralRe: Launch a programm from an application Pin
Corinna John30-Oct-03 2:37
Corinna John30-Oct-03 2:37 
QuestionRichTextBox?? Pin
electronicm29-Oct-03 21:38
electronicm29-Oct-03 21:38 
AnswerRe: RichTextBox?? Pin
Mazdak30-Oct-03 5:43
Mazdak30-Oct-03 5:43 
GeneralRe: RichTextBox?? Pin
Judah Gabriel Himango30-Oct-03 9:15
sponsorJudah Gabriel Himango30-Oct-03 9:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.