Convert a string of characters to a hexidecimal value

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 836 Views - Last Post: 15 February 2012 - 02:11 AM Rate Topic: -----

#1 Renegade_Queermo  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 13-February 12

Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 12:08 PM

For example convert "hello my name is bob" to hex. I have tried using %X in the printf statement to print out the hex value but it prints out the wrong value. Also tried making another for loop but that broke my code.

here is my code so far

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()

{
    char mainArray[20]; // specify max size of string to be 80
    char CopyArray[20];//specify max size of coped arry to be 80
    int i=0; // start indexing into array at 0
    int j=0;
    int index1=0;


    printf("Please enter in a string of character up to 19 characters in length. \n");

    //get characters from keyboard until enter key pressed
    while((mainArray[i] = getchar( ))  != '\n' )
    {
        i++; // increment to next location in array
    }

    mainArray[i] = '\0'; // NULL termination for string

    printf("The first string is %s %X.\n",mainArray);
    //printf("The first string in hex is %x.\n",mainArray);

    for(index1=i-1; index1>=0; index1--)
    {
        CopyArray[j]= mainArray[index1];

        j++;//increment to next location in array
    }
    CopyArray[j]='\0';// NULL termination for string
    printf("The second string is %s.\n",CopyArray);

    {

        return 0;
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Convert a string of characters to a hexidecimal value

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 12:13 PM

You didn't really think it would be that easy, did you?

To print chars as hex numbers you have to print them one at a time.
Was This Post Helpful? 0
  • +
  • -

#3 Renegade_Queermo  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 13-February 12

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 12:17 PM

View Postr.stiltskin, on 13 February 2012 - 12:13 PM, said:

You didn't really think it would be that easy, did you?

To print chars as hex numbers you have to print them one at a time.


Do I have to implement some sort of counter to my string and then based off of the char size convert each independent character into its hex value?
Sorry I am kinda new to C and this program is causing my great confusion.
Was This Post Helpful? 0
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 12:18 PM

Well you can read a string with scanf until you find a delimiter like so:

    char str[20];
    scanf("%[^\n]", str);



Now every character is 1byte, which can be converted to a 2digit hexadecimal value. The %x is correct, but you have to convert your string character by character in a loop.

The resulting code is actually quite short.

Spoiler


h  e  l  l  o     m  y     n  a  m  e     i  s     k  a  r  e  l
68 65 6c 6c 6f 20 6d 79 20 6e 61 6d 65 20 69 73 20 6b 61 72 65 6c


This post has been edited by Karel-Lodewijk: 13 February 2012 - 12:22 PM

Was This Post Helpful? 1
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 12:22 PM

You can read the string from the keyboard exactly as you were doing. But to print it as a list of hex numbers, you have to print the elements of the array one at a time.

You don't have to modify the contents of the array at all. Each char already is actually a number. Just loop through the array as if it were an array of ints, printing one element at a time using the %x printf specification.
Was This Post Helpful? 0
  • +
  • -

#6 Renegade_Queermo  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 13-February 12

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 12:29 PM

View PostKarel-Lodewijk, on 13 February 2012 - 12:18 PM, said:

Well you can read a string with scanf until you find a delimiter like so:

    char str[20];
    scanf("%[^\n]", str);



Now every character is 1byte, which can be converted to a 2digit hexadecimal value. The %x is correct, but you have to convert your string character by character in a loop.

The resulting code is actually quite short.

Spoiler


h  e  l  l  o     m  y     n  a  m  e     i  s     k  a  r  e  l
68 65 6c 6c 6f 20 6d 79 20 6e 61 6d 65 20 69 73 20 6b 61 72 65 6c




Its giving me an error code when I tried your code of 'for' initial declarations are only allowed in c99 mode

View Postr.stiltskin, on 13 February 2012 - 12:22 PM, said:

You can read the string from the keyboard exactly as you were doing. But to print it as a list of hex numbers, you have to print the elements of the array one at a time.

You don't have to modify the contents of the array at all. Each char already is actually a number. Just loop through the array as if it were an array of ints, printing one element at a time using the %x printf specification.



What?
Was This Post Helpful? 0
  • +
  • -

#7 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 12:29 PM

Compile in C99 mode or:

int i;
for (i = 0; str[i] != '\0'; i++)


Was This Post Helpful? 1
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 12:30 PM

View PostRenegade_Queermo, on 13 February 2012 - 02:29 PM, said:

View Postr.stiltskin, on 13 February 2012 - 12:22 PM, said:

You can read the string from the keyboard exactly as you were doing. But to print it as a list of hex numbers, you have to print the elements of the array one at a time.

You don't have to modify the contents of the array at all. Each char already is actually a number. Just loop through the array as if it were an array of ints, printing one element at a time using the %x printf specification.



What?

What what?
Was This Post Helpful? 0
  • +
  • -

#9 Renegade_Queermo  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 13-February 12

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 12:46 PM

I understand what you are saying a attempted doing it but failed horribly.
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 12:58 PM

This illustrates the concept:

#include <stdio.h>

int main ()
{
    char ch;
    while( 1 ) {
        printf("Enter a single character, then press enter (or just enter twice to quit): ");
        ch = getchar();
        getchar();
        if( ch > 20 && ch < 128 ) {
            printf("The hex equivalent of %c is %x\n", ch, ch);
        }
        else {
            break;
        }
    }
    return 0;
}



This "magic" is based on the ascii codes of the characters.
Was This Post Helpful? 0
  • +
  • -

#11 Renegade_Queermo  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 13-February 12

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 01:09 PM

View Postr.stiltskin, on 13 February 2012 - 12:58 PM, said:

This illustrates the concept:

#include <stdio.h>

int main ()
{
    char ch;
    while( 1 ) {
        printf("Enter a single character, then press enter (or just enter twice to quit): ");
        ch = getchar();
        getchar();
        if( ch > 20 && ch < 128 ) {
            printf("The hex equivalent of %c is %x\n", ch, ch);
        }
        else {
            break;
        }
    }
    return 0;
}



This "magic" is based on the ascii codes of the characters.


Sweet thanks man I am going to fu... fiddle around with my code till I get it to work as I need it too.
Was This Post Helpful? 0
  • +
  • -

#12 Renegade_Queermo  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 13-February 12

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 04:17 PM

so I tried messing around with that code snippet you gave me but didn't get far. What I am trying to do is print out a character sting such as "hello" with its hex sting value then print the string out in reverse ie "olleh" and its hex value.
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Convert a string of characters to a hexidecimal value

Posted 13 February 2012 - 04:26 PM

I suggest you concentrate on finishing the first part before you complicate things with the reversing.

You've been shown how to display the hex equivalents of individual characters; all you have to do is adapt that to each of the elements of an array. If you can't do that, post your code with your best effort and explain exactly what's confusing you.
Was This Post Helpful? 0
  • +
  • -

#14 Renegade_Queermo  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 13-February 12

Re: Convert a string of characters to a hexidecimal value

Posted 14 February 2012 - 11:48 AM

View Postr.stiltskin, on 13 February 2012 - 04:26 PM, said:

I suggest you concentrate on finishing the first part before you complicate things with the reversing.

You've been shown how to display the hex equivalents of individual characters; all you have to do is adapt that to each of the elements of an array. If you can't do that, post your code with your best effort and explain exactly what's confusing you.


So I can get the first string to print with its hex equivalent but I can no longer get the reverse to print. Everything after the huge bar is not working in my code.

/**
Lab 6.c V 1.1 Sam Sloane 06/02/12


        \Specification

a program that counts characters in a string entered from the keyboard.
The string must not exceed 80 characters
The program then prints the string out to the console backwards along with the hexadecimal value of each character.
The program will also print to a text file.
An array is used to implemnt the string of numbers.
Another array of the same size is used to sort the string.
Hitting Enter will end the program

       \Design

The user enter character from the keyboard into a string.
The string is stored.
The string is then reversed using and printed back to the user.
The ouput is printed to a txt file; the location of which is determined by the user.
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()

{
    char mainArray[20]; // specify max size of string to be 20
    char CopyArray[20];//specify max size of coped arry to be 20
    int i=0; // start indexing into array at 0
    int j=0;
    int index1=0;



    printf("Please enter in a string of character up to 19 characters in length. \n");
    char str[20];
    int w;
    scanf("%[^\n]", str);
    for (w = 0; str[w] != '\0'; w++)
    printf("%x ", str[w]);
    printf("\n");


/*************************************************************************************************************************/
/*************************************************************************************************************************/
/*************************************************************************************************************************/
    //get characters from keyboard until enter key pressed
    while((mainArray[i] =str[w])  != '\n' )
    {
        i++; // increment to next location in array
    }




    mainArray[i] = '\0'; // NULL termination for string
    printf("\n \n");
    printf("The first string is %s.\n",mainArray);

    for(index1=i-1; index1>=0; index1--)
    {
        CopyArray[j]= mainArray[index1];

        j++;//increment to next location in array
    }
    CopyArray[j]='\0';// NULL termination for string
    printf("The second string is %s.\n",CopyArray);

    {

        return 0;
    }



}


Was This Post Helpful? 0
  • +
  • -

#15 Renegade_Queermo  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 13-February 12

Re: Convert a string of characters to a hexidecimal value

Posted 14 February 2012 - 12:22 PM

k so I got it to print out the first string and its hex value. As well as the second string but no hex value.

here is my code any help is appreicated I almost have it but I just can't get the final spark.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()

{
   // char mainArray[20]; // specify max size of string to be 20
    char CopyArray[20];//specify max size of coped arry to be 20
    //int i=0; // start indexing into array at 0
    int j=0;
    int index1=0;
    char str[20];
    int w;


    printf("Please enter in a string of character up to 19 characters in length. \n");
    scanf("%[^\n]", str);
    for (w = 0; str[w] != '\0'; w++)
    printf("%x ", str[w]);
    printf("\n");



    //get characters from keyboard until enter key pressed



    for(index1=w-1; index1>=0; index1--)


    {


        CopyArray[j]= str[index1];


        j++;//increment to next location in array


    }

    CopyArray[j]='\0';// NULL termination for string


    printf("The second string is %s.\n",CopyArray);
    printf("%x",CopyArray[j]);
    {

        return 0;

    }

}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2