开发者

CString wrote as an unsigned char

I am building an application to read and write to a microchip`s memory.

I need to pass an unsigned char array that has 4 fixed bytes and 2 variable bytes so suposing I want to read memory bank 000开发者_如何学C4 I will pass

unsigned char sRequest[32]={0};
sRequest[0] = 0x02; //FIXED
sRequest[1] = 0x1F; //FIXED
sRequest[2] = 0x0A; //FIXED
sRequest[3] = 0x20; //FIXED
sRequest[4] = 0x00; //VARIABLE
sRequest[5] = 0x04; //VARIABLE

I want to put 2 CEdit boxes for the user to input that variable memory bank, so it would write 0x00 on first CEdit and 0x04 on second one.

so my question is, how can I translate eacho of these inputs on an unsigned char so I can set it on my sRequest variable?

thanks in advance (thanks dave, mistyped bytes into bits, fixed now)


Actually, I wouldn't do that with free-form text entry at all - it would be too much of a bother to do validation (being inherently lazy). If there's a chance to restrict what your user is able to give you as early as possible, you should take it :-)

I would provide drop-down boxes for the values, each nybble so that the quantity to select from is not too onerous. Something like (and with apologies for my graphical skills):

               +---+-+  +---+-+  +---+-+  +---+-+
Enter value 0x | 0 |V|  | 0 |V|  | 0 |V|  | 4 |V|
               +---+-+  +---+-+  +---+-+  +---+-+
               |>0<|
               | 1 |
               | 1 |
               | 2 |
               | 3 |
               : : :
               | E |
               | F |
               +---+

For the values in the listbox, I would set the item data to be 0 through 15 inclusive (for the items 0 through F).

That way, you could get a single byte value with something like:

byte04 = listboxA.GetItemData(listboxA.GetCurSel()) * 16 +
         listboxB.GetItemData(listboxB.GetCurSel());

byte05 = listboxC.GetItemData(listboxC.GetCurSel()) * 16 +
         listboxD.GetItemData(listboxD.GetCurSel());

If you must use a less restrictive input method, C++ provides:

int stoi (const string& str, size_t *idx = 0, int base = 10);

(and stol/stoul for signed and unsigned longs) to allow you to convert C++ strings to integral types.

For your purposes. you'll probably have to detect and strip a leading 0x (along with possibly leading/trailing spaces and so forth) which is why I suggest the restrictive route as a better option.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