Solved

How to convert from char to unsinged char

Posted on 2004-09-01
7
223 Views
Last Modified: 2013-11-20
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
Comment
Question by:benlian888
7 Comments
 

Author Comment

by:benlian888
Comment Utility
I am using VC++  6.0
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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

by:vijay_visana
Comment Utility
it should be
   sscanf(yourcontroltext, "%x", &h);
0
 
LVL 19

Expert Comment

by:drichards
Comment Utility
>> 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

by:
wayside earned 300 total points
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now