printf() formatting for hexadecimal

The # part gives you a 0x in the output string. The 0 and the x count against your “8” characters listed in the 08 part. You need to ask for 10 characters if you want it to be the same. int i = 7; printf(“%#010x\n”, i); // gives 0x00000007 printf(“0x%08x\n”, i); // gives 0x00000007 … Read more

How do I replace characters not in range [0x5E10, 0x7F35] with ‘*’ in PHP?

The following does the trick: $str = “some മനുഷ്യന്റെ”; echo preg_replace(‘/[\x{00ff}-\x{ffff}]/u’, ‘*’, $str); // some ********** echo preg_replace(‘/[^\x{00ff}-\x{ffff}]/u’, ‘*’, $str); // *****മനുഷ്യന്റെ The important thing is the u-modifier (see here): This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8. This modifier is available from PHP … Read more

Convert hex data string to NSData in Objective C (cocoa)

Code for hex in NSStrings like “00 05 22 1C EA 01 00 FF”. ‘command’ is the hex NSString. command = [command stringByReplacingOccurrencesOfString:@” ” withString:@””]; NSMutableData *commandToSend= [[NSMutableData alloc] init]; unsigned char whole_byte; char byte_chars[3] = {‘\0′,’\0′,’\0’}; for (int i = 0; i < ([command length] / 2); i++) { byte_chars[0] = [command characterAtIndex:i*2]; byte_chars[1] … Read more

Convert hex string to byte []

Convert hex to byte and byte to hex. public static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] data = new byte[len/2]; for(int i = 0; i < len; i+=2){ data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16)); } return data; } final protected static char[] hexArray = {‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’A’,’B’,’C’,’D’,’E’,’F’}; public static String … Read more

Large hex values with PHP hexdec

As said on the hexdec manual page: The function can now convert values that are to big for the platforms integer type, it will return the value as float instead in that case. If you want to get some kind of big integer (not float), you’ll need it stored inside a string. This might be … Read more