Help needed converting string hash values into binary...
-
Hi there. I'm writing a hash file converter to convert a set of file hashes from one vendors format to another. As input I have a 32 byte long string containing a hash value stored in a byte[32] array: 6A87A34023132CF8F6D7FDFFB269E3FA I want to convert this into a 16 byte value such that the hex representation is the same, ie: byte[16] = {0x6A,0x87,0xA3 ... }. I hope this makes sense, I'm really struggling with this as my binary skills are not so sharp... Any assistance woud be greatly appreciated. Kind regards, John.
-
Hi there. I'm writing a hash file converter to convert a set of file hashes from one vendors format to another. As input I have a 32 byte long string containing a hash value stored in a byte[32] array: 6A87A34023132CF8F6D7FDFFB269E3FA I want to convert this into a 16 byte value such that the hex representation is the same, ie: byte[16] = {0x6A,0x87,0xA3 ... }. I hope this makes sense, I'm really struggling with this as my binary skills are not so sharp... Any assistance woud be greatly appreciated. Kind regards, John.
No, it doesn't really make sense... What is a "32 byte long string"? A string consists of characters, not bytes. It looks like you have a string containing 32 characters, but that will occupy 64 bytes. Have you really stored the string in a byte[32] array? How? Have you encoded the string? Then you should decode the string using the same encoding. Once you have the data as a string, you can split it up into 16 separate strings using the Substring method. Then use the Int32.Parse method with NumberStyles.AllowHexSpecifier to parse each two character string into a byte value.
--- Year happy = new Year(2007);
-
No, it doesn't really make sense... What is a "32 byte long string"? A string consists of characters, not bytes. It looks like you have a string containing 32 characters, but that will occupy 64 bytes. Have you really stored the string in a byte[32] array? How? Have you encoded the string? Then you should decode the string using the same encoding. Once you have the data as a string, you can split it up into 16 separate strings using the Substring method. Then use the Int32.Parse method with NumberStyles.AllowHexSpecifier to parse each two character string into a byte value.
--- Year happy = new Year(2007);
Yup, you're right - it's a 32 character long string, however it does only occupy 32 bytes - it's not unicode. No encoding required - it's pure ASCII and is stored as such. Anyway after much head scratching, I've got the following code to work: // sHash[32] is a byte array length 32 // eg: the hex value 'A6' is split into sHash[0] & sHash[1] // the following code will amalgamate this character data into a // byte array length 16 int l = 0, iFirst, iSecond; for (int k = 0; k < 32; k += 2) { if (sHash[k] > '9') iFirst = (sHash[k] - 'A') + 10; else iFirst = (sHash[k] - '0'); if (sHash[k+1] > '9') iSecond = (sHash[k+1] - 'A') + 10; else iSecond = (sHash[k+1] - '0'); EnHash[l++] = Convert.ToByte(((iFirst * 16) + iSecond)); } Thanks for your comments above - it's an interesting idea to use int32.parse - I'll give that a go just to see how that works - ultimately it's not too diferent from what I worked out. Kind regards, John.
-
Yup, you're right - it's a 32 character long string, however it does only occupy 32 bytes - it's not unicode. No encoding required - it's pure ASCII and is stored as such. Anyway after much head scratching, I've got the following code to work: // sHash[32] is a byte array length 32 // eg: the hex value 'A6' is split into sHash[0] & sHash[1] // the following code will amalgamate this character data into a // byte array length 16 int l = 0, iFirst, iSecond; for (int k = 0; k < 32; k += 2) { if (sHash[k] > '9') iFirst = (sHash[k] - 'A') + 10; else iFirst = (sHash[k] - '0'); if (sHash[k+1] > '9') iSecond = (sHash[k+1] - 'A') + 10; else iSecond = (sHash[k+1] - '0'); EnHash[l++] = Convert.ToByte(((iFirst * 16) + iSecond)); } Thanks for your comments above - it's an interesting idea to use int32.parse - I'll give that a go just to see how that works - ultimately it's not too diferent from what I worked out. Kind regards, John.
You did it the hard way, you could use System.Globalization.NumberStyles example: int i=int.Parse("12",NumberStyles.AllowHexSpecifier); sets i to 18 (or 0x12). So you simply could pass a two-char substring to byte.Parse and reduce your for-loop body to a single statement. :)
Luc Pattyn
-
You did it the hard way, you could use System.Globalization.NumberStyles example: int i=int.Parse("12",NumberStyles.AllowHexSpecifier); sets i to 18 (or 0x12). So you simply could pass a two-char substring to byte.Parse and reduce your for-loop body to a single statement. :)
Luc Pattyn
-
That's very cool Luc, thank you. I think I'll implement that as I guess it will make the program run much faster! Kind regards, John.
Okay, having implemented your code I've got a saving of three seconds with 2755 hashes processed. My code took 2 mins 33 secs to process, whereas yours took 2 minutes 30 - that's 18.3 hash conversions per second! This is a small sample file so I'm sure that in the real world application of this the savings will be very significant. Many thanks for your help. Kind regards, John.