issue in converting hexadecimal to byte array [duplicate] - c#

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?

Related

Convert.ToInt32 not accepting string hexadecimal values [duplicate]

This question already has answers here:
Convert integer to hexadecimal and back again
(11 answers)
How to Convert Hex String to Hex Number [duplicate]
(2 answers)
Closed 4 years ago.
I'm currently making a program which saves a bunch of memory addresses to a class automatically. However,
Convert.ToInt32(value)
doesn't seem to want to accept the string value "0xB24C" as a valid integer.
'Input string was not in a correct format.'
Actually I'm able to just save offsets as integers like this public const Int32 m_ArmorValue = 0xB24C;
Here's my code where I assign the integer;
hazedumper.netvars.m_ArmorValue = Convert.ToInt32(value);
value being the string offset "0xB24C"
Can anyone tell me why this error is occurring or is it not possible to convert a string memory address/hexadecimal value to an Int32.
Convert.ToInt32() takes the base as second parameter. Furthermore you must remove the 0x prefix. See the docs for details
var s="0xA123";
var i = Convert.ToInt32(s.Substring(2), 16);

Hex String To Byte Array C# [duplicate]

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

Converting string of binary to hex [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting long string of binary to hex c#
nyI'm looking for a way to convert a string of binary to a hex string.
the binary string has four positions.
the binary string looks something like this
string binarystring= "1011";
output string should be like this
output string="B";
is there any way to convert a string of binary into hex?
Convert.ToInt32("1011", 2).ToString("X");
For more information about the string value used with ToString() as its parameter, check the following documentation:
https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

Binary String to ASCII TEXT String [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Binary To Corresponding ASCII String Conversion
I posted a slimier question before but didn't got a correct answer..
my problem is i have a binary string as
00100000011101110110000101101110011101000010000001110100011011110010000001100100011011110111
Which im required to convert to "Normal ASCII String Text"
I used BinaryReader and read and encode System.Text.ASCIIEncoding.ASCII but im getting the same 0101010 string
byte[] bytearray = b.ReadBytes((int)length);
System.Text.Encoding encEncoder = System.Text.ASCIIEncoding.ASCII;
string bb = encEncoder.GetString(bytearray);
any help will be grateful !
first translate the binary string into a byte[] using How could I encode a string of 1s and 0s for transport?
next use the Convert.ToBase64String method to get the actual System.String

String to byte[] and vice versa? [duplicate]

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?

Categories