Click here to Skip to main content
15,995,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have few symbols which generate with alt+codes , I want to insert & retrieve them in a table. These special codes are:-

Symbol --------------> ALT + Number
==========================================
☺ ---------------------------> Alt + 1
☻ ---------------------------> Alt + 2
♥ ---------------------------> Alt + 3
♦ ---------------------------> Alt + 4
♣ ---------------------------> Alt + 5
♠ ---------------------------> Alt + 6

I tried with char, nchar, varchar, nvarchar, text and xml but nothing worked.
Kindly help.
Posted
Comments
Tomas Takac 12-May-15 3:45am    
How do you do the insert? Post your code.
StackQ 12-May-15 3:59am    
I want to insert it as:-
INSERT INTO MyTable VALUES('♣');
StackQ 12-May-15 4:03am    
As well as I tried with StringToBinary, and BinaryToString, byte array but nothing worked.
private void ButtonShow_Click(object sender, EventArgs e)
{
try
{
string s1 = "♣";
s = StringToBinary(s1);
s = BinaryToString(s); //Not working here
cnn = new SqlConnection(connetionString);
cnn.Open();
string sql = "Insert into EDTable (RText) values ('♣≈Ω°±Σⁿ')";
cmd = new SqlCommand(sql, cnn);
cmd.ExecuteNonQuery();
da = new SqlDataAdapter("Select * from EDTable", connetionString);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];

}
catch (Exception ex) { }
finally
{
cmd.Dispose();
cnn.Close();
this.Close();
}
}
public static string StringToBinary(string data)
{
StringBuilder sb = new StringBuilder();
foreach (char c in data.ToCharArray())
{
sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
}
return sb.ToString();
}
public static string BinaryToString(string data)
{
List<byte> byteList = new List<byte>();
for (int i = 0; i < data.Length; i += 8)
{
byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
}
return Encoding.ASCII.GetString(byteList.ToArray());
}

1 solution

Almost certainly, you are saving the values in the database correctly - but what that doesn't save is any context: in this case the Font that you are displaying them from. If you have a good look at the data saved, it's perfectly reasonable: 01, 02, 03, 04, 05, 06 if you investigate the "raw hex" values. When you display them, you need to specify the right font is all.

Personally, I wouldn't do this: you are dependant on the user having the right font loaded, and that can't be guaranteed - users can add and remove fonts as they wish - so I'd use Icons or small images instead.
I'm just guessing that you are trying to create a card game of some kind - seriously, use pictures instead of font based characters stored in a database!
 
Share this answer
 
Comments
StackQ 12-May-15 5:01am    
I am trying to develop my encryption alog using these alt codes. And these are not special fonts. These are in built symbols ,can try with ALT + 1,2,3..... so on. But here issue is:-
1)INSERT INTO MyTable VALUES('♣') executed successfully.
2)But in table it saved as ?
3)So I am unable to retrieve it, in select clause as ♣
OriginalGriff 12-May-15 5:09am    
If this is encrypted data, you do **not** want to be storing and retrieving it as strings: you need to use byte data instead as strings have a nasty habit of changing character data that doesn't fit in the current font - byte data is designed not to change, and encrypted data is not supposed to be readable anyway!

"And these are not special fonts"
Yes they are: they are characters within a particular font which whatever application you are typing ALT+1 into is displaying - which is why they don't render correctly when you retrieve them from SQL: no font information!
StackQ 12-May-15 5:26am    
Any symbol in my question is not encrypted. Those are in original form. I agree with you for special fonts, but in my opinion, on few Computer I tried with ALT KEY + Numeric Keys and got these characters.
Example:- Alt + 5= ♣

Easily we can write on notepad as well. That's why I think those are not special.
OriginalGriff 12-May-15 5:40am    
Try it in Chrome: the "reply" textbox will do.
Doesn't work for me...
Or FireFox - again, nothing.
Any of the apps I write - nope.
The text editor I use (PsPAd)? Nope.
Even Notepad on my system just gives the "default beep" rather than adding a symbol.

It's very, very application specific...or possibly a function of a "key entry" app on your PC only!
Try it: Enter those characters to Notepad. Save the file, close Notepad.
Reopen the file in Notepad. Are they still there?
StackQ 12-May-15 6:44am    

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