Link to home
Start Free TrialLog in
Avatar of CSecurity
CSecurityFlag for Iran, Islamic Republic of

asked on

StringBuilder to byte array not converting properly

I have a StringBuilder which contains encrypted data and for sure entire data in StringBuilder is non printable chars.

I tried to convert StringBuilder to byte array for sending using Socket, but it not getting converted to byte array properly.

I did this:

for (int i=0;i < 32; i++)
            {
                arr[i] = (byte) Output[i];
            }

arr is byte array and Output is StringBuilder

It's not getting converted properly for example for arr(2) it says 220 but it should be 152, in arr(19) it says 57 but it should be 139.

Maybe API I call receives this bytes but again it's not possible, because I call same API in VB6 and I get correct results....

I can't convert it to byte array and it not works totally.

Please advice... I also tried Convert.ToByte etc. not works

I tried this:
arr = System.Text.Encoding.ASCII.GetBytes(Output.ToString());

NONE WORKS...

Please advice... Thanks from now!
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

ASCII will trim all your characters above 127, better use UTF8:
arr = System.Text.Encoding.UTF8.GetBytes(Output.ToString());
 
Avatar of Gautham Janardhan
Gautham Janardhan

System.Text.StringBuilder Builder = new StringBuilder() ;
Builder.Append("a");
byte[] b = new System.Text.ASCIIEncoding().GetBytes(Builder.ToString().ToCharArray());
Avatar of CSecurity

ASKER

It makes 106 char from 32 chars UTF8 not works
what is the encoding of the string in the string builder. use that class instead of AsciiEncoding
StringBuilder temp = new StringBuilder();

And I tried your solution
byte[] b = new System.Text.ASCIIEncoding().GetBytes(Builder.ToString().ToCharArray());

But not worked again :(
byte[] b =
new System.Text.UTF8Encoding().GetBytes(Builder.ToString().ToCharArray());
Created 56 bytes out of 32 bytes. Not works. My AES dll should get 32 multiplier data. It not worked again...

P.S. Thanks a lot for your helps
can u check what is the length of string in the string builder ? is it 32 ?
Yes, it's 32
StringBuilder's length is 32 and have 32 non-printable chars.

When I do this:
byte[] b =
new System.Text.UTF8Encoding().GetBytes(Builder.ToString().ToCharArray());

I have byte b with length of 56

try this
StringBuilder bb =
                new StringBuilder();
            bb.Append("A");
 
            byte[] s = new byte[bb.Length];
            for (int i = 0; i < bb.Length; i++)
            {
                s[i] = C2b(bb[i]);
            }
 
along with
 
public static byte C2b(char c)
        {
            if ((int)c < 256) return (byte)c;
            throw new Exception("character overflows a byte");
        } 

Open in new window

I get this exception: character overflows a byte
3 char of my StringBuilder variable is int of 8249
yep .. i think that is the problem....those characters wont fit in a single byte..
-> because I call same API in VB6 and I get correct results....
how are u converting these chars in VB there also u must be getting the same error ....
 

 
in VB I do this:

I get output of API in String variable

Then I do this:

For i = 0 to Len(strx) -1
arr(i) = asc(mid(strx, i+1, 1))
next i

And its converted properly without any problem.

StringBuilder may have problem but in String of VB I don't have any char's byte higher than 256
u will have to replace those here i guess..
How? What should I do? C++ DLL returns normal char * array and in VB6 I receive it into String and it works properly, what should I do to get it working in C#?

Here is C++ DLL's defination

void __stdcall EncryptString (LPSTR in, LPSTR result, size_t n, int iMode, char *Password)

Output is in result variable and I get it working in VB6.

I defined it in C# like this:

But I can't get it working:

[DllImport("CRYPT.dll")]
        private static extern void EncryptString(
            String cData,
            [Out] StringBuilder Output,
            int nSize,
            int iMode,
            String Password
         );
[DllImport("CRYPT.dll")]
private static extern void EncryptString(
String cData,
[Out][
MarshalAs(UnmanagedType.LPStr)] StringBuilder Output,
int nSize,
int iMode,
String Password
);
 
Again I got Exception("character overflows a byte");
ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THANKS A LOT MAN! YOU SOLVED IT! THANKS!
Now I can encrypt string in C# and decode in VB6 and vice-versa. You are really C# sage... :) Thanks once again!