locked
Convert string of byte to byte[] RRS feed

  • Question

  • I use postgreSQL to Encode:

    select encode('\037\213\010\000\000\000\000\000\004\000\253V\012K-*\316\314\317S\262R2T\322Qr\313,J\215740\21074U\2622\3243\320Q\212\217w\316/\315+\211\217\007\363k\001\347\270@\3731\000\000\000', 'Base64')

    The result is: 

    H4sIAAAAAAAEAKtWCkstKs7Mz1OyUjJU0lFyyyxKjTc0MIg3NFWyMtQz0FGKj3fOL80riY8H82sB
    57hA+zEAAAA=

    How I can Encode that string like that on C#.

    Thanks,

    Monday, October 15, 2018 10:34 AM

Answers

  • Show how your string is represented in C# and check this example:

    string example = @"\037\213\010\000\000\000\000\000\004\000\253V\012K-*\316\314\317S\262R2T\322Qr\313,J\215740\21074U\2622\3243\320Q\212\217w\316/\315+\211\217\007\363k\001\347\270@\3731\000\000\000";
    
    string t = Regex.Replace( example, @"\\(\d\d\d)", m => ( (char)Convert.ToInt32( m.Groups[1].Value, 8 ) ).ToString() );
    byte[] a = t.Select( c => (byte)c ).ToArray();
    
    string result = Convert.ToBase64String( a );
    
    Console.WriteLine( example );
    Console.WriteLine( result );
    


    • Proposed as answer by Tech Aspirant Tuesday, October 16, 2018 6:11 AM
    • Marked as answer by Loris Charge Tuesday, October 16, 2018 6:13 AM
    Tuesday, October 16, 2018 5:48 AM

