I have a code that works with .netstandard2.1/netcore3.0 because of this constructor of BigInteger class. This ctor is not available for netstandard2.0, and I'd like to be able to achieve the same functionality without forcing netstandard2.1.
Here's the problem:
Convert a string to UTF8, and hash it using Sha256
Convert the byte array in BigInteger using big endian byte order
Here's my solution that works with .netstandard2.1
static SHA256 sha256 = SHA256.Create();
internal static string GetEncoded(string value)
{
var data = sha256.ComputeHash(value.GetUTF8Bytes());
return new BigInteger(
value: data,
isUnsigned: true,
isBigEndian: true).ToString();
}
This is what the solution can be in netstandard2.0, unfortunately it doesn't produce the same result.
internal static string GetEncoded(string value)
{
var data = sha256.ComputeHash(value.GetUTF8Bytes());
return new BigInteger(value: data).ToString();
}
Here are some sample values and their expected encoded outputs.
"encoded": "68086943237164982734333428280784300550565381723532936263016368251445461241953",
"raw": "101 Wilson Lane"
"encoded": "101327353979588246869873249766058188995681113722618593621043638294296500696424",
"raw": "SLC"
From what I understand, the BigInteger(byte[]) ctor expects a little endian byte array. The few solutions I tried didn't produce the expected results, so I'm turning to SO for answers.
Any help would be great appreciated.
Related update issue to net core
Here is the code of the constructor you used: https://github.com/dotnet/corefx/blob/191ad0b5d52172366436322bf9d553dc770d23b1/src/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs#L256
You could adapt it in order to replace ReadOnlySpan by byte[].
Related
I'm doing some conversions between some structures and thier byte[] representation. I found two way to do this but the difference (performance, memory and ...) is not clear to me.
Method 1:
public static T ByteArrayToStructure<T>(byte[] buffer)
{
int length = buffer.Length;
IntPtr i = Marshal.AllocHGlobal(length);
Marshal.Copy(buffer, 0, i, length);
T result = (T)Marshal.PtrToStructure(i, typeof(T));
Marshal.FreeHGlobal(i);
return result;
}
Method 2:
public static T Deserialize<T>(byte[] buffer)
{
BinaryFormatter formatter = new BinaryFormatter();
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer))
{
return (T)formatter.Deserialize(stream);
}
}
so which one is better and what is the major difference?
You are talking about two different approaches and two different types of data. If you are working with raw values converted to Byte Array, go for the first method. If you are dealing with values serialized into Byte Array (they also contains serialization data), go for the second method. Two different situations, two different methods... they are not, let me say, "synonyms".
Int32 Serialized into Byte[] -> Length 54
Int32 Converted to Byte[] -> Length 4
When using the BinaryFormatter to Serialize your data, it will append meta data in the output stream for use during Deserialization. So for the two examples you have, youll find it wont produce the same T output given the same byte[] input. So youll need to decide if you care about the meta data in the binary output or not. If you dont care, method 2 is obviously cleaner. If you need it to be straight binary, then you'll have to use something like method 1.
I am having a data conversion issue that need your help.
My project is an InterOp between C and C#, all the data from C is char * type, the data itself could be binary or displayable chars, I.e. each byte is in 0x00 to 0xFF range.
I am using Data marshal::PtrToStringAnsi to convert the char* to String^ in CLI code, but I found some bytes value changed. for example C382 converted to C32C. I guess it is possibly because ANSI is only capable of converting 7-bit char, but 82 is over the range? Can anyone explain why and what is the best way?
Basically what I want to do is, I don't need any encoding conversion, I just want to convert any char * face value to a string, e.g. if char *p = "ABC" I want to String ^s="ABC" as well, if *p="C382"(represents binary value) I also want ^s="C382".
Inside my .NET code, two subclasses will take the input string that either represents binary data or real string, if it is binary it will convert "C382" to byte[]=0xC3 0x82;
When reading back the data, C382 will be fetched from database as binary data, eventually it need be converted to char* "C382".
Does anybody have similar experience how to do these in both directions? I tried many ways, they all seem to be encode ways.
The Marshal class will do this for you.
When converting from char* to byte[] you need to pass the pointer and the buffer length to the managed code. Then you can do this:
byte[] FromNativeArray(IntPtr nativeArray, int len)
{
byte[] retval = new byte[len];
Marshal.Copy(nativeArray, retval, 0, len);
return retval;
}
And in the other direction there's nothing much to do. If you have a byte[] then you can simply pass that to your DLL function that expects to receive a char*.
C++
void ReceiveBuffer(char* arr, int len);
C#
[DllImport(...)]
static extern void ReceiveBuffer(byte[] arr, int len);
....
ReceiveBuffer(arr, arr.Length);
I have this resource file which I need to process, wich packs a set of files.
First, the resource file lists all the files contained within, plus some other data, such as in this struct:
struct FileEntry{
byte Value1;
char Filename[12];
byte Value2;
byte FileOffset[3];
float whatever;
}
So I would need to read blocks exactly this size.
I am using the Read function from FileStream, but how can I specify the size of the struct?
I used:
int sizeToRead = Marshal.SizeOf(typeof(Header));
and then pass this value to Read, but then I can only read a set of byte[] which I do not know how to convert into the specified values (well I do know how to get the single byte values... but not the rest of them).
Also I need to specify an unsafe context which I don't know whether it's correct or not...
It seems to me that reading byte streams is tougher than I thought in .NET :)
Thanks!
Assuming this is C#, I wouldn't create a struct as a FileEntry type. I would replace char[20] with strings and use a BinaryReader - http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx to read individual fields. You must read the data in the same order as it was written.
Something like:
class FileEntry {
byte Value1;
char[] Filename;
byte Value2;
byte[] FileOffset;
float whatever;
}
using (var reader = new BinaryReader(File.OpenRead("path"))) {
var entry = new FileEntry {
Value1 = reader.ReadByte(),
Filename = reader.ReadChars(12) // would replace this with string
FileOffset = reader.ReadBytes(3),
whatever = reader.ReadFloat()
};
}
If you insist having a struct, you should make your struct immutable and create a constructor with arguments for each of your field.
If you can use unsafe code:
unsafe struct FileEntry{
byte Value1;
fixed char Filename[12];
byte Value2;
fixed byte FileOffset[3];
float whatever;
}
public unsafe FileEntry Get(byte[] src)
{
fixed(byte* pb = &src[0])
{
return *(FileEntry*)pb;
}
}
The fixed keyword embeds the array in the struct. Since it is fixed, this can cause GC issues if you are constantly creating these and never letting them go. Keep in mind that the constant sizes are the n*sizeof(t). So the Filename[12] is allocating 24 bytes (each char is 2 bytes unicode) and FileOffset[3] is allocating 3 bytes. This matters if you're not dealing with unicode data on disk. I would recommend changing it to a byte[] and converting the struct to a usable class where you can convert the string.
If you can't use unsafe, you can do the whole BinaryReader approach:
public unsafe FileEntry Get(Stream src)
{
FileEntry fe = new FileEntry();
var br = new BinaryReader(src);
fe.Value1 = br.ReadByte();
...
}
The unsafe way is nearly instant, far faster, especially when you're converting a lot of structs at once. The question is do you want to use unsafe. My recommendation is only use the unsafe method if you absolutely need the performance boost.
Base on this article, only I have made it generic, this is how to marshal the data directly to the struct. Very useful on longer data types.
public static T RawDataToObject<T>(byte[] rawData) where T : struct
{
var pinnedRawData = GCHandle.Alloc(rawData,
GCHandleType.Pinned);
try
{
// Get the address of the data array
var pinnedRawDataPtr = pinnedRawData.AddrOfPinnedObject();
// overlay the data type on top of the raw data
return (T) Marshal.PtrToStructure(pinnedRawDataPtr, typeof(T));
}
finally
{
// must explicitly release
pinnedRawData.Free();
}
}
Example Usage:
[StructLayout(LayoutKind.Sequential)]
public struct FileEntry
{
public readonly byte Value1;
//you may need to play around with this one
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public readonly string Filename;
public readonly byte Value2;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public readonly byte[] FileOffset;
public readonly float whatever;
}
private static void Main(string[] args)
{
byte[] data =;//from file stream or whatever;
//usage
FileEntry entry = RawDataToObject<FileEntry>(data);
}
Wrapping your FileStream with a BinaryReader will give you dedicated Read*() methods for primitive types:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx
Out of my head, you could probably mark your struct with [StructLayout(LayoutKind.Sequential)] (to ensure proper representation in memory) and use a pointer in unsafe block to actually fill the struct C-style. Going unsafe is not recommended if you don't really need it (interop, heavy operations like image processing and so on) however.
Not a full answer (it's been covered I think), but a specific note on the filename:
The Char type is probably not a one-byte thing in C#, since .Net characters are unicode, meaning they support character values far beyond 255, so interpreting your filename data as Char[] array will give problems. So the first step is definitely to read that as Byte[12], not Char[12].
A straight conversion from byte array to char array is also not advised, though, since in binary indices like this, filenames that are shorter than the allowed 12 characters will probably be padded with '00' bytes, so a straight conversion will result in a string that's always 12 characters long and might end on these zero-characters.
However, simply trimming these zeroes off is not advised, since reading systems for such data usually simply read up to the first encountered zero, and the data behind that in the array might actually contain garbage if the writing system doesn't bother to specifically clear its buffer with zeroes before putting the string into it. It's something a lot of programs don't bother doing, since they assume the reading system will only interpret the string up to the first zero anyway.
So, assuming this is indeed such a typical zero-terminated (C-style) string, saved in a one-byte-per-character text encoding (like ASCII, DOS-437 or Win-1252), the second step is to cut off the string on the first zero. You can easily do this with Linq's TakeWhile function. Then the third and final step is to convert the resulting byte array to string with whatever that one-byte-per-character text encoding it's written with happens to be:
public String StringFromCStringArray(Byte[] readData, Encoding encoding)
{
return encoding.GetString(readData.TakeWhile(x => x != 0).ToArray());
}
As I said, the encoding will probably be something like pure ASCII, which can be accessed from Encoding.ASCII, standard US DOS encoding, which is Encoding.GetEncoding(437), or Windows-1252, the standard US / western Europe Windows text encoding, which you can retrieve with Encoding.GetEncoding("Windows-1252").
How can I convert a System.GUID (in C#) to a string in decimal base (aka to a huge, comma delimited integer, in base ten)?
Something like 433,352,133,455,122,445,557,129,...
Guid.ToString converts GUIDs to hexadecimal representations.
I'm using C# and .Net 2.0.
Please be aware that guid.ToByteAray() will NOT return an array that can be passed to BigInteger's constructor. To use the array a re-order is needed and a trailing zero to ensure that Biginteger sees the byteArray as a positive number (see MSDN docs). A simple but less performing function is:
private static string GuidToStringUsingStringAndParse(Guid value)
{
var guidBytes = string.Format("0{0:N}", value);
var bigInteger = BigInteger.Parse(guidBytes, NumberStyles.HexNumber);
return bigInteger.ToString("N0", CultureInfo.InvariantCulture);
}
As Victor Derks pointed out in his answer, you should append a 00 byte to the end of the array to ensure the resulting BigInteger is positive.
According to the the BigInteger Structure (System.Numerics) MSDN Documentation:
To prevent the BigInteger(Byte[]) constructor from confusing the two's complement representation of a negative value with the sign and magnitude representation of a positive value, positive values in which the most significant bit of the last byte in the byte array would ordinarily be set should include an additional byte whose value is 0.
(see also: byte[] to unsigned BigInteger?)
Here's code to do it:
var guid = Guid.NewGuid();
return String.Format("{0:N0}",
new BigInteger(guid.ToByteArray().Concat(new byte[] { 0 }).ToArray()));
using System;
using System.Numerics;
Guid guid = Guid.NewGuid();
byte[] guidAsBytes = guid.ToByteArray();
BigInteger guidAsInt = new BigInteger(guidAsBytes);
string guidAsString = guidAsInt.ToString("N0");
Note that the byte order in the byte array reflects endian-ness of the GUID sub-components.
In the interest of brevity, you can accomplish the same work with one line of code:
string GuidToInteger = (new BigInteger(Guid.NewGuid().ToByteArray())).ToString("N0");
Keep in mind that .ToString("N0") is not "NO"... see the difference?
Enjoy
It is written
byte[][] getImagesForFields(java.lang.String[] fieldnames)
Gets an array of images for the given fields.
On the other hand, as long as I use the method in the web application project built on asp.net 2.o using c#;
the provided web method declared above, returns sbyte;
Have a look my code below;
formClearanceService.openSession(imageServiceUser);
formClearanceService.prepareInstance(formId);
byte[][] fieldImagesList = formClearanceService.getImagesForFields(fieldNames);
formClearanceService.closeSession();
thus I get the following error: Cannot implicitly convert type 'sbyte[]' to 'byte[][]'
So now,
1- should I ask the web service provider what is going on?
or
2- any other way that can use the sbyte as I was suppose to use byte[][] like following using:
byte[] ssss = fieldImagesList [0]..
Java has signed bytes, so that part is correct in some ways (although unsigned bytes are more natural) - but it is vexing that it is returning a single array rather than a jagged array. I expect you're going to have to compare some data to see what you have received vs what you expected.
But changing between signed and unsigned can be as simple as:
sbyte[] orig = ...
byte[] arr = Array.ConvertAll(orig, b => (byte)b);
or (faster) simply:
sbyte[] orig = ...
byte[] arr = new byte[orig.Length];
Buffer.BlockCopy(orig, 0, arr, 0, orig.Length);