2012-11-09 2 views
0

나는이 일을 위해 다양한 방법을 시도하면서 벽에 머리를 두드 리고 있었지만 그 중 아무 것도 제대로 작동하지 않는 것 같습니다.C++에서 문자열과 int의 concatanation. ASCII에서 HEX 로의 변환

주석 처리 된 행은 C++의 코드이며 C++로 다시 작성하려고합니다. 도움을 주시면 감사하겠습니다.

void sendCommand(string command) 
{ 
    //Convert.ToString((8 * startBit) + "4" + command); 
    char buffer[50]; 
    sprintf(buffer, "%d", (8 * startBit)); 
    motor.printf("sendBuffer: %d\r\n", buffer); 
    startBit = 1 - startBit; 
    motor.printf("%s%c%s\n\r", 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; 

    */ 
} 

감사

+0

이것 역시 'C#'태그를 사용해야합니다. – Ian

+0

C# 주석을 명확하게 할 수 있습니까? //Convert.ToString((8 * startBit) + "4"+ command); - Convert.ToString (8 * startBit) + "4"+ 명령을 의미합니까? –

답변

0

나는 당신의 C# 코드가 수행 정확히 모르지만 두 번째

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

에 대한 멀리 백만 마일을 보이지 않는다. 처음의 경우와 유사하게

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

은 원하는대로 가깝습니다.

+0

iostreams를 사용하여 모든 것을하기를 원했지만 원본 소프트웨어가 printf (멤버 함수로 정의 된 경우조차도!)를 사용하기 때문에 동일한 스타일을 유지하고 사용합니다. sprintf (버퍼 "% d4 % s", 8 * startBit, command); –