Question

I would like to split integer into digits and then convert each digit to ASCII (in C programming language).

Example:

int x = 0xABC
/* split integers */
int x1 = 0xA
int x2 = 0xB
int x3 = 0xC
/* convert integers to ASCII */
int x1 = 0x41
int x2 = 0x42
int x3 = 0x43

Also, if the integer is only 2 digits long (hex), I still need 3 splits:

int y = 0xBC
/* split integers */
int y1 = 0x0
int y2 = 0xB
int y3 = 0xC 
.
.
.

Many thanks in advance!

Was it helpful?

Solution

Use math: x = x₁ · 16² + x₂ · 16 + x₃

Use a lookup table to see what the digit is:

static const char hex[16] = "0123456789ABCDEF";

y1 = hex[x1];
...

I won't give you a full solution since it's a homework related question.


Full solution (less easy to understand):

// Divide by 16^n and take the modulo 16:
int x1 = (x >> 8) & 0xF; // == (x/256) % 16
int x2 = (x >> 4) & 0xF; // == (x/16) % 16
int x3 = (x >> 0) & 0xF; // == x % 16

int y1 = x1 < 10 ? x1+'0' : x1+'A'-10;
int y2 = x2 < 10 ? x2+'0' : x2+'A'-10;
int y3 = x3 < 10 ? x3+'0' : x3+'A'-10;

OTHER TIPS

Ok, your question didn't give enough information for me to answer the "spirit" of the exorcise...but here you go:

int x = 0xABC;
printf("%03X",x); // Result = "ABC"
x = 0xAB;
printf("%03X",x); // Result = "0AB"

Can easily modify that to sprintf if you want to store the result.

             A PROGRAM TO CONVERT INT INTO ASCII.




              #include<stdio.h>
              #include<string.h>
              #include<conio.h>

               char data[1000]= {' '};           /*thing in the bracket is optional*/
               char data1[1000]={' '};
               int val, a;
               char varray [9];

               void binary (int digit)
              {
                  if(digit==0)
                   val=48;
                  if(digit==1)
                   val=49;
                  if(digit==2)
                   val=50;
                  if(digit==3)
                   val=51;
                  if(digit==4)
                   val=52;
                  if(digit==5)
                   val=53;
                  if(digit==6)
                   val=54;
                  if(digit==7)
                   val=55;
                  if(digit==8)
                   val=56;
                  if(digit==9)
                    val=57;
                    a=0;

               while(val!=0)
               {
                  if(val%2==0)
                   {
                    varray[a]= '0';
                   }

                   else
                   varray[a]='1';
                   val=val/2;
                   a++;
               }


               while(a!=7)
              {
                varray[a]='0';
                a++;
               }


              varray [8] = NULL;
              strrev (varray);
              strcpy (data1,varray);
              strcat (data1,data);
              strcpy (data,data1);

             }


              void main()
             {
               int num;
               clrscr();
               printf("enter number\n");
               scanf("%d",&num);
               if(num==0)
               binary(0);
               else
               while(num>0)
               {
               binary(num%10);
               num=num/10;
               }
               puts(data);
               getch();

               }

i checked my coding and it's working fine. it converts any given integer to ascii code. must try and let me know if that's what you are looking for thanks.

Since this is homework, I'm not going to give a straight solution to the problem.

First I am confused what you mean by splitting an "integer". It looks like (from the examples) that you wish to split a hexadecimal number, not a decimal number. Either way, think of how a number is built: 123 = 1*100 + 2*10 + 3*1 = 1*10^2 + 2*10^1 + 3*10^0. How would you separate the 1 from the 2 and the three? (hint, use the opposite of the multiplication.) (Another hint, if it's in hexadecimal, remember to change your base from 10 to 16)

Assume now that you have split the digits into separate variables. Well, the ascii table constructed in a very "convenient" way. Look up the table, and find the ascii values for the digits you want. From there, it will be a simple constant addition/subtraction to get the ascii representation of the digits.

(Another hint) I assume you mean to convert from an int into a char. Remember the bits are just data. You have the ability to change the representation of data. Search "casting int to char".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top