STRING TO HEX how to add in a vector ob bytes? - c#

I have the following code:
string s = "2563MNBJP89256666666685755854";
Byte[] bytes = encoding.GetBytes(s);
string hex = "";
foreach (byte b in bytes)
{
int c=b;
hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(c.ToString()));
}
It prints the hex values . How can I add it in a vector ob bytes like this?
new byte [0x32,0x35..]
In hex I have : 323536....and so on. The next step is to add then in a byte[] vector in the following format 0x32,0x35..and so on; How to do this?
THX

Isn't bytes already the list of bytes you want?

C#: System.Text.Encoding.ASCII.GetBytes("test")

For C# you could try
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(strVar);//strVar is the string variable

in C# you can use Encoding.GetBytes Method

Related

German special characters put into Byte array

I'm doing encrypt algotythm right now and I need to encrypt german words also. So I have to encrypt for example characters like: ü,ä or ö.
Inside I've got a function:
private static byte[] getBytesArray(string data)
{
byte[] array;
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
array = asciiEncoding.GetBytes(data);
return array;
}
But when data is "ü", byte returned in array is 63 (so "?"). How can I return ü byte?
I also tried:
private static byte[] MyGetBytesArray(string data)
{
byte[] array;
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
Encoding enc = new UTF8Encoding(true, true);
array = enc.GetBytes(data);
return array;
}
but in this case I get 2 bytes in array: 195 and 188.
Please replace System.Text.ASCIIEncoding with System.Text.UTF8Encoding and rename the encoding object accordingly in your first example. ASCII basically does not support german characters, so this is why you'll have to use some other encoding (UTF-8 seems to be the best idea here).
Please take a look here: ASCII Encoding and here: UTF-8 Encoding
You can use this
System.Text.Encoding utf_8 = System.Text.Encoding.UTF8;
// This is our Unicode string:
string s_unicode = "abcéabc";
// Convert a string to utf-8 bytes.
byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(s_unicode);
// Convert utf-8 bytes to a string.
string s_unicode2 = System.Text.Encoding.UTF8.GetString(utf8Bytes);

How to save byte[] in c# application settings

I am trying to save a byte array (byte[]) in c# application settings that is returned by Object List View.
Can anyone give me a solution on how to save byte array in c# application settings?
or some trick on how to convert byte[] to something like a string then store, then retrieve and again convert it to byte array and give it back to object list view.
One of the most common ways to make a string from an array of bytes is encoding them in Base-64:
string encoded = System.Convert.ToBase64String(toEncodeAsBytes);
Use
byte[] bytes = System.Convert.FromBase64String(encoded);
to get your bytes back.
The canonical way to do this is to convert the byte[] to a string via base64 and the other way round.
By Different way you can convert Byte array to string and string to byte array. Like this :
1)
string asciiString = ASCIIEncoding.ASCII.GetString(byteArray);
byte[] byte = ASCIIEncoding.ASCII.GetBytes(asciiString);
2)
string base64String = System.Convert.ToBase64String(byteArray);
byte[] byte = System.Convert.FromBase64String(base64String);
3)
string utf8String = System.Text.Encoding.UTF8.GetString(byteArray);
byte[] byte = System.Text.Encoding.UTF8.GetBytes(utf8String);
you can also use System.Text.Encoding.BigEndianUnicode, System.Text.Encoding.Unicode, and System.Text.Encoding.UTF32 for converting Byte Array to string and string to Byte Array.
Hope, It should help you.

Python hmac and C# hmac

