| |

VerySource

 Forgot password?
 Register
Search
Author: weiranli00

Solve the binary conversion problem

[Copy link]

2

Threads

22

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

 Author| Post time: 2020-3-19 20:30:01
| Show all posts
Convert.FromBase64String (inputValue);
How to convert the value of inputValue to byte []
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-20 21:00:02
| Show all posts
Convert byte [] to string Convert.ToBase64String (inputValue);
Convert string to byte [] Convert.FromBase64String (inputValue);
Reply

Use magic Report

2

Threads

22

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

 Author| Post time: 2020-3-22 06:30:01
| Show all posts
But cmd.Parameters ["@ UserName"]. Value is of type Object
Reply

Use magic Report

2

Threads

22

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

 Author| Post time: 2020-3-22 13:15:01
| Show all posts
ParameterDirection.Output;

To:

 ParameterDirection.ReturnValue;

I tried it, it seems not possible
Reply

Use magic Report

0

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-3-23 15:30:01
| Show all posts
Sql's Binay pair receives byte [] to receive
byte [] b = cmd.Parameters ["@ UserName"]. value;

Conversion from byte [] to string requires encoding type
This means that you must be aware of the encoding of the strings stored in the database as binary

Generally there are 2 types: System.Text.Encoding.Default
Or System.Text.Encoding.UTF8
Reply

Use magic Report

0

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-3-23 20:00:02
| Show all posts
From your code above
Probably all use UTF32

byte [] b = cmd.Parameters ["@ UserName"]. value;
string sUserName = Encoding.UTF32.GetString (b);
Reply

Use magic Report

2

Threads

22

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

 Author| Post time: 2020-3-24 19:45:01
| Show all posts
byte [] b = cmd.Parameters ["@ UserName"]. value;
string sUserName = Encoding.UTF32.GetString (b);


Error 1 Cannot implicitly convert type "object" to "byte []". There is an explicit conversion (is a cast missing?)
Reply

Use magic Report

2

Threads

22

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

 Author| Post time: 2020-3-24 22:30:01
| Show all posts
How should the object type be converted to byte [] class? Thank you!
Reply

Use magic Report

2

Threads

22

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

 Author| Post time: 2020-3-31 09:30:01
| Show all posts
code show as below:
SqlCommand cmd = new SqlCommand ("UsmgcfoxsExict", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add (new SqlParameter ("@ Uid", SqlDbType.Binary, 30));
cmd.Parameters ["@ Uid"]. Value = Encoding.UTF32.GetBytes (paraUid);

cmd.Parameters.Add (new SqlParameter ("@ UserName", SqlDbType.Binary, 30));
cmd.Parameters ["@ UserName"]. Direction = ParameterDirection.Output;

conn.Open ();
cmd.ExecuteNonQuery ();
conn.Close ();

I want to get the value of the parameter "@UserName" in cmd and convert it to string type. The parameter "@UserName" is binary type.

use:
byte [] b = cmd.Parameters ["@ UserName"]. value;
string sUserName = Encoding.UTF32.GetString (b);

But I get the following error:
Error 1 Cannot implicitly convert type "object" to "byte []". There is an explicit conversion (is a cast missing?)
Reply

Use magic Report

0

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 Italy

Post time: 2020-3-31 18:00:01
| Show all posts
byte [] b = (byte []) cmd.Parameters ["@ UserName"]. value;
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list