By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

Converting char* to char

P: n/a
Hi all,
I'm using C++ Builder5. I want to get data from an edit-box and
send it to a development kit. The dev-kit can only receive char
and no char*

here's a part of my code:

char* Data_byte = 0x00;

UCHAR outBuffer[64];
WORD outPacketSize;
outBuffer[0] = Data_byte;
...
...
void __fastcall TForm1::Data_EditChange(TObject *Sender)
{
Data_byte=Data_Edit->Text.c_str();
}

I get an error -> Can't convert char* to char. Does anyone
have a solution for my problem?
So I just need to get a byte from the edit-box and store this
in a variable.

thnx in advance,
Chris
Jul 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Chris Online wrote:
Hi all,
I'm using C++ Builder5. I want to get data from an edit-box and
send it to a development kit. The dev-kit can only receive char
and no char*

here's a part of my code:

char* Data_byte = 0x00;

UCHAR outBuffer[64];
WORD outPacketSize;
outBuffer[0] = Data_byte;
..
..
void __fastcall TForm1::Data_EditChange(TObject *Sender)
{
Data_byte=Data_Edit->Text.c_str();
}

I get an error -> Can't convert char* to char. Does anyone
have a solution for my problem?
So I just need to get a byte from the edit-box and store this
in a variable.

thnx in advance,
Chris


You will need to check the type of "Text" from the
"Data_Edit" control to see if the string type has a
method to access individual characters. If the
type was std::string, you could use:
{
Data_byte = Data_Edit->Text[0];
}
But alas, Borland may have its own string type.

You could also post this at a Borland newsgroup.
Check the C++ FAQ and welcome.txt below for
suggested newsgroups.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #2

P: n/a
Chris Online wrote:
char* Data_byte = 0x00;

UCHAR outBuffer[64];
WORD outPacketSize;
outBuffer[0] = Data_byte;


What are you trying to do here? What value are you trying to put into
outBuffer[0] and why?
Jul 22 '05 #3

P: n/a

"Chris Online" <cv*******@hotmail.com> wrote in message
news:87**************************@posting.google.c om...
Hi all,
I'm using C++ Builder5. I want to get data from an edit-box and
send it to a development kit. The dev-kit can only receive char
and no char*

here's a part of my code:

char* Data_byte = 0x00;
I'm assuming this is a mistype for

char Data_byte = 0x00;

UCHAR outBuffer[64];
WORD outPacketSize;
outBuffer[0] = Data_byte;
..
..
void __fastcall TForm1::Data_EditChange(TObject *Sender)
{
Data_byte=Data_Edit->Text.c_str();
}

I get an error -> Can't convert char* to char. Does anyone
have a solution for my problem?
So I just need to get a byte from the edit-box and store this
in a variable.

