Converting char* to char

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Online

    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_Ed itChange(TObjec t *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
  • Thomas Matthews

    #2
    Re: Converting char* to char

    Chris Online wrote:[color=blue]
    > 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_Ed itChange(TObjec t *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[/color]

    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:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

    Comment

    • Aggro

      #3
      Re: Converting char* to char

      Chris Online wrote:
      [color=blue]
      > char* Data_byte = 0x00;
      >
      > UCHAR outBuffer[64];
      > WORD outPacketSize;
      > outBuffer[0] = Data_byte;[/color]

      What are you trying to do here? What value are you trying to put into
      outBuffer[0] and why?

      Comment

      • John Harrison

        #4
        Re: Converting char* to char


        "Chris Online" <cvanlumig@hotm ail.com> wrote in message
        news:87eb9d19.0 402170651.6adfc 857@posting.goo gle.com...[color=blue]
        > 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;[/color]

        I'm assuming this is a mistype for

        char Data_byte = 0x00;
        [color=blue]
        >
        > UCHAR outBuffer[64];
        > WORD outPacketSize;
        > outBuffer[0] = Data_byte;
        > ..
        > ..
        > void __fastcall TForm1::Data_Ed itChange(TObjec t *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.
        >[/color]

        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

        [color=blue]
        > thnx in advance,
        > Chris[/color]

        john


        Comment

        • Old Wolf

          #5
          Re: Converting char* to char

          > I'm using C++ Builder5. I want to get data from an edit-box and[color=blue]
          > 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;[/color]

          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.
          [color=blue]
          >
          > UCHAR outBuffer[64];
          > WORD outPacketSize;
          > outBuffer[0] = Data_byte;
          > ..
          > ..
          > void __fastcall TForm1::Data_Ed itChange(TObjec t *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?[/color]

          If you noted the line number of that error, you would have seen
          that it refers to:[color=blue]
          > outBuffer[0] = Data_byte;[/color]

          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.
          [color=blue]
          > So I just need to get a byte from the edit-box and store this
          > in a variable.[/color]

          Finally, change this line:[color=blue]
          > Data_byte=Data_ Edit->Text.c_str() ;[/color]
          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.

          Comment

          • Chris Online

            #6
            Re: Converting char* to char

            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

            Comment

            • John Harrison

              #7
              Re: Converting char* to char


              "Chris Online" <cvanlumig@hotm ail.com> wrote in message
              news:87eb9d19.0 402180037.3e1bb e1b@posting.goo gle.com...[color=blue]
              > 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]
              >[/color]

              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_Edi t->Text.c_str() , "%x", &byte);
              outBuffer[0] = (UCHAR)byte;

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

              john


              Comment

              • Chris Online

                #8
                Re: Converting char* to char

                Hi,
                [color=blue]
                > What are you trying to do here? What value are you trying to put into
                > outBuffer[0] and why?[/color]

                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

                Comment

                • Chris Online

                  #9
                  Re: Converting char* to char

                  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
                  [color=blue]
                  > There are number of different ways to do that. Try this
                  >
                  > unsigned byte;
                  > sscanf(Data_Edi t->Text.c_str() , "%x", &byte);
                  > outBuffer[0] = (UCHAR)byte;
                  >
                  > Note that byte has type unsigned, don't make it any other type.
                  >
                  > john[/color]

                  Comment

                  • John Harrison

                    #10
                    Re: Converting char* to char


                    "Chris Online" <cvanlumig@hotm ail.com> wrote in message
                    news:87eb9d19.0 402180807.34ab6 5e7@posting.goo gle.com...[color=blue]
                    > 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?[/color]

                    addrdata = (addr << 8)|data;
                    [color=blue]
                    > 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;
                    >[/color]

                    Nearly right

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

                    john


                    Comment

                    • Old Wolf

                      #11
                      Re: Converting char* to char

                      > I found another solution for my problem:[color=blue]
                      >
                      > int data_byte = 0x00;
                      > ..
                      > data_byte = Data_Edit->Text.ToInt() ;[/color]

                      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 "stringstre am" 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.

                      Comment

                      • Chris Online

                        #12
                        Re: Converting char* to char

                        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_BUL K_WRITE,
                        &bulkControl ,
                        sizeof(BULK_TRA NSFER_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

                        Comment

                        Working...