5 Replies - 11879 Views - Last Post: 24 April 2009 - 02:54 AM Rate Topic: -----

#1 rajnivas  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 02-April 09

how to encode a string & get output without any special characters

Post icon  Posted 24 April 2009 - 01:00 AM

hi,
I am using system name as table name in mysql. but my system name is durai-23456,but the mysql did not accept the name as table name because of special characters,so i decided to encode the table name without any special characters.so please help me in this aspect.i want output as small & containing alphabet only.
Is This A Good Question/Topic? 0
  • +

Replies To: how to encode a string & get output without any special characters

#2 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: how to encode a string & get output without any special characters

Posted 24 April 2009 - 01:02 AM

I would suggest changing the field type in MySQL, as far as I am aware you should be able to store that in an ordinary varchar field.

Other than that, if you still need to encode it, can you provide what you have so far code wise?

This post has been edited by danny_kay1710: 24 April 2009 - 01:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 rajnivas  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 02-April 09

Re: how to encode a string & get output without any special characters

Posted 24 April 2009 - 01:08 AM

View Postdanny_kay1710, on 24 Apr, 2009 - 12:02 AM, said:

I would suggest changing the field type in MySQL, as far as I am aware you should be able to store that in an ordinary varchar field.

Other than that, if you still need to encode it, can you provide what you have so far code wise?



hi,


i want to use system name as table name not in table.but sql did not accept it as table name because of special character ("duraivel-2312")

thanks for ur reply.
Was This Post Helpful? 0
  • +
  • -

#4 rajnivas  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 02-April 09

Re: how to encode a string & get output without any special characters

Posted 24 April 2009 - 01:18 AM

View Postdanny_kay1710, on 24 Apr, 2009 - 12:02 AM, said:

I would suggest changing the field type in MySQL, as far as I am aware you should be able to store that in an ordinary varchar field.

Other than that, if you still need to encode it, can you provide what you have so far code wise?




IPHostEntry heserver = new IPHostEntry();
			IPAddress[] add = ipEntry.AddressList;
			heserver = Dns.GetHostEntry(server);
			foreach (IPAddress curAdd in heserver.AddressList)
			{
				for (int o = 0; o < add.Length; o++)
				{
				   b[o] = heserver.HostName.ToString();
				   string P= EncodePassword(b[o]);	  //calling Encode method
				 }
			 }




public string EncodePassword(string orig_string)
		{
			Encoding cod = Encoding.GetEncoding(28595);
			Encoding unicode = Encoding.Unicode;

			byte[] unicodeBytes = unicode.GetBytes(orig_string);

			byte[] codBytes = Encoding.Convert(unicode, cod, unicodeBytes);

			char[] codChars = new char[cod.GetCharCount(codBytes, 0, codBytes.Length)];
			cod.GetChars(codBytes, 0, codBytes.Length, codChars, 0);
			string codString = new string(codChars);
			return codString;


		}


Was This Post Helpful? 0
  • +
  • -

#5 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: how to encode a string & get output without any special characters

Posted 24 April 2009 - 01:20 AM

View Postrajnivas, on 24 Apr, 2009 - 12:08 AM, said:

hi,


i want to use system name as table name not in table.but sql did not accept it as table name because of special character ("duraivel-2312")

thanks for ur reply.


Yes I did understand your post I was asking for more information - not the same information.

If your table is set up to accept a string (e.g. CHAR or VARCHAR) then it will accept the numbers and I am pretty confident it will also accept hyphens unless you have validation on the field to prevent it. You are going to struggle to find ANY encoding that doesn't include numbers in it unfortunately. I would convert it to a Base64 string - I am not sure what character it can use but I am sure it is only letters and numbers. Any other encoding/encryption will almost definitely use special characters even unprintable characters and is a rather big overhead when unless I am missing something the data doesn't seem to be sensitive.

Encoding doesn't seem to be the best option here, and following the rules here at DIC, I am not just going to give you any code, you must show at least a best effort first.


EDIT: Apologies you now have code posted - I will take a look and update this post

View Postrajnivas, on 24 Apr, 2009 - 12:18 AM, said:

public string EncodePassword(string orig_string)
		{
			Encoding cod = Encoding.GetEncoding(28595);
			Encoding unicode = Encoding.Unicode;

			byte[] unicodeBytes = unicode.GetBytes(orig_string);

			byte[] codBytes = Encoding.Convert(unicode, cod, unicodeBytes);

			char[] codChars = new char[cod.GetCharCount(codBytes, 0, codBytes.Length)];
			cod.GetChars(codBytes, 0, codBytes.Length, codChars, 0);
			string codString = new string(codChars);
			return codString;


		}



