Scholieren.com forum

Scholieren.com forum (https://forum.scholieren.com/index.php)
-   Software & Hardware (https://forum.scholieren.com/forumdisplay.php?f=20)
-   -   integer omzetten in string. (https://forum.scholieren.com/showthread.php?t=408752)

AtOx 16-03-2003 13:09

integer omzetten in string.
 
Heyz,

Ik ben met 'iets' bezig en daarvoor moet ik integers omzetten in string. Dus getallen naar letters.

Heeft iemand hier een lijst van of een programma voor?

Much greetz,
Atoxje

Talon 16-03-2003 13:11

in C:

itoa(x,y,10);

?

niemand 16-03-2003 13:14

Citaat:

AtOx schreef op 16-03-2003 @ 14:09:
Heyz,

Ik ben met 'iets' bezig en daarvoor moet ik integers omzetten in string. Dus getallen naar letters.

Heeft iemand hier een lijst van of een programma voor?

Much greetz,
Atoxje

het is ook altijd handig om te weten of je in c of in assembly progt..
en wil je dan dat een int met waarde 12345 omgezet word naar een string met waarde "12345" ?

CryptapiX 16-03-2003 13:36

beetje heel rare vraagstelling
meer info over welke taal zou niet overbodig zijn :)

Triloxigen 16-03-2003 15:25

IntToStr(); ???
Over welke taal heb je het?

* ^_^ * 16-03-2003 15:57

Code:

#!/usr/bin/env python
 
###
# Title: Type Checking
# Author: Ben Briggs @ linuxnewbie.org
# Date: March 28, 2001
# Written for: CCAE http://www.codeexamples.org
# Description:
# Illustraits how to check for the types
# of objects.
###
 
###
# This line imports all the objects in the
# module 'types' without object-oriented
# programming. For example, using the import
# method, to use the object type 'IntType',
# you'd need to type 'types.IntType'. With
# the 'from/import' method, you just need
# to type 'IntType'.
###
from types import IntType
 
###
# Now begins the normal module importation
# section.
###
import string  # atoi()
import readline  # Enable 'history' of previously entered values (Use up arrow)

def main():
    ###
    # Variable declarations.
    # ----------------------
    # The variable named 'num' will initially defined
    # with the value of '10000', this will kick off
    # the Number to String/String to Number conversions.
    ###
    num = 10000
   
    ###
    # 'stringspace' will be used throughout the program
    # for converting numbers to strings, and storing
    # other strings.
    #
    # The builtin function 'str()' converts the argument
    # given into a string and returns the new string value.
    ###
    stringspace = str(num)
    print "num: %d  stringspace: %s" % (num, stringspace)

    ###
    # Now perform the number conversion on a literal,
    # as opposed to a variable.
    ###
    stringspace = str(5007)
    print "num: %d  stringspace: %s" % (num, stringspace)

    ###
    # This next line uses the 'atoi()' method of the
    # string module, to convert the string back to a
    # number.
    ###
    num = string.atoi(stringspace)
    print "num: %d  stringspace: %s" % (num, stringspace)

    ###
    # The code in this 'try' statement will assign the
    # string 'good pizza' to the string variable 'stringspace'
    # if the attempt is insuccessful, Python will print
    # an error message stating that it cannot make the
    # conversion.
    ###
    try:
        stringspace = "good pizza"
        num = string.atoi(stringspace)
    except ValueError:
        ###
        # If the buildin exception 'ValueError' happens,
        # print this block of code. A 'ValueError' is
        # storing an inappropriate type in a variable,
        # like assigning a string to an integer.
        ###
        print "Could not convert data to and integer."

    ###
    # Print the values again, to see what is stored in
    # 'num' when the conversion fails.
    ###
    print "num: %d  stringspace: %s" % (num, stringspace)

    ###
    # This 'if' statement checks to see if 'stringspace'
    # was converted to an integer, if it was... print
    # a message stating the new value. If the conversion
    # failed specify that the value in 'stringspace' is
    # a string, and therefore cannot be converted to a
    # integer. This is check with the type 'IntType' from
    # the 'types' module imported at the top of the script.
    ###
    if stringspace is IntType:
        num = string.atoi(stringspace)
        print "new num: %d" % num
    else:
        print "%s is not a number" % stringspace

    print  # A newline is printed
    ###
    # The user is prompted to enter a number.
    ###
    print "Please enter a number at the prompt. Hit enter on a blank line to
exit."

    ###
    # This 'while' loop runs while 'stringspace'
    # is not an empty string (result of pressing
    # the Enter/Return key without entering text).
    ###
    while stringspace != '':
        ###
        # A prompt is printed using the builtin
        # function 'raw_input()', the text passed
        # as a argument, will be printed at the prompt.
        # Any text entered at this prompt, will be
        # stored in the variable 'stringspace'.
        ###
        stringspace = raw_input("> ")
        ###
        # If any text was entered, Python runs
        # the code in this 'if' statement. If not...
        # a "Good Bye" message is printed, and the program
        # exits.
        ###
        if stringspace != '':
            ###
            # This 'try' statement attempts to convert
            # the value(s) entered at the prompt into
            # an integer. If the conversion is successful,
            # Python will print a message stating that the
            # text was a number. If there is a 'ValueError',
            # Python will print that the text is a string.
            ###
            try:
                num = string.atoi(stringspace)
                print "%d is a number." % num
            except ValueError:
                print "%s is a string" % stringspace
        else:
            print "Good Bye!"
 
###
# The next two lines of code check to
# see if this script is being loaded
# as a module, or directly being executed.
# If the answer is the latter, execute the
# main() function.
###
if __name__ == '__main__':
    main()


LB06 16-03-2003 19:35

In JAVA moet je het met de methode valueOf() doen.

Code:

int cijfers = 14;
String tekens = String.valueOf(tekens);

En je hebt de int geconverteerd naar een string

niemand 16-03-2003 19:50

Citaat:

LB06 schreef op 16-03-2003 @ 20:35:
In JAVA moet je het met de methode valueOf() doen.

Code:

int cijfers = 14
string tekens = String.valueOf(tekens)

En je hebt de int geconverteerd naar een string

en die code weigert ie
het type 'string' bestaat namelijk niet, het is 'String'
daarnaast vergeet je 2x een puntkomma
en ja, met die fouten weigert ie gewoon

daarnaast kan het ook een stuk korter:

Code:

int cijfers = 14;
String tekens = ""+cijfers;


TouchOfDarkness 16-03-2003 21:20

Citaat:

niemand schreef op 16-03-2003 @ 20:50:
en die code weigert ie
het type 'string' bestaat namelijk niet, het is 'String'
daarnaast vergeet je 2x een puntkomma
en ja, met die fouten weigert ie gewoon

daarnaast kan het ook een stuk korter:

Code:

int cijfers = 14;
String tekens = ""+cijfers;


volgens mij is dat laatste wel een heel lelijke oplossing...

niemand 16-03-2003 21:26

Citaat:

TouchOfDarkness schreef op 16-03-2003 @ 22:20:
volgens mij is dat laatste wel een heel lelijke oplossing...
wat is er mis mee?
dat is de manier waarop ik op de hva leer om een int om te zetten naar een string

Marcade 17-03-2003 09:51

Hallo zeg wat een bs. hier; die vent heeft niet eens gezegd welke taal.

dafelix 17-03-2003 12:43

hehe. anyway mocht je in VB progameeren:

IntegerName = CStr(StringName)

de niet officiele manier:

Dim IntegerName As Integer
Dim StringName As String

IntegerName = StringName



Maar voortaan issut wel makkelijk als je bijzet welke taal je programmeert, ik heb dit nu waarsgijnlijk voor nix zitten typen :|

LB06 17-03-2003 14:37

Citaat:

niemand schreef op 16-03-2003 @ 20:50:
en die code weigert ie
het type 'string' bestaat namelijk niet, het is 'String'
daarnaast vergeet je 2x een puntkomma
en ja, met die fouten weigert ie gewoon

daarnaast kan het ook een stuk korter:

Code:

int cijfers = 14;
String tekens = ""+cijfers;


Srry. Heb het ge-edit. Had iid wel iets zorgvuldiger mogen zijn.

TouchOfDarkness 17-03-2003 17:27

Citaat:

niemand schreef op 16-03-2003 @ 22:26:
wat is er mis mee?
dat is de manier waarop ik op de hva leer om een int om te zetten naar een string

nou ja, het werkt op zich wel, maar ik vind het geen mooie oplossing. Ben meer voorstander van explicit casting in dit geval, omdat daarmee veel duidelijker aangegeven wordt wat er precies gebeurt.


Alle tijden zijn GMT +1. Het is nu 22:40.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.