Anyone know an easy way to convert a c-style string to an integer array in C++?

This is a discussion on Anyone know an easy way to convert a c-style string to an integer array in C++? within the C++ Programming forums, part of the General Programming Boards category; Since you can't just say Code: char instring[10]; int bufferA[10]; do{ (int)instring[countera]=bufferA[counterA]; countera++; } while.... bla bla bla How do ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    38

    Anyone know an easy way to convert a c-style string to an integer array in C++?

    Since you can't just say

    Code:
    char instring[10];
    int bufferA[10];
    
    do{
    
     (int)instring[countera]=bufferA[counterA];
    countera++;
    }
    
    while.... bla bla bla
    How do I go about accomplishing this?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    the title of your post is exactly the opposite of what you actually want to do. that is the first problem.

    your cast is in the wrong place. you need to cast the elements of bufferA to char to put them into instring. keep in mind that you potentially lose information this way, as most modern systems use a 32-bit int, and only an 8-bit char.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    The purpose of the program is to have the user to enter a string of characters, such as a password. The program would then convert them to an integer array and then multiply each by a key, then write to a file. I've done something like this before, but each letter had to be typed in individually.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    perhaps I should rephrase my question. I want to scramble the letters of a c style string. How can I do this?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Manipulate each letter in the array. Add, subtract, multiply, divide, shift, etc them to your liking.
    No need to convert to integers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    A char array already is an integer array, no need to convert anything

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    Excuse my ignorance, (the only experience I have with this is from online tutorials) but what I take from what you said is that
    Code:
    a= mystring[2] + 15;
    is completely legit??

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ashinms View Post
    Excuse my ignorance, (the only experience I have with this is from online tutorials) but what I take from what you said is that
    Code:
    a= mystring[2] + 15;
    is completely legit??
    Assuming that:

    a is some integer type (say a 'char' for example), and that 'mystring' is an array of that type, and that [2] is a valid index in that array, then yes.
    Code:
    a = mystring[ 2 ] + 15;
    This says:

    add 15 to the value stored in mystring[ 2 ] and store that in a.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. to convert from string to integer
    By vopo in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 05:32 AM
  2. convert string into integer using recursion
    By peter_hii in forum C++ Programming
    Replies: 18
    Last Post: 08-23-2006, 10:09 AM
  3. convert from integer to string
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2006, 09:28 AM
  4. how do i convert a string to an integer??
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 08:21 PM
  5. FAQ how do i convert a string to an integer?? (C)
    By Unregistered in forum FAQ Board
    Replies: 1
    Last Post: 12-02-2001, 04:03 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21