Converting chars to ints, get a strange math error...
Here's the code I have.
import java.util.*;
public class Encryption {
public static void main(String[] args) {
Scanner sc1 = new Scanner(System.in);
System.out.print("Enter a four-digit integer to be encrypted: ");
String encrypt = sc1.nextLine();
char a = encrypt.charAt(0);
char b = encrypt.charAt(1);
char c = encrypt.charAt(2);
char d = encrypt.charAt(3);
int as = new Integer(a).intValue();
int bs = new Integer(b).intValue();
int cs = new Integer(c).intValue();
int ds = new Integer(d).intValue();
int codea = (as + 7);
while (codea > 9) {codea = codea - 10;}
int codeb = (bs + 7);
while (codeb > 9) {codeb = codeb - 10;}
int codec = (cs + 7);
while (codec > 9) {codec = codec - 10;}
int coded = (ds + 7);
while (coded > 9) {coded = coded - 10;}
int finala = codec;
int finalb = coded;
int finalc = codea;
int finald = codeb;
String encrypted = ("" + finala + "" + finalb + "" + finalc + "" + finald + "");
//System.out.println ("The encrypted number is " + encrypted + ""); set aside while debugging
System.out.println(" " + a + " " + b + " " + c + " " + d + " ");
System.out.println(" " + as + " " + bs + " " + cs + " " + ds + " ");
}Now here's the console.
Enter a four-digit integer to be encrypted: 1234
1 2 3 4
49 50 51 52
When I went from char to int, 1 became 49, 2 became 50, etc...how did this happen?
class Fubar1
public static void main(String[] args)
System.out.println("'1' = " + '1'); // prints the char
System.out.println("(int)'1' = " + (int)'1'); // prints the ASCII (unicode?) number that represents that char
}
Similar Messages
-
I am dealing with this problem.
I need to read a file through this code:
byte[] b;
File file = new File(path);
FileInputStream inFile = new FileInputStream(file);
inFile.read(b);
But then i dont know how to convert the byte into int types.
For istance i have that b[0] = "0", and i would like to get the number 0 instead of the char.
Then i have that b[1] = "1", b[2] = "2",b[3] = "3", and i would like to get the whole number 0123.
How can i do?
Thank you in advance
-- AndyFirstly, you need to tell use what the format of the file is:
b[0] = "0" states that the first element in the byte array is a string containing a single 16bit character '0'.
Interger.parseInt(new String(b)); might work, but your description is not precise enough to say.
Pete -
Converting char to int - Easy surely?!
A very easy question I know but it's drving me round the bend!!!
I have searched the forums but the answers I have seen don't seem to work?!
Basically I have a section of code like this:
String argument = "-0";
int member = (int)argument.charAt(1);
member then gets populated with 48 and not 0. Why??!
Any help would of course be greatly appreciated.48 is the internal representation of the character '0'. This is not only true for the Unicode used by Java but also for the ANSI and ASCII codes. The character coded as 0 is often used as string terminator, e.g. for the standard C strings.
So if you want to retrieve the value of a digit character, you need to subtract 48. But make sure it is a digit, i.e. a character code between 48 and 57, both inclusive. You might also want to check out the method Integer.parseInt(String). -
Character c = new Character(x.charAt(k));
String s = c.toString();
int i = Integer.parseInt(s);
Is there an easier way to do it than this?Well, sort of int i = Integer.parseInt( Character.toString( x.charAt( k ) ) );andint i = (int)x.charAt( k ) - (int)'0'; // should use Character.isDigit( char c ) to ensure that k-th char is a digitandint i = Integer.parseInt( x.substring( k, k+1 ) ); // should use x.length() > k+1 to ensure that k+1 is a valid indexWhat do you want to do? Why don't you parse the whole String as inint i = Integer.parseInt( x );
-
Every time I try and convert a document, I get an 'unexpected problem' error message
The files I am trying to convert are 60MB (less than the 100MB limit referred to in the troubleshooting), and have no security locks on them. When I try and convert a file with the online tool, it uploads quickly, then spends a few minutes trying to convert, before a message pops up, saying that 'an unexpected error occurred'. No further information is given to help me fix the problem.
I am using Chrome, and in line with some of the help I have already seen on the forums, I have logged out then back onto the online tool a couple of times already.Hi Chris,
Sorry to hear you're having trouble! Could you try disabling OCR and converting your file again? You can find instructions on how to do this here: http://forums.adobe.com/docs/DOC-3062
Let us know if it helps!
-David -
Strange internal error on loading applet 2nd time
Dear all,
On two independent pages of my site, the same applet appears. Although most everything runs smoothly in both instantiations, I do get a strange internal error (and some minor but irritating misbehaviour) on loading the second instance only, and only in MSIE 6, not in Mozilla, not in Netscape 7.
The error messages generated are below. I can't see what might be going on, can you?
java.lang.InternalError: obsolete interface used
at sun.java2d.NullSurfaceData.getRaster(Unknown Source)
at sun.java2d.loops.OpaqueCopyAnyToArgb.Blit(Unknown Source)
at sun.java2d.loops.GraphicsPrimitive.convertFrom(Unknown Source)
at sun.java2d.loops.MaskBlit$General.MaskBlit(Unknown Source)
at sun.java2d.loops.Blit$GeneralMaskBlit.Blit(Unknown Source)
at sun.java2d.pipe.DrawImage.blitSurfaceData(Unknown Source)
at sun.java2d.pipe.DrawImage.renderSurfaceData(Unknown Source)
at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
at sun.plugin.AppletViewer.paintForegrnd(Unknown Source)
at sun.plugin.AppletViewer.paint(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Thanks for any light shed on the issue!Hello riskmits,
I have the same in my side and only occur to some clients.
I wonder how did you resolve this issue. Could you please send me a mail to [email protected]
Thanks! -
Why do I get a signing in error message
Everytime I try to convert a pdf I get a signing in error message
Hi bucko6,
What specific message are you seeing? Have you been able to convert any pdf's at this point? Did you confirm your subscription when you received your 'welcome to adobe' email?
Let me know how we can help!
Looking forward to hearing back from you.
Regards, Stacy -
Problem in converting char * to java objects
Hi All,
When I am trying to compile the following JNI code, I am getting the under-mentioned errors.
<code>
JNIEXPORT jboolean JNICALL Java_XXXAPI_nativeCheckSomeAccess
(JNIEnv *jnv, jobject jobj, jstring fresource, jstring uname){
jboolean accessible;
int ierror;
// convert jstring to char pointers
const char resname = (jnv)->GetStringUTFChars(jnv, fresource, 0);
const char u = (jnv)->GetStringUTFChars(jnv, uname, 0);
// pass parameters and check access
int canAccess = checkSomeAccess(resname, user, &ierror);
if(!ierror && canAccess == 1)
accessible = true;
else
accessible = false;
// release variables from JNI environment
(*jnv)->ReleaseStringUTFChars(jnv, fresource, resname);
(*jnv)->ReleaseStringUTFChars(jnv, uname, u);
return accessible;
the checkSomeAccess in "someCfile.h" actually looks like this:
int checkSomeAccess(char resname, char u, <enumeration> *ptr_to_enum_containing_integers);
</code>
When compiled the code... I am getting the following errors:
some_jni.c: In function `Java_XXXAPI_nativeCheckSomeAccess':
some_jni.c:65: warning: passing arg 1 of `checkSomeAccess' discards qualifiers from pointer target type
some_jni.c:65: warning: passing arg 2 of `checkSomeAccess' discards qualifiers from pointer target type
some_jni.c:65: warning: passing arg 3 of `checkSomeAccess' from incompatible pointer type
some_jni.c:67: `true' undeclared (first use in this function)
some_jni.c:69: `false' undeclared (first use in this function)
Please tell me, what is the problem here.
thank you,
Ran.Hi All,
How to convert an enum in C to Java-JNI ? I have the following enum defined in C.
typedef enum{
ERR_NOERROR=0,
ERR_INTERNAL,
ERR_NOOBJECT,
ERR_INVALIDOBJ,
}error_t;I am using this enum in my C function (in the header file) as:
int isSuccess(char* some_name, error_t *error);Now, while writing the JNI implementation for this, I have actually passed an integer which is giving compile time warning:
int result;
char *sn = (char*)(*env)->GetStringUTFChars(env, jstring_val, 0);
int errorCode;
result = isSuccess(sn, &errorCode);I would like to know, how to represent the enum type in JNI. It would be grateful If you provide an example code snippet.
thanks in advance,
Ran. -
Hi all,
i searched alot but couldn't find solution.
i have l_value char(45) field value has 4560
I need to convert this into vbak-netwr field in sales order user exit MV45AFZZ
when i assign vbak-netwr = l_value.
I am getting 45.60 in vbak-netwr field.
when i try same login in report progam it is working fine but not in user exit.
any suggestions?
GiriSample program to convert Char to Curr.
data: curr type kna1-umsa1,
char(254) type c.
data: temp type p length 15 decimals 2.
char = '1,405.25'.
REPLACE ALL OCCURRENCES OF '.' IN char WITH space.
REPLACE ALL OCCURRENCES OF ',' IN char WITH space.
CONDENSE char NO-GAPS.
temp = char.
curr = temp / 100.
write: 'CHAR = '.
write :/ char.
write:/ 'CURR = '.
write: curr.
<< Removed by moderator >>
Regards,
Uttam Agrawal
<< Removed by moderator >>
Edited by: uttamagrawal on Feb 22, 2011 9:21 AM
Edited by: Neil Gardiner on Feb 22, 2011 9:01 PM -
Convert Char (##) to P
Hi experts
I have to convert a char-variable to a p-variable.
The char-variable contains "##" (HEX = 001C).
Normaly it should be "1" and not '##'. Because when I put a "1" to the P-variable, I get the same hex-value like in the char.
When I say p-variable = char-variable, I get a dumpt CONVT_NO_NUMBER.
So, how can I convert the char-variable to the p-variable.
Thx in advance.
ChristianHi Christian,
The problem here lies in interpretation.
The same sequence of bytes (or bits) doesn't always mean the same. In your case 001C hex value is something different for character variable and means something other for p-var. It is the system who interprets this byte sequence.
With p var the system parses each byte as sequence of two digits (BCD - byte coded decimal). This means if we have such declaration
data p_var(2) type p value 12.
Its hex representation is 012C . 1 - is stored in memory on 4 bits (half a byte), 2 - is stored as 4 bits too. C - stands for sign of this variable (half a byte), 0 - is just added in front (half a byte). In total its 2 bytes.
If p_var was -12 then its hex would be 012D .
In contrary your c_var with hex 001C is UTF-16 representation of special character 1C which in [ASCII table|http://ascii-table.com] is not a printable one. That's why it is displayed to you as ##
What is more it doens't really make sense to convert p_var to c_var or vice versa, as system treats these two variables in totally different manner. It only make sense to take the integer representation of c_var . This is similar to saying: "please find this character in current code page or in Unicode and give me its position in this table (its integer value)".
So you can perform such conversion
data i_var type i.
data c_var type c value 'A'
i_var = c_var.
"i_var = 65
Only then you could map this i_var to your p_var but it really doens't make sense as the sytem would write it like 065C - p_var = 65
Hope you get the difference and will not persist on conversion b/w those two incompatible types
Regards
Marcin -
Convert Char to Date in SQL Server
Hello Experts,
I am trying to convert Char to Date but getting error in Universe designer. Can anybody advise please?
Thanks,
RaviHi,
Try with CAST() and CONVERT() functions. For more information refer use this url : http://msdn.microsoft.com/en-us/library/ms187928.aspx.
It is more easy to get solution if you can post your query.
Cheers,
Suresh Babu Aluri. -
CAST Not working for me - Arithmetic overflow error converting int to data type numeric - error
GPM is DECIMAL(5,2)
PRICE is DECIMAL(11,4)
COST is DECIMAL(7,2)
Trying to update the Gross Profit Margin % field and I keep getting the "Arithmetic overflow error converting int to data type numeric" error.
UPDATE SMEMODETAIL SET SMD_GPM = (SMD_PRICE-SMD_COST) / SMD_PRICE * 100
FROM SMEMODETAIL WHERE SMD_PRICE<>0 AND SMD_QUANTITY<>0
Example record:
SMD_PRICE SMD_COST GPM%
1.8500 1.62 12.4324324324324300
I added cast and I still get the error.
How do I format to get this to work?
Thanks!Hi GBerthume,
The error is caused by some value such as 1000.01 of the expression (SMD_PRICE-SMD_COST) / SMD_PRICE * 100 exceeds the
precision of the column(DECIMAL(5,2)). The example data doesn't cause the overflow error for the value of the expression is 12.43 which is in the scope of DECIMAL(5,2).
USE TestDB
CREATE TABLE SMEMODETAIL
SMD_PRICE DECIMAL(11,4),
SMD_COST DECIMAL(7,2),
SMD_GPM DECIMAL(5,2)
INSERT INTO SMEMODETAIL(SMD_PRICE,SMD_COST) SELECT 1.8500,1.62
UPDATE SMEMODETAIL SET SMD_GPM = (SMD_PRICE-SMD_COST) / SMD_PRICE * 100
FROM SMEMODETAIL WHERE SMD_PRICE<>0-- AND SMD_QUANTITY<>0
SELECT * FROM SMEMODETAIL
DROP TABLE SMEMODETAIL
The solution of your case can be either scale the DECIMAL(5,2) or follow the suggestion in Scott_morris-ga's to check and fix your data.
If you have any question, feel free to let me know.
Eric Zhang
TechNet Community Support -
Fm for converting char3 to int
suggest me a function module for converting char of length 3 to int
and a function module for converting from int to char 3Hi ,
You dont require a FM for that.
Simply do the following:
Data : l_temp(3) type c, " CHar
l_sum type i. " Integer
* To move char to integer
L_temp = '1234'.
move l_temp to l_sum.
* To move integer to char
clear : l_temp, l_sum.
l_sum = '23433'.
move l_sum to l_temp.
Best regards,
Prashant -
Can anyone tell me how you would keep all the characters from within one pair of tags together each time characters() is called?
the character() method doesn't read all the character data at once, and i would like to create a buffer of what it has read.
thank you.I recommend using getNodeName and/or/combination
with
getNodeValue to get the strings.i have tried using a buffer, and i have got that to work!
At long last I am seeing the string I would like to see as it is in the xml file!
thank you for your help
here is an example of my code:
private boolean isDescription = false;
StringBuffer sb = new StringBuffer();
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if(localName.equals("description")) {
sb = new StringBuffer();
isDescription = true;
public void characters(char []ch, int start, int length) throws SAXException {
if(isDescription == true) {
String desc = new String(ch, start, length);
sb.append(desc);
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
if(localName.equals("description")) {
isDescription == false;
System.out.println(sb.toString());
} -
How to convert String to int in JSP?
Hi,
I set a session attribute in Servlet and want use it in JSP, How can I convert it to int or Integer?
the line in my code doesn't work:
int quantity=(int)session.getAttribute("vehiclequantity") ;
Thanks in advance.
Wolf
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<HTML>
<HEAD>
<TITLE>Using the for Statement</TITLE>
</HEAD>
<BODY>
<H1>Using the for Statement</H1>
<%=session.getAttribute("vehiclequantity") %>;
<%
int loopIndex;
int quantity=(int)session.getAttribute("vehiclequantity") ;
out.println(quantity);
for (loopIndex = 1; loopIndex <=2; loopIndex++) {
out.println("This is iteration number "
+ loopIndex + "<BR>");
%>
</BODY>
</HTML>Learning how to read errors and understand them will help you solve your problems quicker by yourself... So lets take a look at the error and classes involved...
The error says:
"Cannor Resolve Symbol: method valueOf(java.lang.Object) in the class java.lang.Integer" and gives you line line where the error occurs: Integer quantity = Integer.valueOf(session.getAttribute("vehiclequantity"));
Now, if we look at the API for the Integer we notice that are are only two valueOf methods: valueOf(java.lang.String s) and valueOf(java.lang.String s, int radix). Not valueOf(java.lang.Object) method.
Now we look at the getAttribute(java.lang.String name) method of HttpSession we see that the method returns a java.lang.Object. Now, you know you put a java.lang.String into that attribute, but the get method returns an Object. This is because you could have put any object in there, an Integer, a String, or some other class instance. But you know it is a String, so you can cast the returned value to a String, so that you will be calling the valueOf(java.lang.String s) method of Integer with the Object returned from the HtttpSession's getAttribute(java.lang.String name) method:
Integer quantity = Integer.valueOf((String)session.getAttribute("vehiclequantity"));
Maybe you are looking for
-
How can I backup Adobe's software activation?
I have purchased Adobe Creative Suite CS6 for Windows and am using it on my desktop and notebook computer. This is the maximum number of computers I'm allowed to use it on. Since the software has an activation and deactivation feature and requires an
-
Business partner balance does not match journal entries message
Dear All, The error message "Business partner balance does not match journal entries" appears in customer receivable report print preview. This is happened While generating the Customer Receivables Aging report by sales documents. What is the main ca
-
Master data related fields for DP, SNP and PPDS needed in Material Master
Hi Gurus, Can any one please suggest or guide me what are the fields need to be done in Material master in ECC for SAP APO DP, SNP (heuristic with capacity leveling)& PPDS(Heuritic with optimizer). Please it will be really great help. T
-
I try to sync my ipod touch to my computer and no device shows up in itunes. On my device it says trust or don't trust. When I say trust, nothing happens. The folder comes up in the computer but no device in itunes? Please help!
-
Cannot see asset report year 2008
cannot see asset report year 2008 the system show that i must change fiscal year. but i want to see report year 2008 before calculate depreciation month 12.2008. what should i do ? please help.