ascii to str

This is a discussion on ascii to str within the C++ Programming forums, part of the General Programming Boards category; hello. here is a(maby a really stupid) qestion. how can i convert a hex (in a string) to a dec ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    37

    ascii to str

    hello.

    here is a(maby a really stupid) qestion.

    how can i convert a hex (in a string) to a dec int

    and what must i do to get the char (which ascii number my int contains) into a string or char*??

    or is there an easyer way to get the hex number in a textfiled to the char??

    thanx

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I assume you know how to convert a hexadecimal to decimal number? Well, just read each character from the string and apply that algorithm. The result for each character should be added to the sum and the result, the sum, can be placed in the integer.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    How about a hex string to a long.
    Code:
    #include <stdlib.h>
    .
    .
       char str[10] = "ffff";
       long num;
       char *endptr;
    
       num = strtol(str,&endptr,16);
       cout << num << endl;

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    37
    thankyou.

    ive got it already. thought that there is a single command for this.....

    by the way:

    why is
    Edit1->Text=(char)"0x43"+(char)"0x30";

    not the same as

    Edit1->Text=(char)"0x43";
    Edit1->Text=Edit1->Text+(char)"0x30";

    ??????

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Is Edit1->Text of type string?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    37
    yes. im using borland c++builder5. Edit1 is my editbox and its text is of type AnsiString.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >why is
    >Edit1->Text=(char)"0x43"+(char)"0x30";

    >not the same as

    >Edit1->Text=(char)"0x43";
    >Edit1->Text=Edit1->Text+(char)"0x30";

    I'll take a guess.

    In case 1, I would guess it interprets the + as addition (because both operands are of type char).

    In case 2, I would guess it interprets the + as concatenation (since the first operand is a string).

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    37
    -swoopy
    you seem to be right.

    what must i do to get the concatenation in a single line.
    is there a other operator than "+"??

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    -wesentlich,
    you could try:

    Edit1->Text= (AnsiString)(char)"0x43"+(AnsiString)(char)"0x30";

    assuming the compiler takes it. Also, I'm thinking this could be written:

    Edit1->Text= (AnsiString)(char)0x43+(AnsiString)(char)0x30;

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    37
    thanx.
    Edit1->Text= (AnsiString)(char)0x43+(AnsiString)(char)0x30;
    works

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Glad to help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 06:03 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 02:43 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 08:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 08:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21