How to convert a piece of data into a hex format

Source: Internet
Author: User
How can we convert a piece of data into a hex format,

This is the data in a blob. now I use stream_get_contents to extract the data and convert it directly into the right-side ascii format in the graph, but it is not based on ascii in the protocol. How can I retrieve or convert it into a hex string?


Reply to discussion (solution)

First, clarify one thing
The ASCII form on the right and HEX form in the middle of the figure are the same thing (we call it a binary string)
These two representation methods are not used for actual data communication, and binary strings are still used for communication.

$s = 'ABCDEFGHIJKLMNOP';echo strtoupper(join(' ', str_split(bin2hex($s), 4)));
4142 4344 4546 4748 494A 4B4C 4D4E 4F50

I feel that the function used for the value is incorrect, but I have little experience. What can be used to make the original data into strings?

First, clarify one thing
The ASCII form on the right and HEX form in the middle of the figure are the same thing (we call it a binary string)
These two representation methods are not used for actual data communication, and binary strings are still used for communication.

$s = 'ABCDEFGHIJKLMNOP';echo strtoupper(join(' ', str_split(bin2hex($s), 4)));
4142 4344 4546 4748 494A 4B4C 4D4E 4F50


Bamboo is big enough to come ~~~ I understand this now. in our protocol, we use a section of hex to parse data. for example, the second row of DD07 is converted to decimal followed by 221 and 7, later, the agreement stipulates that 221 + 7*256 = 2013. But now I have taken this character directly into the ascii format, and the protocol will not work. my problem is that, how can I get it out of the hex format?

First, clarify one thing
The ASCII form on the right and HEX form in the middle of the figure are the same thing (we call it a binary string)
These two representation methods are not used for actual data communication, and binary strings are still used for communication.

$s = 'ABCDEFGHIJKLMNOP';echo strtoupper(join(' ', str_split(bin2hex($s), 4)));
4142 4344 4546 4748 494A 4B4C 4D4E 4F50

Thank you! I have solved this problem.

HEX is used only for intuitive description.

If the hexadecimal format of a string is DD07
You can write in php.
$ S = chr (0xdd). chr (0x07 );
Echo ord ($ s {0}; /// 221
Echo ord ($ s {1}; // 7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.