|
-
Aug 12th, 2003, 08:32 AM
#1
Thread Starter
Hyperactive Member
Going crazy with encoding...
Some one can please explain me ALL about encoding????
All I want to do is doing a simple transformation of a string, and I can't believe it's not possible!!!!!!!!
E.G.
dim Mystring as string="ABCDEFG"
I want to take the ascii value of each character in this string and add 1. So that string will be:
"BCDEFGH".
What's the simplest way to do that??!??!?
HEEEEEEEEEEEEEEEEEEEEELP!!!!!!!!!!!!!!!!!!!!!
Thanks,
Xmas79
Learn, this is the Keyword...
-
Aug 12th, 2003, 09:10 AM
#2
Addicted Member
something like:
VB Code:
Dim Mystring As String = "ABCDEFG"
Dim myCharArray As Char() = Mystring.ToCharArray
Dim newString As String
For charIndex As Integer = 0 To myCharArray.Length - 1
newString &= Chr(Asc(myCharArray(charIndex)) + 1)
Next
MsgBox(newString)
-
Aug 12th, 2003, 09:19 AM
#3
or:
VB Code:
[COLOR=BLUE]Dim[/COLOR] Mystring [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String[/COLOR] = "ABCDEFG"
[COLOR=BLUE]Dim[/COLOR] x [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Integer
Dim[/COLOR] retString [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String
For[/COLOR] x = 0 [COLOR=BLUE]To[/COLOR] Mystring.Length - 1
retString += Convert.ToChar(Asc(Mystring.Chars(x)) + 1)
[COLOR=BLUE]Next
[/COLOR]MessageBox.Show(retString)
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Aug 12th, 2003, 05:35 PM
#4
Thread Starter
Hyperactive Member
Nooooooo..... Incredible! But, the same code in VB and C# produces different results!!!! Why????? For instance the code is the VB.NET and C# translation from C++ of the "DC++" client...
Learn, this is the Keyword...
-
Aug 12th, 2003, 05:37 PM
#5
Originally posted by Xmas79
Nooooooo..... Incredible! But, the same code in VB and C# produces different results!!!! Why????? For instance the code is the VB.NET and C# translation from C++ of the "DC++" client...
It won't provide different results AT ALL since both are accessing the same function with the same information.
You're doing something wrong and Ms Cleo does not frequent these boards, so you're going to have to post the code
-
Aug 12th, 2003, 05:42 PM
#6
Addicted Member
The above examples use the Microsoft.VisualBasic namespace. Why didn't you post on a C# board?
-
Aug 12th, 2003, 06:50 PM
#7
in C# it's easy also, here's a quick example
VB Code:
[COLOR=BLUE]private[/COLOR] [COLOR=BLUE]void[/COLOR] button1_Click([COLOR=BLUE]object[/COLOR] sender, System.EventArgs e)
{
[COLOR=BLUE]char[/COLOR][] Mystring="ABCDEFG".ToCharArray();
[COLOR=BLUE]string[/COLOR] retString=[COLOR=BLUE]string[/COLOR].Empty;
[COLOR=BLUE]int[/COLOR] x;
[COLOR=BLUE]for[/COLOR](x=0;x!=Mystring.Length;x++)
{
retString+=Convert.ToChar((([COLOR=BLUE]char[/COLOR])Mystring[x])+1);
}
MessageBox.Show(retString);
}
hope it helps.
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Aug 13th, 2003, 03:13 AM
#8
Thread Starter
Hyperactive Member
As I said, they produce different results... Can it be because the string I translate is composed with ascii set and not only by letters and numbers? And another thing: The right code is the C#, and I do know nothing about C# and I know VB.NET, so I'm sure I'm not doing strange things!!!
There's a little problems in VB when you have to manipulate a string byte by byte, and it's better to do it in C#... Encoding.UTF8, Encoding.UFT7... WHAT'S THAT??!??! In C# simply take that byte and transform it! I also tried the code you all posted, with
Convert.ToChar(Asc(Mystring.Chars(x)) + 1)
or
Chr(Asc(myCharArray(charIndex)) + 1)
but the result is the same. Both this method produces THE SAME result, but THIS result is different from the C# code, that is the RIGHT one.
Thanks.
Learn, this is the Keyword...
-
Aug 13th, 2003, 04:40 AM
#9
i'm not sure if you want vb.net or c# but to do the same in vb.net as the c# example i made, you need to do this...
VB Code:
[COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Sub[/COLOR] Button1_Click([COLOR=BLUE]ByVal[/COLOR] sender [COLOR=BLUE]As[/COLOR] System.Object, [COLOR=BLUE]ByVal[/COLOR] e [COLOR=BLUE]As[/COLOR] System.EventArgs) [COLOR=BLUE]Handles[/COLOR] Button1.Click
[COLOR=BLUE]Dim[/COLOR] Mystring [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Byte[/COLOR]() = System.Text.Encoding.ASCII.GetBytes("ABCDEFG".ToCharArray())
[COLOR=BLUE]Dim[/COLOR] retString [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String[/COLOR] = [COLOR=BLUE]String[/COLOR].Empty
[COLOR=BLUE]Dim[/COLOR] x [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Integer
[/COLOR] [COLOR=BLUE]For[/COLOR] x = 0 [COLOR=BLUE]To[/COLOR] Mystring.Length - 1
retString += Convert.ToChar(Mystring(x) + 1)
[COLOR=BLUE]Next
[/COLOR] MessageBox.Show(retString)
[COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Sub[/COLOR]
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Aug 13th, 2003, 06:39 AM
#10
Thread Starter
Hyperactive Member
Let's explain...
I was trying to do a little client for the "Direct Connect" network. To do this I took the protocol code from a well done and well known program called DC++. This program is written in C++. I was trying to convert this code into a VB.NET code.
Now, this network works in this way:
Client connects to server.
Server sends a string, let's say SERVERSTRING.
Client must respond to this string with another string, let's call CLIENTSTRING, that is elaborated from the SERVERSTRING. If the elaborated string is wrong the client can't get authenticate.
Now, with the help of a net sniffer, I took a SERVERSTRING and a CLIENTSTRING. All I've done is take the SERVERSTRING, elaborate it with the VB code and the C# code. The VB code returns a different string, the C# code the right string. This means that a client can't get authenticate if the elaboration is done with the VB code.
Clear at this point?
Now, I figured out the same problem when the client have to send the string. Infact, socket implementation wants a "array of bytes", and the problem is there: VB can't handle strings as well as C and C++ and C# do!!!! I couldn't get authenticate with VB code!! What I've done:
Made the conversion of CLIENTSTRING (elaborated with the C# code) from string to byte array (in VB) and sent (in VB). I tried with the "two variations" code you posted! Nothing to do...
Instead, I wrote the "send" routine in C# that manages strings very well, transformed string into an array of byte in C#, and sent always in C#. I got authentication...
How is it possible if C# and VB use the same functions and library??!??!?!??!?!
Learn, this is the Keyword...
-
Aug 14th, 2003, 05:51 PM
#11
maybe your sending method is off? You should prolly post all the code that deals with the encoding and sending the strings.
BTW: Moving a character up one place is not the safest method of encryption
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Aug 18th, 2003, 04:30 PM
#12
Lively Member
As I said, they produce different results... Can it be because the string I translate is composed with ascii set and not only by letters and numbers? And another thing: The right code is the C#, and I do know nothing about C# and I know VB.NET, so I'm sure I'm not doing strange things!!!
Try this.
Is in C# but is quite simple to convert to VB (although you can use http://www.kamalpatel.net/ConvertCSharp2VB.aspx to convert)
PHP Code:
Byte[] b = System.Text.UnicodeEncoding.ASCII.GetBytes(mystring);
for ( i = 0 ; i < code.Length ; i += 1) {
b[i]++;
}
mystring = System.Text.UnicodeEncoding.ASCII.GetString(b);
Bye
-
Aug 21st, 2003, 01:15 PM
#13
Thread Starter
Hyperactive Member
Thank you all...
Thank you all for replying, and excuse me for this late reply, I wasn't home 
<ABX:
Moving up one character was only an example. What I really do with these strings is xoring bytes, and replacing some special character, like byte 0, with strings like %/DCN000/%.
My post method wasn't off. Now I have that code nomore, but for instance it was only a thing like this:
VB Code:
Private Sub SendMethod(ByVal MyString as String)
dim buf() as byte
buf=Encoding.ASCII.GetBytes(MyString)
MySocket.Send(buf)
End sub
As I already said, I tried to change the code with "Encoding.xxx.GetBytes" with no results! The problem in fact is there.
Then I wrote a piece of code in C# like this for translating from string to bytes:
Code:
byte[] b=new byte[mystring.length];
int i;
for (i=0; i<mystrring.length; i++) b[i]=(byte) mystring[i];
return b;
and for translating from bytes to string:
Code:
StringBuilder sb=new StringBuilder();
int i;
for (i=0; i<mystring.length; i++) sb.Append(((char) mystring[i]));
return sb.ToString();
Now all works perfectly... Why??? Boohoho???? As you can see this is the only thing I change... Try creating a random string, and make strange operations like xoring bytes, adding, and so on. You'll be surprised to find that the C# version and the VB version produce different results... Or am I wrong at all???
Learn, this is the Keyword...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|