Thread: String to a single char

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    20

    String to a single char

    I have an interesting problem in a program I'm working on. I have input strings from a file that I'm reading in one by one that are supposed to be used as a hex value later but I'm supposed to store the double digit into a single character byte. How do I do this?

    Input:
    ADD 3 18
    ADDF 3 58
    ADDR 2 90
    AND 3 40
    BASE 0 FF
    BYTE 0 FF
    CLEAR 2 B4

    (third column is the hex values represented as a string)

    Code:
         char  ot_opcode;
    How do i store the double digits into the single byte of ot_opcode?

  2. #2
    Hello,

    The char data type is a single byte, capable of holding one character in the local character set. This tells us that we can only hold one character at the time being in a single char. Though, if we use an array, we could store our double digit number.

    Character Arrays
    The most common type of array in C is the array of characters. For example:
    Code:
    char str[2];
    That declares str to be an array of 2 characters. Array subscripts always start at zero in C, so the elements are str[0] and str[1]. Characters, unlike integers, are written between single quotes which represent an integer value equal to the numerical value of the character in the machine's character set. This is called a character constant, although it is just another way to write a small integer. For example, 'A' is a character constant; in the ASCII character set its value is 65. Of course 'A' is preferred over 65, though in fact you could have your character constant equal either one.

    The easiest way to find the integer value of a character constant is to subtract '0'. Not 0, but '0'. By definition chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions. This is natural, and convenient. For example, c-'0' is an integer expression, as c is a character constant.

    Back to your double digit. You could take 18 for example and convert it to your character array. All that could be done is to simply find how many 10's or 100's were found in your program and find the remainder, convert them to ASCII by adding '0' to your integers and setting each character array index to the corresponding value.


    - Stack Overflow
    Last edited by Stack Overflow; 11-15-2004 at 08:49 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    You want to convert Hex to binary and store it in a single byte char
    So you may use
    Code:
    char *my_str="0xAD"
    sscanf(,"0x%x",&hex_number);
    
    //Or, a more complex way
    my_str += 2;
    char c0 = my_str[0];
    char c1 = my_str[1];
    c0 = 'A'<=c0 && c0<='Z' ? c0-'A' : ('a'<=c0 && c0<='z' ? c0-'a' : 0);
    c1 = 'A'<=c1 && c1<='Z' ? c1-'A' : ('a'<=c1 && c1<='z' ? c1-'a' : 0);
    hex_number = c0<<4+c1;

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    Thanks Stack Overflow, I know about character arrays even though that was a great explanation, but that would be too simple of a question. The problem I'm having is that I need for example:

    input: (hex values that start as a character string)
    FF
    C4
    14
    F3

    Code:
    char hexValueString[5];
    char ot_opcode;
    
    fscanf ( fptr, "%s", hexValueString );
    
    /* ot_opcode = 'hexValueString' such as:
    
        ot_opcode = 'FF'
        ot_opcode = '14'
        ot_opcode = 'C4'
        ot_opcode = 'F3'
    */
    I feel like I'm making this too hard and yes this is part of an assignment I'm doing but this is a very minimal part that's holding me up and I keep reading:

    "Note, that using %x to read a value for ot_opcode won't work. So,
    you have to read the value (into a temporary variable) and then
    put this value into the one byte of ot_opcode."

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    Hey xErath, you actually just beat me to posting while I was so I'm checking out your solution right now... thanks

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    /* ot_opcode = 'hexValueString' such as:
    
        ot_opcode = 'FF'
        ot_opcode = '14'
        ot_opcode = 'C4'
        ot_opcode = 'F3'
    */
    Do you know for sure what a char is??
    A char holds a value from 0 to 255, and is represented in hex with two hex digits, being the 1st the 4 more significant bytes, and the 2nd the least 4 significant bytes.

    'f' in ascci would be 0x66 in hex or 01100110 in binary.

    what you wrote there is invalid
    Could be
    Code:
     
        char* ot_opcode = "FF";
        char* ot_opcode = "14";
        char* ot_opcode = "C4";
        char* ot_opcode = "F3";
    but that's not in any way a single byte var.

  7. #7
    It is possible to convert your hex value you get from your file to an integer, though I'm not quite sure a single character can hold FF since F is one character and the other F is another.

    We do know that 0xFFFF is 65535, and FF is 255, as an integer, though a character can only hold between 0 and 255. If we did set a character to 255 it would not be FF. You can check out AsciiTable.com to see the character set in ASCII.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    A char holds a value from 0 to 255,
    To quote yourself
    Do you know for sure what a char is??
    Because obviously you don't
    Also your "more complex" way is not portable in the least.

    Thumper333 if you want to read in "FF" and store it's value in a character then you should look at strol().

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
      char hexstring[] = "FF";
      unsigned char value = strtol(hexstring, NULL, 16);
      printf("String: %s\tValue: %d\n", hexstring, (int)value);
      return 0;
    }
    I'll leave it up to you to figure out how to isolate the part you want to convert.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    I see what you both mean. I just kept thinking in terms of converting to the character since it seemed like it was between 0 and 255, but now I can just try changing it to an int instead. How would you change it to an integer then?

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    255 it would not be FF
    ????
    255 in decimal IS FF in hex, and represents some strange char in the asci table, or doesn't represent anything at all.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    There it goes again! Beat to the post. Thank you all for your help. I think that looks more like what I'm trying to do. You all are great though!

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    char *mamemimomu="AB";
    int burp = mamemimomu[0]+mamemimomu[1]<<8;

  13. #13
    Quote Originally Posted by Thantos
    Do you know for sure what a char is??
    »Indeed. The three types of char's:

    Signed Char
    Signed char can hold all values in the range of SCHAR_MIN to SCHAR_MAX, defined in <limits.h>. SCHAR_MIN must be -127 or less (more negative), and SCHAR_MAX must be 127 or greater. Note that many compilers for processors which use a 2's complement representation support SCHAR_MIN of -128, but this is not required by the standards.

    Unsigned Char
    Unsigned char can hold values between 0 and UCHAR_MAX inclusive, which is required to be at least 255. If CHAR_BIT is greater than 8, UCHAR_MAX is required to be 2 CHAR_BIT - 1. So an implementation which uses 9 bit chars can fit values from 0 to 511 in an unsigned char.

    Default Char
    Default char can hold all the values between CHAR_MIN and CHAR_MAX inclusive. If char is signed by default CHAR_MIN is equal to SCHAR_MIN and CHAR_MAX is equal to SCHAR_MAX. Otherwise the default char type is unsigned and CHAR_MIN is 0 and CHAR_MAX is equal to UCHAR_MAX.

    Quote Originally Posted by xErath
    255 in decimal IS FF in hex, and represents some strange char in the asci table, or doesn't represent anything at all.
    Of course 255 is FF in hex. Though my main point was that someone can't expect to make a char equal 255 and have it display FF by displaying it as a %c.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You also forgot that sizeof(char) is 1 and that its the smallest addressable unit. All other data types have to be equal or greater in size.

    Also while '0'-'9' must be contigious the same is not true for 'a'-'z' and 'A'-'Z'

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    Thantos, your code did the trick. Thanks everyone for your input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 02:29 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM