0

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!

Kijewski
  • 25,517
  • 12
  • 101
  • 143
jurij
  • 383
  • 3
  • 7
  • 21
  • possible duplicate of [How to convert an int to a series of characters](http://stackoverflow.com/questions/6687486/how-to-convert-an-int-to-a-series-of-characters) – Bo Persson Jul 31 '12 at 17:53
  • @PengyuCHEN: [Don't add homework tag to questions](http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions/10812#10812) – Eric Finn Jul 31 '12 at 18:04
  • @EricFinn Yes and I haven't read that before. My apologies. – starrify Jul 31 '12 at 18:28

4 Answers4

2

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;
Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • I thought the base of hexadecimal was 16...? – Robbie Rosati Jul 31 '12 at 17:39
  • @Kay: It's not a homework related question, actually I am trying to find the best way to send integers via UART ... and the only way is to covert them to ASCII values, in other way I cannot distinguish between them. Thanks anyway! – jurij Jul 31 '12 at 17:43
  • @InternetSeriousBusiness then you would have to use an if-expression or an conditional. I guess a lookup table easier to understand, isn't it? – Kijewski Jul 31 '12 at 17:44
  • Is it possible to do this: int x3 = (x >> 0) & 0xF; // == x % 16 int y1 = x3 < 10 ? x3+'0' : x3+'A'-10; ... in one line? How to reference value in first condition, if you don't have it stored yet in "x3"? – jurij Jul 31 '12 at 19:25
  • @jurij it is possible, you can just insert the intermediate value: `((x >> 8) & 0xF) + (((x >> 8) & 0xF) < 10 ? '0' : 'A'-10)`. It's terrible on the eyes, though. The lookup table-method might be easier to read: `hex[(x >> 8) & 0xF]`. – Kijewski Jul 31 '12 at 19:53
1

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.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Gimballon
  • 81
  • 7
1
             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.

0

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".

Michael
  • 369
  • 2
  • 10
  • No, I don't want char. I want hex digit, stored in an integer (0...A) converted into its ascii value, not into ascii char! – jurij Jul 31 '12 at 17:58
  • Apply most of what I said then: Divide out the digits one at a time, using the method above (except use base 16 instead of 10), then add the constant difference between the digit and the ascii value (add 48 to convert digits between 0 and 9, add 55 for digits between A and F) – Michael Jul 31 '12 at 18:05