I've played around some with the SM130 Mifare module from SonMicro and didn't have any problems. Maybe that's not the one you got though.
In general hexadecimal formatting is merely used as a notation on paper and in programming languages. If you write 0x80 in C# (and C/C++) the actual value will be 128 decimal as it would if you wrote 128 in the first place.
In C# you can use hexadecimal notation freely so you should not need any conversion - unless of course the hexadecimal notation is in a string, then you need to convert it.
For single bytes, this can be done quite easily back and forth using (in analogy with the above example) 128.ToString("X2") and byte.Parse("0x80", NumberStyles.AllowHexSpecifier) functions respectively.
I don't know of a smart way to convert whole strings, but you could of course always traverse the array and assemble a string as you go using the mentioned functions.
This methods below are supposed to translate byte arrays into strings with 8 bit space separated hexadecimal numbers and vice versa. This is from the top of my head so you may need to alter it a bit if it does not compile as is:
public class HexConversion{ private static string _hex = "0123456789ABCDEF"; public static string toHex(byte[]{ string s = ""; for(int i = 0; i < b.length; i++, s += " ") s += _hex[b[i] >> 4] + _hex[b[i] & 0xf]; return s; } public static byte[] fromHex(string s) { string[] a = s.Split(' '); byte[] b = new byte[a.Length]; for(int i = 0; i < b.length; i++) b[i] = byte.Parse(a[i], NumberStyles.AllowHexSpecifier); return b; } }It could be you need to add the "0x" prefix in both the functions above. You would then use these functions like so:
// convert a regular string to a hex stringstring hex = HexConversion.toHex(Encoding.ASCII.GetBytes("Hello world"));// convert a hex string into a byte arraybyte[] bytes = HexConversion.fromHex(hex);// convert a byte array to a hex stringhex = HexConversion.toHex(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
Hanzibal, is there a chance you could share the reading and writing code for the SM130 module? I think I might go with this if I have some proven code to go with... thanks!
- Thomasraw likes this