Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

convert 123 into 1,2 & 3

southern_ladsouthern_lad Member Posts: 21
Hi again :)

another qu concerns the following:

if i have the 123d and i want to have each element in it's own char
like

int no = 123;

char huns = 123/100; //works!! i have 1 in huns
char tens = This is were am stuck!!
char units = 123 MOD 10; //works!! i have 3 in units.

but how do i get my tens?
this has me stumped.
could u also help with this?

Thanx

Comments

  • southern_ladsouthern_lad Member Posts: 21
    okay i think i've solved this one if i just div the result by 10 then i'll have my 10's.

    : char tens = (123%10)/10;

    thanx anyway :)


    : Hi again :)
    :
    : another qu concerns the following:
    :
    : if i have the 123d and i want to have each element in it's own char
    : like
    :
    : int no = 123;
    :
    : char huns = 123/100; //works!! i have 1 in huns
    : char tens = This is were am stuck!!
    : char units = 123 MOD 10; //works!! i have 3 in units.
    :
    : but how do i get my tens?
    : this has me stumped.
    : could u also help with this?
    :
    : Thanx
    :

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [code]
    #include

    int n=179;
    char buf [16];

    sprintf (buf, "%d", n);
    char c1 = buf [0]; // Will be '1'
    char c2 = buf [1]; // Will be '7'
    char c3 = buf [2]; // Will be '9'

    [/code]
  • southern_ladsouthern_lad Member Posts: 21
    Hi,
    thankx but id like it in dec, i think that this would give me ascii?
    am i wrong?


    : [code]
    : #include
    :
    : int n=179;
    : char buf [16];
    :
    : sprintf (buf, "%d", n);
    : char c1 = buf [0]; // Will be '1'
    : char c2 = buf [1]; // Will be '7'
    : char c3 = buf [2]; // Will be '9'
    :
    : [/code]
    :

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [b][red]This message was edited by AsmGuru62 at 2002-10-25 6:28:29[/red][/b][hr]
    : Hi,
    : thankx but id like it in dec, i think that this would give me ascii?
    : am i wrong?
    :
    :
    : : [code]
    : : #include
    : :
    : : int n=179;
    : : char buf [16];
    : :
    : : sprintf (buf, "%d", n);
    : : int c1 = buf [0][red]-'0'[/red]; // Will be decimal 1
    : : int c2 = buf [1][red]-'0'[/red]; // Will be decimal 7
    : : int c3 = buf [2][red]-'0'[/red]; // Will be decimal 9
    : :
    : : [/code]
    : :
    :
    :
    [blue]Yes. It will give an ASCII, but with RED - it will be DEC.[/blue]


  • southern_ladsouthern_lad Member Posts: 21
    Thankx v. Much

    : [b][red]This message was edited by AsmGuru62 at 2002-10-25 6:28:29[/red][/b][hr]
    : : Hi,
    : : thankx but id like it in dec, i think that this would give me ascii?
    : : am i wrong?
    : :
    : :
    : : : [code]
    : : : #include
    : : :
    : : : int n=179;
    : : : char buf [16];
    : : :
    : : : sprintf (buf, "%d", n);
    : : : int c1 = buf [0][red]-'0'[/red]; // Will be decimal 1
    : : : int c2 = buf [1][red]-'0'[/red]; // Will be decimal 7
    : : : int c3 = buf [2][red]-'0'[/red]; // Will be decimal 9
    : : :
    : : : [/code]
    : : :
    : :
    : :
    : [blue]Yes. It will give an ASCII, but with RED - it will be DEC.[/blue]
    :
    :
    :

  • Justin BibJustin Bib USAMember Posts: 0

    ________ | http://forcoder.org | free ebooks and video tutorials about { MATLAB, Visual Basic, Java, Assembly, C#, C++, PHP, Visual Basic .NET, Objective-C, Perl, Swift, Ruby, PL/SQL, Go, Delphi, C, Python, JavaScript, R, Scratch Prolog, Alice, Bash, Awk, ML, Julia, Fortran, Rust, Clojure, ABAP, Lisp, COBOL, FoxPro, Apex, VBScript, Logo, Kotlin, Hack, Crystal, F#, SAS, D, Erlang, Scheme, LabVIEW, Lua, Dart, Transact-SQL, Scala, Ada } ___

Sign In or Register to comment.