I am not sure what this does - to me it looks like it will return the exact same as you in putted??? I think your mistaking encoding for something else.

Encoding will not remove special characters it is all about how each character is represented in binary. Unicode uses 16 bits (2 bytes) to store each character. Standard ASCII using only 8 bits. ASCII has 255 possible characters. Unicode has 65535 possible characters. A lot of these will be unprintable or foreign. Unicode was introduced for use in things like internet explorer - where an english computer may end up visiting a japansese website. Before Unicode web browsers often got confused on what characters is should be showing or the user had to manually change the encoding IE used to read the site.Different areas had their own encoding.

You need to really encrypt and have an output that doesn't contain special characters. Which is going to be hard. The best thing I can suggest is


string EncodeString(string origText)
{
			byte[] stringBytes = Encoding.Unicode.GetBytes(origText);
			return Convert.ToBase64String(stringBytes, 0, stringBytes.Length);
}

string DecodeString(string encodedText)
{
			byte[] stringBytes = Convert.FromBase64String(encodedText);
			return Encoding.Unicode.GetString(stringBytes);
}




Hope it helps and clears things up

Thanks
Danny

This post has been edited by danny_kay1710: 24 April 2009 - 01:35 AM

Was This Post Helpful? 0
  • +
  • -

#6 rajnivas  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 02-April 09

Re: how to encode a string & get output without any special characters

Posted 24 April 2009 - 02:54 AM

View Postdanny_kay1710, on 24 Apr, 2009 - 12:20 AM, said:

View Postrajnivas, on 24 Apr, 2009 - 12:08 AM, said:

hi,


i want to use system name as table name not in table.but sql did not accept it as table name because of special character ("duraivel-2312")

thanks for ur reply.


Yes I did understand your post I was asking for more information - not the same information.

If your table is set up to accept a string (e.g. CHAR or VARCHAR) then it will accept the numbers and I am pretty confident it will also accept hyphens unless you have validation on the field to prevent it. You are going to struggle to find ANY encoding that doesn't include numbers in it unfortunately. I would convert it to a Base64 string - I am not sure what character it can use but I am sure it is only letters and numbers. Any other encoding/encryption will almost definitely use special characters even unprintable characters and is a rather big overhead when unless I am missing something the data doesn't seem to be sensitive.

Encoding doesn't seem to be the best option here, and following the rules here at DIC, I am not just going to give you any code, you must show at least a best effort first.


EDIT: Apologies you now have code posted - I will take a look and update this post

View Postrajnivas, on 24 Apr, 2009 - 12:18 AM, said:

public string EncodePassword(string orig_string)
		{
			Encoding cod = Encoding.GetEncoding(28595);
			Encoding unicode = Encoding.Unicode;

			byte[] unicodeBytes = unicode.GetBytes(orig_string);

			byte[] codBytes = Encoding.Convert(unicode, cod, unicodeBytes);

			char[] codChars = new char[cod.GetCharCount(codBytes, 0, codBytes.Length)];
			cod.GetChars(codBytes, 0, codBytes.Length, codChars, 0);
			string codString = new string(codChars);
			return codString;


		}



I am not sure what this does - to me it looks like it will return the exact same as you in putted??? I think your mistaking encoding for something else.

Encoding will not remove special characters it is all about how each character is represented in binary. Unicode uses 16 bits (2 bytes) to store each character. Standard ASCII using only 8 bits. ASCII has 255 possible characters. Unicode has 65535 possible characters. A lot of these will be unprintable or foreign. Unicode was introduced for use in things like internet explorer - where an english computer may end up visiting a japansese website. Before Unicode web browsers often got confused on what characters is should be showing or the user had to manually change the encoding IE used to read the site.Different areas had their own encoding.

You need to really encrypt and have an output that doesn't contain special characters. Which is going to be hard. The best thing I can suggest is


string EncodeString(string origText)
{
			byte[] stringBytes = Encoding.Unicode.GetBytes(origText);
			return Convert.ToBase64String(stringBytes, 0, stringBytes.Length);
}

string DecodeString(string encodedText)
{
			byte[] stringBytes = Convert.FromBase64String(encodedText);
			return Encoding.Unicode.GetString(stringBytes);
}




Hope it helps and clears things up

Thanks
Danny











Quote

Hi
Thans for ur help.it helps me lot to think.
with regards
M.P.Rajnivas

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1