# How to convert ascii to Hex (not using a tool that does it for you!)



## ajrobson (Aug 2, 2006)

Sorry if this is in the wrong place but I didn't know where to put it and seeing how the info needed is for a networking course I take I figured I had a chnace of a reply here.

Can someone please remind me how to convert a ascii string eg "hello" into hexadeciaml? I had to do it last year but for the life of me can't remeber I think you convert ascii to binary then binary to hex?

Can someone tell me or show me a site with step by step instructions for this because all I can find is ascii tables which won't help coz can't take them into lesson or online ascii to hex translators which don't help coz they not teaching me how its done.

Any help at all will be great!


----------



## TerryNet (Mar 23, 2005)

Computers understand only binary, not decimal numbers or the alphabet, etc. ASCII is the common way to encode the alphabet and other characters into binary, which can be represented to us humans as octal, hexadecimal, decimal, etc. So there is no algorithm for conversion of ascii to hex; the encoding has to be memorized or looked up in a table. There are some shortcuts to memorization; for example, if you remember the encoding for 'a' and 'A' you can easily determine the other letters because the alphabet follows in numerical sequence.


----------



## JohnWill (Oct 19, 2002)

In addition, if you're talking about a WEP key, they don't all use the same method. My suggestion is to use HEX keys only for WEP encryption keys.


----------



## ajrobson (Aug 2, 2006)

No i'm not talking about a wep key just a string to hex like "hello" is 68656c6c6f but how do i do that on paper? 

how do i go from ascii to binary? i seem to remeber writing powers of 2 and putting a one where ever the divsion leaves a remainder but cant find my past notes for it


----------



## TerryNet (Mar 23, 2005)

I suppose you can convert from octal, decimal or hexadecimal to binary by successively dividing by two and writing down the remainder from right to left. But, again, ascii is an encoding; there is no algorithm for conversion unless you mean to convert the hex representation of ascii to the binary representation.


----------



## ajrobson (Aug 2, 2006)

so is it possible to use divsion to go from ascii to binary? if so how?


----------



## TerryNet (Mar 23, 2005)

> so is it possible to use divsion to go from ascii to binary?


No.

ascii is an encoding, or mapping from characters we like to use to the only thing that a computer understands--numbers. So, for example, our letter 'A' is mapped to the binary number '01000001' and 'b' is mapped to '01100010'. All the numbers from 00000000 through 11111111 are mapped this way to letters, numbers, punctuation and "special characters."

Our brains are able to handle hexadecimal numbers easier than binary, so we'd generally prefer seeing an ascii table mapping the characters into hexadecimal numbers. For example, 'A' is hex 41 and 'b' is hex 62.

We can convert hex numbers to binary by using division as I outlined in post # 5.


----------



## ajrobson (Aug 2, 2006)

ok i'll put it another way: I'm given a string of ascii characters and need to encode it to hex via any other encoding but I have to do it on paper and without having the ascii table infront of me is that possible?


----------



## avisitor (Jul 13, 2008)

Well, anything's possible... You just need to memorize the ASCII table. You can go from ASCII to binary to octal. Or, if you consider the letters as base 256, then you can go from base 256 to octal directly. It's 256, because by the fundamental counting principle, you have 8 slots, each of which can have 0 or 1, which is 2^8 or 256.


----------



## JohnWill (Oct 19, 2002)

You also have to tell us if you want to go from 7 bit ASCII or 8 bit ASCII.


----------



## TerryNet (Mar 23, 2005)

> I'm given a string of ascii characters and need to encode it to hex


Do you mean, for example, that you are given the string "A# b5" and have to give the answer in hex: 4123206235? And you can't use an ASCII table as I did but have to give some type of algorithm that will convert A to 41 and space to 20, etc.?


----------



## ajrobson (Aug 2, 2006)

TerryNet said:


> Do you mean, for example, that you are given the string "A# b5" and have to give the answer in hex: 4123206235? And you can't use an ASCII table as I did but have to give some type of algorithm that will convert A to 41 and space to 20, etc.?


yes


----------



## TerryNet (Mar 23, 2005)

Must be a new algorithm that wasn't yet invented when I had to do such tasks. Please explain it to us when you learn it.


----------



## JohnWill (Oct 19, 2002)

[WEBQUOTE="http://www.gidnetwork.com/b-44.html"]

```
// This program will convert a string value into it's internal 
// binary equivalent for any base up to 36. The conversion will 
// stop at the first invalid character for the base specified 
// (which includes the \\n or \\0)

#include <stdio.h>
int Convert(char *, int);

int main()
{
    char inbuf[32];
    int  base;      // Base for the conversion
    int  binary;    // final binary value
    int  j;         // loop counter

    printf("Enter the base (in decimal): ");
    fgets(inbuf, 32, stdin);    // Get the base for conversion
    sscanf(inbuf,"%d",&base);   // Make it a value

    printf("Enter the base %d value to convert: ", base);
    fgets(inbuf, 32, stdin);    // Get the value for conversion

    binary = Convert(inbuf, base);
    printf("%d  %Xh \n", binary, binary);  // output dec & hex
    return 0;
}
    
int Convert(char *buf, int cbase)
{
    int j;
    int bin;
    
    j = 0;
    bin = 0;                 // start the binary value at 0
    while (buf[j])
    {
        buf[j] -= 48;           // convert character for binary
        if (buf[j] > 16)
            buf[j] -= 7;        // character was probably letter
        if (buf[j] >= cbase)
            buf[j] -= 32;       // character was probably lower 
        if (buf[j] >= cbase || buf[j] < 0)
            break;              // invalid character, done
        bin *= cbase;           // multiply by the base 
        bin += buf[j];          // add current value
        j++;                    // next character
    }
    return bin;             // return the binary value
}
```
[/WEBQUOTE]


----------

