Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
I got error when i convert string to sbyte

For byte attay i used
byte[] abyte1 = new byte[4];
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
abyte1 = encoding.GetBytes(s1);

and its work fine with no error.

but when i change code byte array to sbyte array
sbyte[] abyte1 = new sbyte[4];

then above code is not work and got string to sbyte conversion error.
Is there any system.tect class for the encoding??
In my code i am only use sbyte[] array not byte[] array.
please help me for this giving me proper answer.....

regards,
ravi
Posted
Updated 3-May-11 3:16am
v6
Comments
BobJanova 3-May-11 9:00am    
Why do you need sbytes? Most of the .Net framework functions use bytes, not sbytes.
Dave Kreskowiak 3-May-11 13:13pm    
Why on earth are you converting a string to SByte?? Nothing that handles character data should ever need a mathematically Signed Byte for characters.

First convert the String to Byte. Then convert Byte to SByte.
byte[] abyte1 = new byte[4];
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
abyte1 = encoding.GetBytes(s1);

sbyte[] sbytes = Array.ConvertAll(abyte1, q => Convert.ToSByte(q));
 
Share this answer
 
v2
Comments
Ravi Sharma 2 3-May-11 7:15am    
Hello, You give me byte to sbyte conversion, but i want string to sbyte convesion . string to byte conversion we use asciiencoding method, but what we have to use string to sbyte conversion?
In my code i am not using byte[] array , i only use sbyte[] array.
Its greatful for me, if u give me code of this..........
BobJanova 3-May-11 9:00am    
Reread the code, it creates a byte[] only as a temporary intermediate to creating the sbyte[].
Olivier Levrey 3-May-11 8:22am    
Have my 5.
Toniyo Jackson 3-May-11 8:23am    
Thanks Oliver
go through this below link this may helps you

Here[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900