Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Conversion: char3 to numc3

Former Member
0 Likes
2,224

Hi,

Is it a complicated process to convert char3 to numc3?

Thanks,

Georgy

16 REPLIES 16
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
2,054

Nop it is not.

data: c(3) type c value '1',
        n(3) type n.

n = c.
write:/ n.

Regards,

RIch Heilman

Read only

0 Likes
2,054

Hi Rich,

I am not sure it'll work. Here is an example:

DATA: numc TYPE numc3.

DATA: char TYPE char3.

char = 'AOB'.

numc = char. <-this will fail

this is in a way is what you are trying to do, I assume.

Thanks,

Gerogy

Read only

0 Likes
2,054

It will NOT fail, it will simply only move the numeric value to the numeric field. So if I make it A3B, you can see that the 3 is moved to the numeric, you can not move non-numeric values to a numeric field, this is fundamentals of programming.



report  zrich_0001.

data: numc type numc3.
data: char type char3.

char = 'A3B'.
numc = char.

write:/ numc.

Regards,

RIch Heilman

Read only

Former Member
0 Likes
2,054

Hi Georgy,

Type C and Type n are interconvertible..

But the only thing is: If your char type field has Alphanumeric chars, then only the numeric chars will be copied in type N field...

Thanks and Best Regards,

Vikas Bittera.

**Reward if useful**

Read only

former_member194669
Active Contributor
0 Likes
2,054

Hi,



report zars.
data : v_char(3) type c.
data : v_numc(3) type n.

v_char = 'A23'.
write v_char to v_numc.
write v_numc.

aRs

Read only

Former Member
0 Likes
2,054

DATA : w_a(10) TYPE c,

w_b(10) TYPE n.

w_a = '12345'.

w_b = w_a. ( w_b will be '0000012345' )

w_a = 'A12345'.

w_b = w_a. ( '0000012345')

alphanumeric char will be ignored.

Thanks and Best Regards,

Vikas Bittera.

**Reward if useful**

Read only

0 Likes
2,054

Hi Vikas,

This is the point. I need to convert chars to numbers such as A to a digit. Is it possible?

Thanks,

Georgy

Read only

0 Likes
2,054

Ok, I see wha you are trying to do now. But what is the conversion?

A = 1

B = 2

C = 3

D = 4

E = 5

and so on?

Regards,

Rich Heilman

Read only

0 Likes
2,054

Hi Georgy,

Do you have any specific value to be put for 'A' or any other char??? tell me the same.. i feel by replace this will be possible..

Thanks and Best Regards,

Vikas Bittera.

**Reward if useful**

Read only

0 Likes
2,054

IF that is the case, you can do something like this.



report  zrich_0001.

data: numc type numc3.
data: char type char3.
data: str type string.

char = 'A3B'.

do 3 times.

  sy-index = sy-index - 1.
  case char+sy-index(1) .
    when 'A'.  char+sy-index(1) = '1'.
    when 'B'.  char+sy-index(1) = '2'.
    when 'C'.  char+sy-index(1) = '3'.
    when 'D'.  char+sy-index(1) = '4'.
    when 'E'.  char+sy-index(1) = '5'.
    when 'F'.  char+sy-index(1) = '6'.
    when 'G'.  char+sy-index(1) = '7'.
    when 'H'.  char+sy-index(1) = '8'.
    when 'I'.  char+sy-index(1) = '9'.
  endcase.
enddo.

numc = char.

write:/ numc.

Regards,

Rich Heilman

Read only

0 Likes
2,054

Or....



report  zrich_0001.

data: numc type numc3.
data: char type char3.
data: str type string.

str = 'A1B2C3D4E5F6G7H8I9'.   " Translate String

char = 'A3B'.

translate char using str.

numc = char.

write:/ numc.

Regards,

Rich Heilman

Read only

0 Likes
2,054

Hi Rich,

I have to disagree with you on the point that you say this is a general principle of programming. In Java for instance you can split a string in to chars and convert each to a short (if I remeber it correctly). In addition it should be possible to convert each char to an ASCII representation and store it in int. Anyhow, I thought that ABAP might have something similar like that where I can execute a function module simply to convert char to an integer. Yes, it is possible to assign number to letters and than code up the logic (as you suggested) but I do not want to handle it myself. I was hoping there is a function module for it.

Thanks for you input though.

Georgy

Read only

0 Likes
2,054

<i>I have to disagree with you on the point that you say this is a general principle of programming.</i>

I think there is a mis-communitcation in this regard, I was saying that the basic principle is that you can not store a non-numeric value in a numeric field.

Regards,

Rich Heilman

Read only

0 Likes
2,054

Hi Rich,

Gotcha 😃

Thanks for helping.

P.S. It would be nice to have some utilities like that in ABAP.

Read only

0 Likes
2,054

I agree, it would be nice. You might find this link of some interest.

http://www.sap-img.com/abap/how-can-i-get-ascii-value-of-any-letter.htm

Regards,

Rich Heilman

Read only

0 Likes
2,054

Nice!

Thank you,

Georgy