Sql binary to c# - How to get SQL binary equivalent of binary in c#

后端 未结 2 628
野趣味
野趣味 2021-01-24 10:50

It might seem a dumb question to you guys. I have one SQL table with one binary column. It has some data in binary format.

e.g. 0x9A8B9D9A002020202020202020202020<

相关标签:
2条回答
  • 2021-01-24 11:35
    public static byte[] ConvertToBinary(string str)
    {
        System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
        return encoding.GetBytes(str);
    }
    

    or

    Convert.ToByte(string);
    
    0 讨论(0)
  • 2021-01-24 11:42

    It really depends on which encoding was used when you originally converted from string to binary:

     byte[] binaryString = (byte[])reader[1];
    
     // if the original encoding was ASCII
     string x = Encoding.ASCII.GetString(binaryString);
    
     // if the original encoding was UTF-8
     string y = Encoding.UTF8.GetString(binaryString);
    
     // if the original encoding was UTF-16
     string z = Encoding.Unicode.GetString(binaryString);
    
     // etc
    
    0 讨论(0)
提交回复
热议问题