Conversione tra byte a 7-bit e 8-bit

venerdì 19 novembre 2004 - 10.51

ffederico Profilo | Newbie

ho il seguente problema da risolvere:

leggo da uno StreamReader del testo (proveniente dallo standard output)

string sout = sr.ReadToEnd();

adesso vorrei leggerne il contenuto trasformandolo in byte

Encoding ascii = Encoding.ASCII;
byte[] ub = new byte[sFile.Length];
int i = ascii.GetBytes(sFile, 0, iFlag, ub, 0);

il problema e' che i caratteri trasformati in ascii sono signed. a me serve invece il formato ascii unsigned.

ffederico Profilo | Newbie

scrivo qui come ho risolto il problema se magari interesa a qualcun altro.

public unsafe override byte[] GetBytes(char[] chars) {
char ch;
byte []buffer=new byte[chars.Length];

for( int i=0;i<chars.Length;i++ ){
ch=chars[i];
buffer[i]=*((byte *)&ch);
}
return buffer;
} // GetBytes
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5