We have a python web service. It needs a hash as a parameter.
The hash in python is generated this way.
hashed_data = hmac.new("ant", "bat", hashlib.sha1)
print hashed_data.hexdigest()
Now, this is how I generate the hash from C#.
ASCIIEncoding encoder = new ASCIIEncoding();
Byte[] code = encoder.GetBytes("ant");
HMACSHA1 hmSha1 = new HMACSHA1(code);
Byte[] hashMe = encoder.GetBytes("bat");
Byte[] hmBytes = hmSha1.ComputeHash(hashMe);
Console.WriteLine(Convert.ToBase64String(hmBytes));
However, I'm coming out with different result.
Should I change the order of the hashing?
Thank you,
Jon
In order to print the result:
In Python you use: .hexdigest()
In C# you use: Convert.ToBase64String
Those 2 functions don't do the same thing at all. Python's hexdigest simply converts the byte array to a hex string whereas the C# method uses Base64 encoding to convert the byte array. So to get the same output simply define a function:
public static string ToHexString(byte[] array)
{
StringBuilder hex = new StringBuilder(array.Length * 2);
foreach (byte b in array)
{
hex.AppendFormat("{0:x2}", b);
}
return hex.ToString();
}
and then:
ASCIIEncoding encoder = new ASCIIEncoding();
Byte[] code = encoder.GetBytes("ant");
HMACSHA1 hmSha1 = new HMACSHA1(code);
Byte[] hashMe = encoder.GetBytes("bat");
Byte[] hmBytes = hmSha1.ComputeHash(hashMe);
Console.WriteLine(ToHexString(hmBytes));
Now you will get the same output as in Python:
739ebc1e3600d5be6e9fa875bd0a572d6aee9266

Convert a binary number to ascii characters

I am reading information from a device and it's returning data to me in integer format and I need to convert this to ASCII characters using C#.
The following is an example of the data I have to convert. I receive the integer value 26990 back from my device and I need to convert that to ASCII. I just happen to know that for this value, the desired result would be "ni".
I know that the integer value 26990 is equal to 696e in hex, and 110100101101110 in binary, but would like to know how to do the conversion as I can't work it out for myself.
Can anyone help please?
Many thanks,
Karl
int i = 26990;
char c1 = (char)(i & 0xff);
char c2 = (char)(i >> 8);
Console.WriteLine("{0}{1}", c1, c2);
Use BitConverter and Encoding to perform the conversion:
class Program
{
public static void Main()
{
int d = 26990;
byte[] bytes = BitConverter.GetBytes(d);
string s = System.Text.Encoding.ASCII.GetString(bytes);
// note that s will contain extra NULLs here so..
s = s.TrimEnd('\0');
}
}
If the device is sending bytes you can use something like this:
byte[] bytes = new byte[] { 0x69, 0x6e };
string text = System.Text.Encoding.ASCII.GetString(bytes);
Otherwise, if the device is sending integers you can use something like this:
byte[] bytes = BitConverter.GetBytes(26990);
string text = System.Text.Encoding.ASCII.GetString(bytes, 0, 2);
In either case, text will contain "ni" after that executes. Note that in the latter you may have to deal with endianness issues.
Further reference:
Encoding class
Encoding.ASCII property
Encoding.GetString method overloads

Convert string "0x32" into a single byte

I'm working with C# trying to convert a string value into a byte. Seems to be harder then I expected. Basically I have a string called hex = "0x32" and need byte block to equal this value.
string hex = "0x32";
byte block = Convert.ToByte(hex);
The above doesn't work, does anybody know how I can successfully assign the hex value to the byte. I need to append this byte to a byte array later in the code.
Try the following
byte block = Byte.Parse(hex.SubString(2), NumberStyles.HexNumber);
The reason for the SubString call is to remove the preceeding "0x" from the string. The Parse function does not expect the "0x" prefix even when NumberStyles.HexNumber is specified and will error if encountered
Convert.ToByte(hex, 16)
string hex = "0x32";
int value = Convert.ToInt32(hex, 16);
byte byteVal = Convert.ToByte(value);
Will work...
Edit
A little code to demonstrate that 0x32 (hex) and 50 (int) are the same.
string hex = "0x32";
byte[] byteVal = new byte[1];
byteVal[0] = Convert.Byte(hex, 16);
Console.WriteLine(byteVal[0] + " - Integer value");
Console.WriteLine(BitConverter.ToString(byteVal) + " - BitArray representation");;

Categories

Resources