• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems converting int to char/string in an array  RSS feed

 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried a lot of things but I can't convert from int to chart/String in an array



I think I am getting the ASCII value of C when it prints out. I have tried different forms of Casting but nothing!
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Daniel,

I'm not sure what exactly the problem is. If I try this:
I get as output:
67
C
67
C
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Daniel,

I'm not sure what exactly the problem is. If I try this:
I get as output:
67
C
67
C


Thank you. That solved it.
I was doing this among other alternatives:

But now I see that I need to do the casting for this particular situation in the System.out.println.

Thanks again
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to convert the char to an int is to use the unary + operator:-Unary + is an operator not at all frequently used; it does not change the number from negative to positive. It promotes the number (a char is a number) to occupy at least 32 bits.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since OP said his solution was to cast in the println(), his array was declared as an int[][], not a char[][], so the unary + wouldn't have helped.

His other option would have been to use printf() and the %c format specifier instead.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was mistaken, yes. I thought OP wanted to convert things the other way.
But wouldn't you have to cast the int to a char before you can use %c?
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:wouldn't you have to cast the int to a char before you can use %c?

There is no need to cast int to char with %c
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!