byte[] to string in C# .NET Core [duplicate] - c#

This question already has answers here:
How to convert UTF-8 byte[] to string
(16 answers)
Closed 6 years ago.
I have a project which is using (I assume) .NET Core. I have a library which stores securely key-pair values. The key is string and the value should be a byte[], so I have to convert the string that I want to store as byte[]:
bytes[] my_bytes = Encoding.Unicode.GetBytes(txtSomeInfo.Text)
The problem is when i retrieve the value, because is an byte[] array i should convert it back to string like this:
my_string = Encoding.Unicode.GetString(my_bytes)
I found it some answers in Stack Overflow, but... I can't use it just like that because this version of GetString method of System.Text.Encoding is asking for two additional parameters: int index, and int count
How can I get my string back?

my_string = Encoding.Unicode.GetString(my_bytes, 0, my_bytes.Length);
As Jon points out in the comments, the C# naming standards use camelCase for local variables so you should rename my_bytes and my_string to myBytes and myString respectively.

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

How to convert UTF-8 to string in C# [duplicate]

This question already has answers here:
How to Decode "=?utf-8?B?...?=" to string in C#
(4 answers)
Closed 8 years ago.
H, I have this string that I got from a htm file
String s = "%u05d9%u05e8%u05d5%u05e9%u05dc%u05d9%u05dd"
It is in UTF8 code value Hebrew characters and I want to convert it to a real string that I can write to a file and have meaning (not just the code value of the char set).
I tried to do this but it does not work -
byte[] bytes = Encoding.UTF8.GetBytes(s);
addr = Encoding.UTF8.GetString(bytes);
The original string might have been UTF-8 encoded, but this is irrelevant because what you have right now is an URL encoded string. You can decode it using HttpUtility.UrlDecode:
System.Web.HttpUtility.UrlDecode("%u05d9%u05e8%u05d5%u05e9%u05dc%u05d9%u05dd")
returns ירושלים
try
System.Net.WebUtility.UrlDecode(s);

issue in converting hexadecimal to byte array [duplicate]

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?

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