how to return the char in C?


 
Thread Tools Search this Thread
Top Forums Programming how to return the char in C?
# 1  
Old 05-29-2011
how to return the char in C?

hi
I have the below code to convert hexa to binary. It is working fine, but i want to store the binary in char or int. The below program is printing the result.
Code:
void ConvertHexToBinary(char sHexValue[])
{
  int i;
  printf("Binary Equivalent=");
  for(i=0;sHexValue[i]!=NULL;i++)
  {
    switch(sHexValue[i])
    {
    case '0':
      printf("0000");
      break;
    case '1':
      printf("0001");
      break;
    case '2':
      printf("0010");
      break;
    case '3':
      printf("0011");
      break;
    case '4':
      printf("0100");
      break;
    case '5':
      printf("0101");
      break;
    case '6':
      printf("0110");
      break;
    case '7':
      printf("0111");
      break;
    case '8':
      printf("1000");
      break;
    case '9':
      printf("1001");
      break;
    case 'a':
    case 'A':
      printf("1010");
      break;
    case 'b':
    case 'B':
      printf("1011");
      break;
    case 'c':
    case 'C':
      printf("1100");
      break;
    case 'd':
    case 'D':
      printf("1101");
      break;
    case 'e':
    case 'E':
      printf("1110");
      break;
    case 'f':
    case 'F':
      printf("1111");
      break;
    default:
      printf("\nEntered number is not Hexadecimal. Printed value is incorrect. ");
      break;
    }
  }
}
void main()
{
  char s[128]= cpSegment;
  printf("\n\n\nBinary equivalent of primary segment is: ");
  ConvertHexToBinary(s);
// Here i want to get binary in int or char like this
//char or int = ConvertHexToBinary(s);
}

thanks

Moderator's Comments:
Mod Comment Please use less formatting, code tags and indent code. Thanks

Last edited by Scott; 05-29-2011 at 09:16 AM.. Reason: Code tags, etc.
# 2  
Old 05-29-2011
how about sscanf(string, "%x", &intvariable);
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

2. UNIX for Advanced & Expert Users

File command return wrong filetype while file holds group separator char.

hi, I am trying to get the FileType using the File command. I have one file, which holds Group separator along with ASCII character. It's a Text file. But when I ran the File command the FileType is coming as "data". It should be "ASCII, Text file". Is the latest version of File... (6 Replies)
Discussion started by: Arpitak29
6 Replies

3. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

4. Shell Programming and Scripting

Insert carriage return on the 10th char position of each line

Hi experts, Need your help on how to insert carriage return after the 10th char position of each line in a file and then add two blank spaces after the carriage return. Example: >cat test.txt testingline dummystring samplesample teststringline Expected output should be.. ... (2 Replies)
Discussion started by: brichigo
2 Replies

5. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

6. Shell Programming and Scripting

A variable is including the Carriage Return char...

Hi all, I'm reading a file with this layout: First_Col Second_Col The Second_Col has values as 1000, -1, 10, 43... While reading the file I'm getting the second column value with awk command, but it is including the CR control char. do item_saved=`echo $b | awk '{print... (4 Replies)
Discussion started by: mrreds
4 Replies

7. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

8. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

9. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

10. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies
Login or Register to Ask a Question