how get UnsignedBytes in php? In java very easy: readUnsignedByte (Java)
There's no byte datatype in php, however, php character type is 8-bit, that is, you can use char/string type to store bytes. fgetc reads a character from input and ord returns a numeric byte value, given a character.
Php does not have a byte type. Maybe an alternative is to create a byte array to represent your byte value.
Related
I want to check the size of a string that can contain any type of data.
I have checked strlen and mb_strlen but I am unsure about the differences relating to different data contents.
Some background : what I need to do in the end is cut the string in chunks to serialize it and store it in chunks (being able to restore afterwards). Chunks always have the same size (32Kb) and contain a serialized object with data and the part of the string that I cut, so I need the exact size of the string to be able to do that.
From PHP's manual:
Note:
strlen() returns the number of bytes rather than the number of characters in a string.
By contrast, mb_strlen will take character encoding into consideration. It returns the number of actual characters as defined in the character encoding in the string. For multibyte/variable byte character encodings, strlen can/will be bigger than mb_strlen.
mb_strlen may also return FALSE if you specify a character encoding to which the string being tested doesn't conform.
I'm trying to convert a string to UTF8, on both obj-c and php.
I get different results:
"\xd7\x91\xd7\x93\xd7\x99\xd7\xa7\xd7\x94" //Obj-C
"\u05d1\u05d3\u05d9\u05e7\u05d4" //PHP
Obj-C code:
const char *cData = [#"בדיקה" cStringUsingEncoding:NSUTF8StringEncoding]
PHP code:
utf8_encode('בדיקה')
This difference breaks my hash algorithm that follows.
How can I make the two strings encoded the same way? Should I change the obj-c\php ?
Go to http://www.utf8-chartable.de/unicode-utf8-table.pl
In the combo box switch to “U+0590 … U+5FF Hebrew”
Scroll down to “U+05D1” which is the rightmost character of your input string.
The third column shows the two UTF-8 bytes: “d7 91”
If you keep looking you will see that the PHP and the Objective-C are actually the same. The “problem” you are seeing is that while PHP uses an Unicode escape (\u), Objective-C uses direct byte hexadecimal escapes (\x). Those are only visual representations of the strings, the bytes in memory are actually the same.
If your hash algorithm deals with bytes correctly, you should not see differences.
What are you using to do the encoding on PHP? It looks like you're generating a UTF-16 string.
Try utf8_encode() and see if that gives better results.
Is there a native or inexpensive way to check for the length of a string in bytes in PHP?
See http://bytes.com/topic/php/answers/653733-binary-string-length
Relevant part:
"In PHP, like in C, the string ends with a zero-character, '\0', (char)
0, null-terminator, null-byte or whatever you like to call it."
No, that's not the case - PHP strings are stored with both the length and the
data, unlike C strings that just has one pointer and uses a terminator. They're
"binary-safe" - NUL doesn't terminate the string.
See the definition of zvalue_value in zend.h; the string part has both a "char
*val" and "int len".
Problems would start if you're using the mbstring.func_overload, which changes
how strlen() and the other functions work, and does try and treat strings as
strings of characters in a specific encoding rather than a string of bytes.
This is not the normal PHP behaviour.
The answer is that strlen should return the number of bytes regardless of the content of the string. For multi-byte character strings, you get the wrong number of characters, but the right number of bytes. However, you need to be certain you're not using the mbstring overload, which changes how strlen behaves.
In the event that you have mbstring overload set or your are developing for the platforms where you are unsure about this setting you can do the following:
$len=strlen(bin2hex($data))/2;
The reason why this works is that in Hex you are guaranteed to get 2 characters for all bytes that come from bin2hex (it returns two chars even for the initial binary 0).
Note that it will use significantly more resources than a normal strlen (afterall, so you should definitely not do that to the large amount of data if it's not absolutely necessary.
On php.org, someone was nice enough to create this function. Just multiply by 8 and you've got however many bits were in that string, as the function returns bytes.
The length of a string (textual data) is determined by the position of the NULL character which marks the end.
In case of binary data, NULL can be and often is in the middle of data.
You don't check the length of binary data. You have to know it beforehand. In your case, the length is 16 (bytes, not bits, if it is UUID).
As far as UUID validity is concerned, any 16-byte value is a valid UUID, so you are out of luck there.
so I have my php API (html Get api for Flash builder and C# apps). So if you want to submit data to it you use string like
http://localhost/cms/api.php?method=someMethod&string=Your_String
If there are english letters in it its ok. But what if I need to pass UTF-8 string like this Русское Имя to my api what shall I do?
Use the rawurlencode() function. It will encode your string byte by byte, but it is not a problem, since UTF-8 is an ASCII aware representation. All code positions below 128 are identical to the ASCII one, all code positions above 127 are represented with byte sequences which are all between 128 and 255, so you will not have problems with it. The input wrapper should decode the parameters into your $_REQUEST array properly.
I am trying to send something to serial port (r232) with PHP.
I am using this class: http://www.phpclasses.org/browse/package/3679.html
The problem is that I am allowed to send only 1 byte.
But if I send something like "1", I am actually sending 49 (ASCII for 1).
Instead of send("1"), I tried with send(1) but it is no good, because this is integer which has 2 bytes.
So is there a way to send a "real" char, not ASCII equivalent?
The chr() function returns a character given by the integer for the corresponding ascii character.
It looks like the library is expecting characters as input. If you need to send the character which would encode to 0x01, you just send "\001". The function chr() would convert characters to integer values and would be no use here.
One more thing: The byte size of integers depends on the underlying system and is mostly 4 bytes.
I'm not sure what you are trying to accomplish. Are you trying to to send the integer 1? Not being familiar with the class, have you tried to give just the value 1 as an argument? If that doesn't work, try to wrap it with the chr() function.