c 中的字符串和int的condanation.从ASCII转换为十六进制

Concatanation of strings and ints in C++. Conversion from ASCII to HEX

本文关键字:ASCII 转换 十六进制 int 字符串 condanation      更新时间:2023-10-16

我一直在墙上撞到墙上,尝试了许多不同的方法来做到这一点,但它们似乎都没有正常工作。

评论的线是我试图在C 中重写的C#中的代码。任何帮助将不胜感激。

void sendCommand(string command)
{
    //Convert.ToString((8 * startBit) + "4" + command);
    char buffer[50];
    sprintf(buffer, "%d", (8 * startBit));
    motor.printf("sendBuffer: %drn", buffer);
    startBit = 1 - startBit;
    motor.printf("%s%c%snr", buffer, "4", command);
    return;
}
string strAcceleration(int acceleration)
{
    //string accelerationHex = acceleration.ToString("X");
    //accelerationHex = accelerationHex.PadLeft(8,'0');
    char buffer[50];
    sprintf(buffer, "%00000000X", acceleration);
    motor.printf("acc: %s", buffer);
    return buffer;
}
string strSpeed(int speed)
{
/*
    string speedHex = null;
    if (speed == 0) speedHex = "0";
    else if (speed > 0) speedHex = speed.ToString("X");
    else speedHex = 0xFFFFFFFF + speed.ToString("X");
    if(speedHex.Length == 1) speedHex = "0000000" + speedHex;
    if(speedHex.Length == 2) speedHex = "000000" + speedHex;
    if(speedHex.Length == 3) speedHex = "00000" + speedHex;
    return speedHex;
    */
}

谢谢

我不知道您的C#代码是什么,但是第二个

sprintf(buffer, "%08X", acceleration);

看上去没有一百万英里。同样对于第一个

std::ostringstream buffer;
buffer << (8 * startBit) <<  "4" << command;
std::string motor = buffer.str();

看起来它已经接近您想要的。