1)What's the safest way to receive data from a php script and return to an exe?

2)If I have lets say

char Hi[4] = "3E2C";

and I wanted to put this into a BYTE Bye[2]; how would I do this so that Bye[0] = 0x3E and Bye[1] = 0x2C?

Thanks guys!

Dani AI

Generated

A compact, practical follow‑up that fills gaps in the thread.

The declaration char Hi[4] = "3E2C"; in the first post is incorrect: string literals include a terminating NUL, so that literal needs 5 bytes. Use char Hi[] = "3E2C"; or explicitly allocate one extra byte. That avoids buffer overruns before conversion.

A robust, easy-to-read way to turn two hex characters into a byte is to parse each 2‑char substring as base‑16 and store the result in an unsigned byte container. std::stoul (or std::from_chars for a no‑throw approach) works well and automatically handles upper/lowercase hex. Example:

#include <string>
#include <vector>
#include <cstdint>

std::vector<uint8_t> hexToBytes(const std::string& s) {
    std::vector<uint8_t> out;
    if (s.size() % 2 != 0) throw std::invalid_argument("odd length");
    out.reserve(s.size()/2);
    for (size_t i = 0; i < s.size(); i += 2) {
        unsigned long v = std::stoul(s.substr(i,2), nullptr, 16);
        out.push_back(static_cast<uint8_t>(v));
    }
    return out;
}

For C++ <-> PHP communication, prefer a simple HTTP(S) API over fragile file or raw exec approaches. Key recommendations: use HTTPS with certificate verification, authenticate (API keys, HMAC, or mutual TLS), send structured payloads (JSON for text, base64 or application/octet-stream for binary), and use a mature client library (libcurl) on the C++ side and native cURL/filters on PHP. If both processes are local and you need better performance or tighter security, use Unix domain sockets / Windows named pipes or a message queue instead of exposing an executable to the web. (See for IPC ideas.)

Quick troubleshooting and safety notes: validate input lengths and characters before parsing, prefer unsigned types (uint8_t) for raw bytes, log hex dumps when testing, set timeouts and retries on network calls, and run any server‑side executable with least privileges. Credit to and for pointing toward parsing/encoding; the code above gives a straightforward, safe parser and practical transport guidance.

Useful references: libcurl (https://curl.se/libcurl/) and C++ parsing utilities (https://en.cppreference.com/w/cpp/string/basic_string/stoul).

Recommended Answers

All 10 Replies

Your Hi variable

char Hi[4] = {'3','E','2','C'};

is four bytes with the values of(in ascii)
0x33 = '3'
0x45 = 'E'
0x32 = '2'
0x43 = 'C'

and this (in ascii)

Bye[0] = 0x3E and Bye[1] = 0x2C

would equate to

Bye[0] = '>' and Bye[1] = ','

I think your mixing up characters and their acsii values.

no those are the hex values that should be placed in the byte accordingly.


char Hi[4] = "3E2C";
needs to be converted into:
Bye[0] = 0x3E;
Bye[1] = 0x2C;

no those are the hex values that should be placed in the byte accordingly.


char Hi[4] = "3E2C";
needs to be converted into:
Bye[0] = 0x3E;
Bye[1] = 0x2C;

Firstly, you got to read the Hi array 2 characters at a time. For the first character, bit shift it to the left 4 positions using the operator (<<). Using the results, apply the bitwise OR operation (|) with the second character. You should get the result in a single byte.
For bitwise operations, you can refer to this link

Jesus, I don't understand :(

Also whats the safest c++(exe) to php and php to c++(exe) communication

Sorry for bumping after 9 hrs but I'm going back to school in 2 days and I will not be able to code so I want to finish this :(

I don't known much about PHP, but can you use a pipe or set up a socket connection?

Maybe this will help.

#include <iostream>
using namespace std;

int main(){
 char sample[] = "3E";
 int result[1] = {};

 cout << "Encoding : " << sample << endl;

 result[0] = (sample[0] << 8) | (sample[1]) ; //encode

 cout << "Encoded value : " << hex << result[0] << endl;
 cout << char( (result[0] & 0xff00) >> 8) << endl; //decode top 8 bits
 cout << char(result[0] & 0x00ff) << endl; //decode bottom 8 bits
 return 0;
}

This was awesome! But, it needs to be placed inside a byte
BYTE[0] = 0x3E;

And what's the problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.