Well think about it. An edit box can contain multiple characters That's why
Data_Edit->Text is a string (probably, you didn't say). You want one
character, but which one? The first, the second, the last, the first one
that isn't a space, what exactly? It's not just a matter of how do I convert
a char* to a char, there is no universal way to do that, its a matter of
defining your problem a little more clearly.

FWIW you can index a string using the [] operator

Data_byte = Data_Edit->Text[0] // the first character from the string

thnx in advance,
Chris


john
Jul 22 '05 #4

P: n/a
> I'm using C++ Builder5. I want to get data from an edit-box and
send it to a development kit. The dev-kit can only receive char
and no char*

here's a part of my code:

char* Data_byte = 0x00;
This is the same as:
char *Data_byte = NULL;

ie. a null pointer. It does not point to any chars.
I have a feeling that what you meant was:
char Data_byte = 0x00;
ie. one byte, set to the end-of-string marker.

UCHAR outBuffer[64];
WORD outPacketSize;
outBuffer[0] = Data_byte;
..
..
void __fastcall TForm1::Data_EditChange(TObject *Sender)
{
Data_byte=Data_Edit->Text.c_str();
}

I get an error -> Can't convert char* to char. Does anyone
have a solution for my problem?
If you noted the line number of that error, you would have seen
that it refers to: outBuffer[0] = Data_byte;
because outBuffer[0] is a char, but Data_byte is a char* .
If you declare Data_byte as a char, as I suggested above, this
error will go away.
So I just need to get a byte from the edit-box and store this
in a variable.
Finally, change this line: Data_byte=Data_Edit->Text.c_str();

to:
Data_byte = *Data_Edit->Text.c_str();

In your existing code, this compiled correctly (c_str() gives a char*,
and Data_byte was a char*), but left you with a dangling pointer.
Jul 22 '05 #5

P: n/a
Hi,
I editted my program just like you said:

char Data_byte = 0x00;

UCHAR outBuffer[64];
WORD outPacketSize;
outBuffer[0] = Data_byte;

Data_byte = *Data_Edit->Text.c_str();

The program works much better now. There is just one
thing I need to know. When I insert a number in the
edit-box the send byte is (number + 30). How come?
When I insert 3 into the edit-box, 33 is send to Outbuffer
When I insert 8 into the edit-box, 38 is send to Outbuffer

I want to use the edit-box to let the user enter an
address or data. The user needs to enter this in HEX

for example:
The user wants to send the byte 0xAF to address 0xC3
First he enters the address. The program must
get "C3" from the edit-box and send it into Outbuffer[0]

Edit-box:
C3 AF

How can I read first C3 and secondly AF?

please ask if something isn't clear

kind regards,
Chris
Jul 22 '05 #6

P: n/a

"Chris Online" <cv*******@hotmail.com> wrote in message
news:87**************************@posting.google.c om...
Hi,
I editted my program just like you said:

char Data_byte = 0x00;

UCHAR outBuffer[64];
WORD outPacketSize;
outBuffer[0] = Data_byte;

Data_byte = *Data_Edit->Text.c_str();

The program works much better now. There is just one
thing I need to know. When I insert a number in the
edit-box the send byte is (number + 30). How come?
When I insert 3 into the edit-box, 33 is send to Outbuffer
When I insert 8 into the edit-box, 38 is send to Outbuffer

I want to use the edit-box to let the user enter an
address or data. The user needs to enter this in HEX

for example:
The user wants to send the byte 0xAF to address 0xC3
First he enters the address. The program must
get "C3" from the edit-box and send it into Outbuffer[0]


OK, now you've changed your problem description. You threw every one when
you said you need to get a byte from the edit box, when actually you wanted
to convert the characters in the edit box to a single byte value.

What you really want to do is convert a text representation (in hex) of a
byte value to the actual byte value. I.e. you are converting text to number.

There are number of different ways to do that. Try this

unsigned byte;
sscanf(Data_Edit->Text.c_str(), "%x", &byte);
outBuffer[0] = (UCHAR)byte;

Note that byte has type unsigned, don't make it any other type.

john
Jul 22 '05 #7

P: n/a
Hi,
What are you trying to do here? What value are you trying to put into
outBuffer[0] and why?


I'm trying to get an address and data from the edit-box. The user inserts
these 2. First I want to send the address via Outbuffer[0]. Secondly,
I want to send the data via Outbuffer[0]

The user had 2 edit-boxes. one for the address and one for the data.

Address Data
|_____| |_____|

Both bytes can varie between 0x00 and 0xFF.

rgds,
Chris
Jul 22 '05 #8

P: n/a
Hi,
I found another solution for my problem:

int data_byte = 0x00;
...
data_byte = Data_Edit->Text.ToInt();
I have another question:
The address is one byte long and the data is one byte long ( I know
I define them as a int, but I only use the values 0x00 to 0xFF so..)
I want to store the address and data into one int.

address data -> 8 bits long
| | |
| |
addr & data -> int (16 bits long)

which code can I use to put the adr and data into the int?
and which code do I use to extract the adr and data after sending
them to my development kit?

somehting like:
addr = addrdata & 0xF0;
data = addrdata & 0x0F;

really don't have a clue :P

thnx in advance
Chris
There are number of different ways to do that. Try this

unsigned byte;
sscanf(Data_Edit->Text.c_str(), "%x", &byte);
outBuffer[0] = (UCHAR)byte;

Note that byte has type unsigned, don't make it any other type.

john

Jul 22 '05 #9

P: n/a

"Chris Online" <cv*******@hotmail.com> wrote in message
news:87**************************@posting.google.c om...
Hi,
I found another solution for my problem:

int data_byte = 0x00;
..
data_byte = Data_Edit->Text.ToInt();
I have another question:
The address is one byte long and the data is one byte long ( I know
I define them as a int, but I only use the values 0x00 to 0xFF so..)
I want to store the address and data into one int.

address data -> 8 bits long
| | |
| |
addr & data -> int (16 bits long)

which code can I use to put the adr and data into the int?
addrdata = (addr << 8)|data;
and which code do I use to extract the adr and data after sending
them to my development kit?

somehting like:
addr = addrdata & 0xF0;
data = addrdata & 0x0F;


Nearly right

addr = (addrdata&0xFF00)>>8;
data = addrdata&0x00FF;

john
Jul 22 '05 #10

P: n/a
> I found another solution for my problem:

int data_byte = 0x00;
..
data_byte = Data_Edit->Text.ToInt();


Three problems here:
1) this won't convert a hex value to an int (you suggested
in another post that you wanted this)
2) ToInt() throws an exception if the user types
something that is not an int
3) Calling an int a byte is misleading; you should check that your
program doesn't break if they type -12345678, for example

I recommend you use C++ Standard objects and functions to do
what you want. This has two advantages:
- you learn something that will work when you have to program
on a non-Borland compiler in future
- you learn about the C++ Standard Library

Start with:
std::string edit_text(Data_Edit->Text.c_str());

and read some documentation on "stringstream" and on stream
manipulators such as std::hex.

Alternatively you could use the C function strtoul() in
<cstring>, which can auto-detect decimal or hex.
Jul 22 '05 #11

P: n/a
Hi,
okay, i've copied your code into my program:

[Builder]
int data_byte = 0x00;
int address_byte = 0x00;
...
UINT outBuffer[64];
outBuffer[0] = (address_byte << 8)|data_byte;
success = DeviceIoControl (handle,
IOCTL_EZUSB_BULK_WRITE,
&bulkControl,
sizeof(BULK_TRANSFER_CONTROL),
&outBuffer[0],
outPacketSize,
&nBytes,
NULL);
[/Builder]

[Keil uVision]
char adr;
char dat;
int adrdat;
BYTE xdata Alpha[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8,
0x80, 0x98, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e };
...
adrdat = &(Alpha[OUT2BUF[0]]); // Get data from USB bus
adr = (adrdat&0xFF00)>>8;
dat = adrdat&0x00FF;
[/Keil uVision]

When I look at 'adr' and 'dat' I see that they do not match the send
addr_byte and data_byte. If I change 'addr_byte', 'adr' also changes but
the value isn't correct. Any idea why?

what does BYTE xdata Alpha mean? should I change it into int xdata Alpha?

regards,
Chris
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.