All replies

  • See Convert.ToBase64().

    namespace ConsoleCS
    {
        using System;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                string encodedData = "H4sIAAAAAAAEAKtWCkstKs7Mz1OyUjJU0lFyyyxKjTc0MIg3NFWyMtQz0FGKj3fOL80riY8H82sB57hA+zEAAAA=";
                byte[] data = Convert.FromBase64String(encodedData);
                Console.WriteLine("---");
                Console.WriteLine(encodedData);
                Console.WriteLine("---");
                Console.WriteLine(System.Text.Encoding.Default.GetString(data));
                Console.WriteLine("---");
                Console.WriteLine(Convert.ToBase64String(data));
                Console.WriteLine("---");
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
        }
    }

    Monday, October 15, 2018 11:05 AM
  • Thank you,

    But my string is '\037\213\010\000\000\000\000\000\004\000\253V\012K-*\316\314\317S\262R2T\322Qr\313,J\215740\21074U\2622\3243\320Q\212\217w\316/\315+\211\217\007\363k\001\347\270@\3731\000\000\000'

    not byte[] 

    How I can convert it into "H4sIAAAAAAAEAKtWCkstKs7Mz1OyUjJU0lFyyyxKjTc0MIg3NFWyMtQz0FGKj3fOL80riY8H82sB
    57hA+zEAAAA="

    Monday, October 15, 2018 11:34 AM
  • Hello,

    Is it possible in your case to connect with postgres server from C# application if it is then you can fire same query from your application and get the required results in C#.

    Monday, October 15, 2018 11:49 AM
  • You can only convert an array of bytes to base64 by using Convert.ToBase64().

    And you have a byte array, cause each \xxx is a byte. You just wrote it as escaped string in PostgreSQL. This string has no special meaning in C#. The equivalent would be something like:

    \x037\x213\x010\x000\x000\x000\x000\x000\x004\x000\x253V\x012K-*\x316\x314\x317S\x262R2T\x322Qr\x313,J\x215740\x21074U\x2622\x3243\x320Q\x212\x217w\x316/\x315+\x211\x217\x007\x363k\x001\x347\x270@\x3731\x000\x000\x000

    But your give sample base64 string is not the result of your given array/string, cause the bytes are different:

    Monday, October 15, 2018 11:55 AM
  • Using Npgsql.dll you can run Postgres query in .Net

    Add Npgsql.dll as Reference to your project

    you can get this dll from Nuget package

    https://www.nuget.org/packages/Npgsql/

    Refer below code for reference

     try
                {
                    string Query = @"select encode('\037\213\010\000\000\000\000\000\004\000\253V\012K-*\316\314\317S\262R2T\322Qr\313,J\215740\21074U\2622\3243\320Q\212\217w\316/\315+\211\217\007\363k\001\347\270@\3731\000\000\000', 'Base64')";
                    string connectionstring = "Server=127.0.0.1;Port=5432;Username=postgres;Password=1234;Pooling=false;";
                    NpgsqlConnection conn = new NpgsqlConnection(connectionstring);
                    if (conn.State == System.Data.ConnectionState.Closed) { conn.Open(); }
                    NpgsqlCommand cmd = new NpgsqlCommand(Query, conn);
                    NpgsqlDataReader reader = cmd.ExecuteReader();
                    DataTable dt = new DataTable();
                    dt.Load(reader);
                    if (dt.Rows.Count > 0) 
                    {
                        string output = dt.Rows[0][0].ToString();
                        MessageBox.Show(output);
                    }
                    conn.Close();
                }
                catch (Exception ex) { string errormsg = ex.ToString(); }


    • Marked as answer by Loris Charge Tuesday, October 16, 2018 2:44 AM
    • Unmarked as answer by Loris Charge Tuesday, October 16, 2018 2:44 AM
    Monday, October 15, 2018 12:34 PM
  • Thank Tech Aspirant that answer & explain was helpful but I try to make the tool running without Postgre, do we have any way to do that ?
    Tuesday, October 16, 2018 3:14 AM
  • Show how your string is represented in C# and check this example:

    string example = @"\037\213\010\000\000\000\000\000\004\000\253V\012K-*\316\314\317S\262R2T\322Qr\313,J\215740\21074U\2622\3243\320Q\212\217w\316/\315+\211\217\007\363k\001\347\270@\3731\000\000\000";
    
    string t = Regex.Replace( example, @"\\(\d\d\d)", m => ( (char)Convert.ToInt32( m.Groups[1].Value, 8 ) ).ToString() );
    byte[] a = t.Select( c => (byte)c ).ToArray();
    
    string result = Convert.ToBase64String( a );
    
    Console.WriteLine( example );
    Console.WriteLine( result );
    


    • Proposed as answer by Tech Aspirant Tuesday, October 16, 2018 6:11 AM
    • Marked as answer by Loris Charge Tuesday, October 16, 2018 6:13 AM
    Tuesday, October 16, 2018 5:48 AM
  • @Viorel_ It works checked with different string in postgres documentation as well.

    Tuesday, October 16, 2018 6:13 AM
  • Thank you it works quite good for me.
    Tuesday, October 16, 2018 6:14 AM
  • Hi Loris Charge,

    Thank you for posting here.

    If you want to make the tool running without Postgre, do you mean you want to convert the string below to Base64?

    \037\213\010\000\000\000\000\000\004\000\253V\012K-*\316\314\317S\262R2T\322Qr\313,J\215740\21074U\2622\3243\320Q\212\217w\316/\315+\211\217\007\363k\001\347\270@\3731\000\000\000


    If yes, based on my test, I could not convert it to Base64 with the string below you provided.

    "H4sIAAAAAAAEAKtWCkstKs7Mz1OyUjJU0lFyyyxKjTc0MIg3NFWyMtQz0FGKj3fOL80riY8H82sB
    57hA+zEAAAA="

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 16, 2018 6:19 AM
  • Could you explain for me how it works please, I tried to understand but ... 

    string t = Regex.Replace( example, @"\\(\d\d\d)", m => ( (char)Convert.ToInt32( m.Groups[1].Value, 8 ) ).ToString() );

    Thank you,

    Tuesday, October 16, 2018 7:06 AM
  • Could you explain for me how it works please, I tried to understand but ... 

    string t = Regex.Replace( example, @"\\(\d\d\d)", m => ( (char)Convert.ToInt32( m.Groups[1].Value, 8 ) ).ToString() );


    Assuming that sequences like “\037”, “\213”, etc., represents the octal values, the sample replaces them with the corresponding characters. It use Regular Expressions to find such sequences; “\\(\d\d\d)” means “\” followed by three digits; m is the found sequence, and m.Groups[1] is the numeric part without “\”. Convert class converts the found substrings to integer numbers using the octal base. Then each integer is interpreted as the code of a single character. Printable characters, which appear in the string directly: “V”, “K-*”, etc., remain unchanged.


    Tuesday, October 16, 2018 8:17 AM
  • Is reconvert from base64 to string byte possible in this case @Viorel_ ?
    Tuesday, October 16, 2018 10:23 AM