将包含十进制数字的字符串转换为unsigned char

时间:2016-02-05 22:00:58

标签: c

我有一个包含十进制数的char(string)数组。 如何将其转换为unsigned char?

Assembly myAssembly = Assembly.GetExecutingAssembly();
Stream myStream = 
    myAssembly.GetManifestResourceStream(myAssembly.GetName().Name + 
    "pa_logo_notap.png");
Bitmap bmp = new Bitmap(myStream);
Clipboard.SetDataObject(bmp, true);
_xlSheet.Paste(logoRange, bmp);

1 个答案:

答案 0 :(得分:1)

unsigned char value = my_first_reg[0] - '0'; ASCII字符转换为其数字值:

 array:4 [▼
      0 => array:5 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
      ]
      1 => array:5 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
      ]
      2 => array:6 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
        5 => "some content"
      ]
      3 => array:5 [▼
        0 => "some content"
        1 => "some content"
        2 => "some content"
        3 => "some content"
        4 => "some content"
      ]
    ]

这是有效的,因为ASCII表中的数字是连续的:

    '0' = 0x30 = 48
    '1' = 0x31 = 49
    '2' = 0x32 = 50
    '3' = 0x33 = 51
    '4' = 0x34 = 52
    ...
    '9' = 0x39 = 57

以上是转换一个字符。如果您有更长的字符串,请考虑使用atoi()strtol()sscanf()或类似内容。