Results 1 to 4 of 4

Thread: How to convert char to hex

  1. #1
    Join Date
    Jun 2009
    Posts
    87

    How to convert char to hex

    I want to create a program where a user inputs some numbers and code is capable of adding those number by first converting them into hexadecimal character and then the addition procedure is executed. I know this sounds really awkward but I want to do this via programming. Is this possible? Can I convert char value to hex and then add them?

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: How to convert char to hex

    Code:
    char *HexStringConversion (char *hStr)
    {
    char *str = new char[(strlen(hStr)*2)+1];
    char *str1 = hStr;
    char *str2 = str;
    
    while('\0' != *str1) 
    {
    sprintf(str2, "%02X", (char)(*str1++));
    str2+=2;
    }
    *(str2) = '\0';
    return(str);
    }
    You can call the above function to convert your characters or strings into hexadecimal number and then add them in your main as normally you would do.

  3. #3
    Join Date
    Nov 2008
    Posts
    996

    Re: How to convert char to hex

    There is no need of any functions if you are doing so in C language. All you need to do is except the number and then display it using "%x". You can even manipulate with this value if you want.

    Code:
    # include <stdio.h>
    # include <conio.h>
    
    int main()
    {
        clrscr();
        char chr[15];
        scanf ("%s", &chr);
        printf ("%x\n", chr[0]);
        getch();
        return 0;
    }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to convert char to hex

    The C++ code goes as below:

    Code:
    # include <iostream.h>
    static const char HexToAsciiValue[256][2] = { {'0','0'}, {'0','1'}, .... {'F','E'},{'F','F'} };
    string CharToHex (const unsigned char* pArray, unsigned int strLen)
    {
        string str;
        str.resize (strLen*2);
        char* pszHex = &str[0];
        const unsigned char* pEnd = pArray + strLen;
        for (const unsigned char* pChar = pArray; pChar != pEnd; pChar++, pszHex += 2 ) {
            pszHex[0] = HexToAsciiValue[*pChar][0];
            pszHex[1] = HexToAsciiValue[*pChar][1];
        }
        return str;
    }

Similar Threads

  1. How to convert string to char array in java?
    By Baazigar in forum Software Development
    Replies: 6
    Last Post: 10-01-2011, 06:36 PM
  2. Array of char and int
    By Jensen Ackles in forum Software Development
    Replies: 5
    Last Post: 23-03-2010, 09:50 AM
  3. Converting Char to Int in PHP
    By GlassFish in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 08:06 PM
  4. Unable to convert from 'System.StringSplitOptions' to 'char'.
    By Juany in forum Software Development
    Replies: 5
    Last Post: 17-02-2010, 08:06 PM
  5. Comparing char array in c++
    By MAHAH in forum Software Development
    Replies: 5
    Last Post: 15-02-2010, 08:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,791,934.13537 seconds with 16 queries