1

私はC++で次のコードを持っています。これはled_pwm16進数の変数を取り、それを string に変換することになっていますled_pwm_string

long int led_pwm=0x0a;

std::ostringstream ostr;
ostr << std::hex << led_pwm; //use the string stream just like cout,
                             //except the stream prints not to stdout 
                             //but to a string.

std::string led_pwm_string = ostr.str(); //the str() function of the stream
                                         //returns the string

このコードで私が抱えている唯一の問題は、 と の間led_pwmの任意の値がの 1 桁に変換されることです。これは後で私に問題を引き起こします。0x000x0aled_pwm_string

あらゆる状況で、led_pwm_string常に 2 桁の文字列を含むようにしたいと考えています。したがって、led_pwm(0x01たとえば) の場合、だけでなく になりled_pwm_stringます。011

4

1 に答える 1