I have a string which under all circumstances satisfies ([a-zA-Z0-9])*
, and I want to let it run through sha1.
So how do I convert the string (or the char array obtained using ToCharArray()) to a byte array?
All answers I found so far have a big bunch of comments why the conversion from string to byte array is evil, they provide links to character encoding tutorials, and include a bunch of character encodings bloating the code.
Under my circumstances, conversion should be a LINQ oneliner, safe and neat.
I tried:
sha.ComputeHash(validator.ToCharArray().ToArray<byte>())
and I played around as far as my LINQ knowledge goes:
sha.ComputeHash(validator.ToCharArray().ToArray<byte>(c => (byte)c))
Write the contents of the object to the output stream using the writeObject() method of the ObjectOutputStream class. Flush the contents to the stream using the flush() method. Finally, convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.
The Ints class also has a toByteArray() method that can be used to convert an int value to a byte array: byte[] bytes = Ints. toByteArray(value);
char[] arr = { 'p', 'q', 'r', 's' }; The method valueOf() will convert the entire array into a string. String str = String. valueOf(arr);
validator.Select(c => (byte)c).ToArray()
Will also work. The "string" type supports "IEnumerable", so you can use LINQ directly with one.
The "Select" method allows you specify a lambda to customize your output. This replaces what you were trying to do with the "ToArray(c => (byte)c))".
Encoding.GetEncoding("UTF-8").GetBytes(chararray);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With