473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Converting char* to char

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
11 3184
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
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

"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
> 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
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

"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
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
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

"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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
15
by: allthecoolkidshaveone | last post by:
I want to convert a string representation of a number ("1234") to an int, with overflow and underflow checking. Essentially, I'm looking for a strtol() that converts int instead of long. The...
11
by: hamishd | last post by:
Is this possible? Sorry if this question isn't relevant here. actually, I'm really trying to convert a unsigned char * to an int
1
by: vcbytes | last post by:
I am having a problem with the following code: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { String^ texts = textBox1->Text; char *text =...
35
by: Sean Farrow | last post by:
Hi: What is best and safest way of converting a char* to a const char *? Can I use const_cast? Cheers Sean.
4
by: screamer81 | last post by:
Hello, I've a SDK functions description for a scanner and I try to convert unmanaged DLL C++ functions to c#. I converted all except one of them. This function is getting a struct parameter. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.