Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# How to convert from char to unsinged char

Posted on 2004-09-01
Medium Priority
238 Views
Hi, Expert,

I need user enter decimal number into edit box, e.g. 0 to 511, say 511 (this is ascii number), I need conver 511 into hex number into variable unsigned char array[0] = 0xff, unsigned char array[1] = 0x01 since 511d is 1ffh.
I can transmit this array out through serial port.

Thanks,
Ben
0
Question by:benlian888
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Author Comment

ID: 11959229
I am using VC++  6.0
0

LVL 55

Expert Comment

ID: 11959688
I will exploit some WINAPI macros in my solution:

first you have to conver the hex string  to a numeric variable:

DWORD h;
sscanf(yourcontroltext, "%x", &i);

but h is a 32-bit value. Since I assume you want to transmit value, assign it to another variable:
WORD n = LOWORD(h);

then copy binary content to a char array in the order you want:

char array[2];
array[0] = HIBYTE(n);
array[1] = LOBYTE(n);

0

LVL 7

Expert Comment

ID: 11960481
it should be
sscanf(yourcontroltext, "%x", &h);
0

LVL 19

Expert Comment

ID: 11960775
>> first you have to conver the hex string  to a numeric variable

It is not a hex string.  You want a %d format rather than a %x:

sscanf(yourcontroltext, "%d", &h);

Hex string would be 0x1ff or at least 1ff.

Alternative code if you are using Winsock (need it for htons):

unsigned short val = (unsigned short)::atoi(numText);
val = ::htons(val);
unsigned char *bytes = (unsigned char*)&val;
// And optionally, if you really want the bytes in a real array...
char data[2] = { bytes[0], bytes[1] };
0

LVL 14

Accepted Solution

wayside earned 1200 total points
ID: 11967165
I like to use bitmasks directly instead of the macros, and atoi() is easier than sscanf:

int number = atoi(yourcontroltext);

char data[2];

data[0] = number & 0xff;
data[1] = (number & 0xff00) >> 8;
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
###### Suggested Courses
Course of the Month6 days, 16 hours left to enroll