Convert hex to float

In Python 3: >>> import struct >>> struct.unpack(‘!f’, bytes.fromhex(‘41973333’))[0] 18.899999618530273 >>> struct.unpack(‘!f’, bytes.fromhex(‘41995C29’))[0] 19.170000076293945 >>> struct.unpack(‘!f’, bytes.fromhex(‘470FC614’))[0] 36806.078125 In Python 2: >>> import struct >>> struct.unpack(‘!f’, ‘41973333’.decode(‘hex’))[0] 18.899999618530273 >>> struct.unpack(‘!f’, ‘41995C29’.decode(‘hex’))[0] 19.170000076293945 >>> struct.unpack(‘!f’, ‘470FC614’.decode(‘hex’))[0] 36806.078125

Save and retrieve image (binary) from SQL Server using Entity Framework 6

Convert the image to a byte[] and store that in the database. Add this column to your model: public byte[] Content { get; set; } Then convert your image to a byte array and store that like you would any other data: public byte[] ImageToByteArray(System.Drawing.Image imageIn) { using(var ms = new MemoryStream()) { imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); … Read more

What is the best way to handle versioning using JSON protocol?

The key to versioning JSON is to always add new properties, and never remove or rename existing properties. This is similar to how protocol buffers handle versioning. For example, if you started with the following JSON: { “version”: “1.0”, “foo”: true } And you want to rename the “foo” property to “bar”, don’t just rename … Read more

Why doesn’t C have binary literals?

According to Rationale for International Standard – Programming Languages C ยง6.4.4.1 Integer constants A proposal to add binary constants was rejected due to lack of precedent and insufficient utility. It’s not in standard C, but GCC supports it as an extension, prefixed by 0b or 0B: i = 0b101010; See here for detail.

How to read file binary in C#?

Quick and dirty version: byte[] fileBytes = File.ReadAllBytes(inputFilename); StringBuilder sb = new StringBuilder(); foreach(byte b in fileBytes) { sb.Append(Convert.ToString(b, 2).PadLeft(8, ‘0’)); } File.WriteAllText(outputFilename, sb.ToString());

Parsing a binary file. What is a modern way?

If it is not for learning purpose, and if you have freedom in choosing the binary format you’d better consider using something like protobuf which will handle the serialization for you and allow to interoperate with other platforms and languages. If you cannot use a third party API, you may look at QDataStream for inspiration … Read more

Is it possible to program in binary?

Of course. It’s more commonly called machine code. It’s basically assembly language without the mnemonic devices. Someone who knows assembly very well could program in machine code with additional effort, referring to opcode listings (e.g. x86) as needed. Would I do it? No. Even assembly is only useful in rare circumstances, and there’s no reason … Read more

Embedding binary blobs using gcc mingw

In your C program remove the leading underscore: #include <stdlib.h> #include <stdio.h> extern char binary_input_txt_start[]; int main (int argc, char *argv[]) { char *p; p = binary_input_txt_start; return 0; } C compilers often (always?) seem to prepend an underscore to extern names. I’m not entirely sure why that is – I assume that there’s some … Read more

How to get binary representation of Int in Kotlin

Method 1: Use Integer.toBinaryString(a) where a is an Int. This method is from the Java platform and can be used in Kotlin. Find more about this method at https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toBinaryString(int) Note: This method works for both positive and negative integers. Method 2: Use a.toString(2) where a is an Int, 2 is the radix Note: This method … Read more