Thread: conversion from string to char

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    1

    Question conversion from string to char

    How do I convert a c++ string into a unsigned char?
    Thanks
    Last edited by erik_markman; 10-02-2003 at 10:39 AM.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    edit:

  3. #3
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    It is probably not the best method, but if you are pretty sure that you're string contains a correct char, you can do the following:

    1. Get the c_str.
    2. Use atoi().
    3. Make a cast.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Question What?

    You can't generally convert a string to a char... A typical string is a string-of-characters, and won't fit into one type-char variable.

    If your string is only one character long, you can "extract" that character.

    Or, you can make a char equal to any one of the characters in a string.

    Or, you can make a character string (AKA C-style string, or array of characters) that is equal to your C++ string.

    Sorry I don't have any examples for you... No books or compiler with me... I might screw it up.

  5. #5
    root
    Join Date
    Sep 2003
    Posts
    232
    >How do I convert a c++ string into a unsigned char?
    Code:
    unsigned char ch = accumulate(s.begin(), s.end(), 0) % numeric_limits<unsigned char>::max();
    I'll leave it up to you to figure out if this particular solution is useful or not.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

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. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. char string to long conversion
    By rastan in forum C Programming
    Replies: 10
    Last Post: 11-06-2003, 07:28 PM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 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