This question already has answers here:
How do you convert a byte array to a hexadecimal string, and vice versa?
(53 answers)
How can I convert a hex string to a byte array? [duplicate]
(4 answers)
Closed 9 years ago.
This is a duplicate question, my apologies everyone!
Firstly, I apologize if this is a simple question, I have been searching for a very long time, and either an answer about this does not exist, the answer I am looking for has been buried under answers to questions about how to convert a string to a byte array, or I'm not searching with the right terminology. I have also found a few answers on converting a single hex value into a byte but applying those methods to work with what I want to do doesn't really seem to work very well.
What I am looking for is not how to convert "string" to a byte array, rather, I am trying to convert an already in bytes value from a textbox, into something my application will recognize as a byte array. I'll try to explain better with an example:
textBox.Text = 019F314A
I want byte[] bytes to equal { 0x01, 0x9F, 0x31, 0x4A }
Hopefully that makes sense. Thanks to anyone who can offer any assistance!
I believe you can use Convert.ToByte(), you might have to slice your string in pairs and loop through it.
If you do a quick search there are many topics on this already on stackoverflow
How do you convert Byte Array to Hexadecimal String, and vice versa?
You can also look at this MS example, it is to convert to int, but the idea is the same.
http://msdn.microsoft.com/en-us/library/bb311038.aspx
Related
This question already has answers here:
How do I convert an array of floats to a byte[] and back?
(4 answers)
Closed 1 year ago.
I want to convert 794.328247 value to byte.
I used Convert.ToByte(794.328247) but it showing following error,
Error : - Value was either too large or too small for an unsigned byte.
So anyone can help me to resolve this issue.
Thanks.
You probably want Bitconverter.GetBytes
byte[ ] byteArray = BitConverter.GetBytes( 794.328247);
Note that this produce an array of bytes since a float is 32bit, so it needs 4 bytes to store it. Do the reverse with ToSingle.
The alternative is to truncate the float: var b = (byte) 794.328247;, but this is usually not a good idea since a byte has a far smaller range of values that a float. In some cases you might need to first normalize the input value to some range of values before truncating. I.e. you would remap values from say 750-800 to the 0-255 range.
This question already has answers here:
How do you convert a byte array to a hexadecimal string, and vice versa?
(53 answers)
Closed 8 years ago.
i have string like
string test = "0x527AE53437CAED39D3E2A8B3E90FFC3BA9073B6D933BC05FF6677B5521DC34"
How can i convert the string to byte[]?
use SoapHexBinary in namespace System.Runtime.Remoting.Metadata.W3cXsd2001
string s = "0x527AE53437CAED39D3E2A8B3E90FFC3BA9073B6D933BC05FF6677B5521DC34";
byte[] num = SoapHexBinary.Parse(s.Substring(2)).Value;
That string is hexadecimal.
If you want to convert it to a numeric you're going to need special handling - that's a very big number and will overflow the basic types.
If it was a reasonable size, all numeric types are agnostic of base-representation. To see a hexadecimal version of a number, you simply call .ToString("X") on it.
EDIT
My answer was based on the initial version of the question before the byte[] was specified. There is a previous SO question and answer for this: How can I convert a hex string to a byte array?
This question already has answers here:
Determine a string's encoding in C#
(10 answers)
Closed 9 years ago.
I have a string read as a UTF8 (not from a file, can't check BOM).
The problem is that sometimes the original text was formed with another encoding, but was converted to UTF8 - so the string is not readable, sort of gibberish.
is it possible to detect that this string is not actual UTF8?
Thanks!
No. They're just bytes. You could try to guess, if you wanted, by trying different conversions and seeing whether there are valid dictionary words, etc., but in a theoretical sense it's impossible without knowing something about the data itself, i.e. knowing that it never uses certain characters, or always uses certain characters, or that it contains mostly words found in a given dictionary, etc. It might look like gibberish to a person, but the computer has no way of quantifying "gibberish".
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
.NET String to byte Array C#
How do I convert String to byte[] array and vice versa? I need strings to be stored in some binary storage. Please show example in both directions. And one more thing: each string maybe bigger than 90Kb.
If you want to use UTF-8 encoding:
// string to byte[]
byte[] bytes = Encoding.UTF8.GetBytes(someString);
// byte[] to string
string anotherString = Encoding.UTF8.GetString(bytes);
Before you march off and use one of the examples someone's already given you should be aware that there is, in general, no unique mapping between a string and a sequence of bytes. How the string is mapped to binary (and vice versa) is determined by the encoding that you use. Joel Spolsky wrote an awesome article on this subject.
When decoding binary to get a string, you need to use the same encoding as was used to produce the binary in the first place, otherwise you'll run into problems.
Use the Encoding class.
How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
byte[] array pattern search
I am trying to write a simple compression algorthm in C# and .NET 3.5 and I need to be able to search a particular file for occurrences of a certain sequence of bits. what is the fastest way of doing this?
Since the file is 8K I would start by reading all the bytes into an in memory array like this:
byte[] bytes = System.IO.File.ReadAllBytes(fileName);
There is a code sample to search in this stack overflow entry:
byte[] array pattern search