小编Ber*_*des的帖子

将int转换为C++中包含2位数的字符串

我在C++中有以下代码,它应该采用"led_pwm"十六进制变量,并将其转换为字符串"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
Run Code Online (Sandbox Code Playgroud)

我对此代码唯一的问题是,对于0x00和0x0a之间的任何led_pwm值,它会转换为"led_pwm_string"中的单个数字.这会在以后给我带来麻烦.

我希望,在每种可能的情况下,"led_pwm_string"始终包含2位数字符串.所以如果"led_pwm"是0x01(例如),那么"led_pwm_string"将是"01",而不仅仅是"1".

我希望我足够清楚!

c++ converter

0
推荐指数
1
解决办法
2491
查看次数

标签 统计

c++ ×1

converter ×1