Print out List<byte> as a string value - c#

I googled and found the statement to convert byte[] value to string value as this,
string myString =System.Text.Encoding.UTF8.GetString(myByteArray);
But I defined the variable to List<byte>. How can I convert the List<byte> bMsg to byte[] bMsgArrary?
List<byte> bMsg to byte[] bMsgArray ?;
Or there is other method to convert List<byte> value to string value. Appreciated for you help.

Use ToArray() on the list in order to get myByteArray and off you go.

Try
byte[] byteMessage = bMsg.ToArray();

Related

decode base64 does not give a correct original GUID

I have a string GUID I need to decrease its length from 36 to max 30,the problem is I need to parse to to GUID first :
var messageID = Guid.Parse("95ec6174-1d10-4348-a126-faeeb3a026dc");
it gives me a correct GUID but when I convert to base 64 and short it,Im getting a short string out of it but when i decoce that string it give me something weird!
var shortedGuid= System.Convert.ToBase64String(messageID.ToByteArray());
the result from above line is:
dGHslRAdSEOhJvrus6Am3A==
when i decode it I get :
taHC&&
any help will be appreacited
You must be doing something wrong then converting from the base64 string.
Use Convert.FromBase64String() to convert from string to byte array, then pass the byte array to the Guid constructor:
Guid originalGuid = Guid.Parse("95ec6174-1d10-4348-a126-faeeb3a026dc");
string base64 = Convert.ToBase64String(originalGuid.ToByteArray());
byte[] decoded = Convert.FromBase64String(base64);
// Pass the byte[] to Guid constructor.
// The result is a Guid = 95ec6174-1d10-4348-a126-faeeb3a026dc
Guid decodedGuid = new Guid(decoded);

How to convert a byteArray to Binary Value in c#?

I initilaly have a string which is converted to byteArray.
Then I convert this byteArray to HEX as shown below in my code.
Then I further need to convert this to a binary value.
string ID = "A0101185K";
byte[] ba = Encoding.Default.GetBytes(IC);
var hexString= BitConverter.ToString(ba).Replace("-", "");
You can convert byte to string in that way
static string ToBinary(byte b) => Convert.ToString(b, 2).PadLeft(8, '0');
and use it in your program like this
string MESSAGE = "A0101185K";
byte[] bytes = Encoding.Default.GetBytes(MESSAGE);
var binaries = string.Concat(bytes.Select(ToBinary));
I hope that helps you. But of course you didn't tell how you want to store your result, so maybe this is not the right answer.

Set the value of a new byte using a string?

I have a string with a numerical value that will fit into a data type byte variable (e.g. "243").
I want to set the byte to the numerical value of the string, something similar to byte myByte = 243; but using the string instead. How would this be possible?
Use byte.Parse() method:
byte myByte = byte.Parse("243");
You can do something like this
string myString = "243";
var byteData = Convert.ToByte(myString);
You can use this to convert your string value to byte:
byte MyByte = Convert.ToByte("243");

Input string was not in a correct format?

In my application i write the code like this
byte[] byt = new byte[Convert.ToSbyte(textbox1.Text)];
it is giving the error that input string was not in a correct format.
This is a wild guess, but are you trying to convert the contents of the text box into a byte array? If so, you can do it like this:
byte[] byt = Encoding.UTF8.GetBytes(textbox1.Text);
The text in textbox1 is not a valid numeral for a signed byte.
Does it have spaces? Letters? ...?
What are you trying to do? The new byte[num] creates an array of 'num' bytes, where 'num' is usually an integer. All bytes in the array are then 0.
It doesn't create a filled array, as I suspect you may be trying to do.
What are the contents of that textbox1.Text that gave the error?
what you want in fact is this
// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}

How to convert a byte array to a string?

Using the function from: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx
public static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
As you can see it returns a byte array, I want to convert the byte array to a string.
How can I convert it from a byte array to string and visa versa?
If you don't care how it's stored, an easy way is to use:
Convert byte array into string: Convert.ToBase64String(YourByteArray) and
Convert string into byte array: Convert.FromBase64String(YourString).
This will give a concise, printable ASCII representation of the byte array.
This can help you a lot, is about to converting into Hex format but can be very usefull
How do you convert Byte Array to Hexadecimal String, and vice versa?
System.Text.Encoding.ASCII.GetString(bytes);
While using Rijndael Encryption i faced this problem, it returns encrypted byte[] (array),
Convert byte[] to string;
myStringVariable= Convert.ToBase64String(myEncryptedByteArray);
Convert string to byte[];
byte[] bytes = Convert.FromBase64String(myStringVariable);
For more about Rijndael
Cheers !!!

Categories