Set the value of a new byte using a string? - c#

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");

Related

Get byte value as a string

I'm trying to get the actual value of a byte instead of its conversion to an ASCII string.
I have a byte with the value 0x00000073 how do I get 73 as a string value instead of it being converted to the string value of 115?
Andrew Morton is spot on.
If you have byte b = 115; then you want string s = b.ToString("X");

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.

get dsa signature value in octect string

I am trying to read the octect string value of attribute dsa-signature. I got the field from default naming context properties. But its in byte array, when I try to convert it into a string it give wrong output.
Does anyone knows how to do correct octect converion?
DirectoryEntry entry = new DirectoryEntry("LDAP://DC=cobra,DC=net");
PropertyValueCollection propCol = entry.Properties["dSASignature"];
Console.WriteLine(propCol.PropertyName + " : " + propCol.Value);
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
string str = enc.GetString((System.Byte[])propCol.Value);
Console.WriteLine("value : " + str);
Thanks in advance
But its in byte array, when I try to convert it into a string it give wrong output.
It's a byte array. Just an arbitrary sequence of bytes. It's not UTF-8-encoded text, which is what you're treating it as.
If you want to display the bytes formatted as hex, you can use:
byte[] data = (byte[]) propCol.Value;
string hex = BitConverter.ToString(data);
If you need to transfer it more efficiently but as text, you could use base64... but ideally you should treat it as a byte array for as long as possible, given that fundamentally it's just binary data.

How do I convert a Hexidecimal string to byte in C#?

How can I convert this string into a byte?
string a = "0x2B";
I tried this code, (byte)(a); but it said:
Cannot convert type string to byte...
And when I tried this code, Convert.ToByte(a); and this byte.Parse(a);, it said:
Input string was not in a correct format...
What is the proper code for this?
But when I am declaring it for example in an array, it is acceptable...
For example:
byte[] d = new byte[1] = {0x2a};
You have to specify the base to use in Convert.ToByte since your input string contains a hex number:
byte b = Convert.ToByte(a, 16);
byte b = Convert.ToByte(a, 16);
You can use the ToByte function of the Convert helper class:
byte b = Convert.ToByte(a, 16);
Update:
As others have mentioned, my original suggestion to use byte.Parse() with NumberStyles.HexNumber actually won't work with hex strings with "0x" prefix. The best solution is to use Convert.ToByte(a, 16) as suggested in other answers.
Original answer:
Try using the following:
byte b = byte.Parse(a, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
You can use UTF8Encoding:
public static byte[] StrToByteArray(string str)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}

Print out List<byte> as a string value

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();

Categories