Non ASCII char in PHP? - php

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.

Related

ASCII encoded unicode to SQL statement in PHP with mb_convert_encoding

I'm working with a file type that is comprised of ASCII characters only. Other characters are encoded. Two byte characters are preceded with \X2\ and followed by \X0\ and four byte characters use \X4\ and \X0\ . I can find these easily enough but I'm not sure how to then handle them.
e.g. the character ø is given as \X2\00F8\X0\.
My whole string I'm looking for is 100 mm\X2\00F8\X0\.
How can I get that to a string that I could then say insert into my DB?
I figure I need to use mb_convert_encoding()
But I'm not sure which encoding I want to go to and from.
Any ideas?
Put simply I have "100 mm\X2\00F8\X0\" and I want to output that as "100 mmø"
It depends on SQL server you are using. For example for MySQL you can use mysqli_real_escape_string
This method automatically takes encoding from your sql connection so it is safe to use.
I would pass raw value 100m ø.
so all I needed was hex2bin('00F8')
Then utf8_encode(...) to get it from utf-16 to utf 8

Check for binary string length?

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.

PHP: Very simple Encode/Decode string

Is there any PHP function that encodes a string to a int value, which later I can decode it back to a string without any key?
Sure, you can convert strings to numbers and vice versa. Consider:
$a = "" + 1
gettype($a) // integer
$b = "$a"
gettype($b) // string
You can also do type casting with settype().
If I misunderstood you and you want to encode arbitrary strings, consider using base64_encode() and bas64_decode(). If you want to convert the base 64 string representation to a base 10 integer, simply use base_convert().
And int has 4 or 8 bytes depending on the platform, and each character in a string is one byte (or more depending on encoding). So, you can only encode very small strings to integers, which basically makes the answer to your question: no.
What do you want to accomplish?
I would suspect not, since there are far more possible string combinations than integers within the MAX_INT.
Does it have to be an integer?
i'm convinced that what you think you want to do is not really what you want to do. :-) this just sounds like a silly idea. As another user has asked before:) what do you need this for? What are your intentions?
Well now that you mentioned that numbers and a-z letter are acceptable, then I have one suggestion, you could loop through the individual letters' ordinal value and display that as a two-digit hexadecimal. You can then convert these hexadecimals back to the ordinal values of the individual characters. Don't know what kind of characters are you about to encode, possibly you will need to use 4-characters per letter (e.g. String Peter would become 00700065007400650072 ) Well... have fun with that, I still don't really see the rationale for doing what you're doing.
op through the individual letters' ordinal value and display that as a two-digit hexadecimal. You can then convert these hexadecimals back to the ordinal values of the individual characters. Don't know what kind of characters are you about to encode, possibly you will need to use 4-characters per letter (e.g. String Peter would become 00700065007400650072 ) Well... have fun with that, I still don't really see the
There is no function for PHP but I recently wrote a class to encrypt and decrypt a string in PHP. You can look at it at: https://github.com/Lars-/PHP-Security-class

method readUnsignedByte in PHP

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.

How to pass UTF8 string into your PHP HTML API?

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.

Categories