Ascii in java

hi there,
I do have a question to ask you regarding the ascii. Basically i've written a java program that send a bunch of ascii text out to seiral port. The serial port is connecting to plc. I've try to send this from hyper terminal and it works fine. The problem is when i try to do the same thing but in java instead the program didn't work as expected. This is the list of command that i've sent from hyper terminal " alt-2(stx)100A00135alt-3(etx)CE " and plc output lit at y0,y2, y4,y5. I've try to do this exact command in java which is :
String mn = "02100A0013503CE" ;
output.write(mn.getBytes());
and this does not work ( the indicator on y0,y2,y4,y5 did not lit ) . I've also try to work around this using
String mn = "02 1 00A0 01 35 03 CE" ;
and String mn = "STX100A00135ETXCE" ;
and String mn = "STX 1 00A0 01 35 ETX CE" ; and none of it work at all. Do i needed to insert a special command which make sure that stx and etx is sending properly rather than sending s and then t and then x
please help
sincerely yours
sunsern

Take a look at portmonitor at http://www.sysinternals.com/ntw2k/freeware/portmon.shtml this will let you watch what is send over your serial line. That way you can see what you are actually sending and see what is wrong with it. That will help you a lot.
As stated before, java strings(and chars) are Unicode, not ascii. However, the getBytes function returns ascii bytes on my system. You might get in trouble here when strange character sets are used, like hebrew or chinese, but i guess thats not a problem for you.
What goes wrong anyway is the way you include the special stx and etx characters in your string. you can't type these characters so you have to say you want character code 02 and 03 in you string like this:
String mn = (char)0x02 + "100A00135" + (char)0x03 + "CE";
output.write(mn.getBytes());That should do the trick...

Similar Messages

  • Using ASCII in java

    How can i use ascii in java programming. For example i want convert binary to decimal and use operator such XOR etc to campare to another decimal number. After that convert into ascii code.
    thank you

    If you have a string, then you can convert it into an array of char. Chars are just numbers. (They're numbers that represent unicode characters, but they're still numbers.)
    And all the primitive types in java are essentially binary; you don't have to convert anything to binary unless you mean to convert it for display purposes.
    If you have a string and want to convert it to ascii, then you can specify the encoding type (ascii) while converting to bytes or writing output to a file, etc.
    Hope this helps.

  • Packed Number Sign Format to Ascii equivalent  java program

    Hi
    I have a requirement to convert the packed Number Sign Format(usually used in MainFrame Platforms) to Ascii equivalent .
    please help me out with java code or steps to be done to achieve these task
    thank's
    habeeb
    [email protected]
    [email protected]

    Already answered in other thread.

  • How to find file type (Binary/Ascii) using Java IO

    Hi
    I want to find whether the file is binary file or not.
    Can any one please help.
    Thanks,
    Joy

    Encephalopathic wrote:
    I'm no IO expert, but aren't all files binary?Yes.
    I think what the OP is after is how to tell if a file contains only characters (e.g. ASCII characters). This can be done by reading the file and checking if every byte is a valid ASCII character more or less. An example would be limiting byte values to 9 (tab), 10 (LF), 12 (FF), 13 (CR), and 32-127.

  • Java.io.io​exception error on third party twitter app

    Device info
    My carrier:
    Blackberry 9360
    smartphone(3g,wi-fi)
    7.0 Bundle 1555 (v7.0.0.319, platform 8.0.0.359)
    3G Bands 1,2,5,6
    Cryptographic kernel v3.8.7.0
    Branding version: 1.0.107.263H
    Apps and free space
    Ubersocial (third-party twitter app) free memory : 105682514 Bytes (Yes, a battery pull solved the problem, but its keeps coming back)
    App: Ubersocial for Twitter
    Vendor:UberMedia, Inc
    Version: 1.252
     I just bought my Blackberry Curve 9360. I'm currently using the data plan offered by Starhub. I am not using the blackberry internet service or their data plan. Ubersocial worked perfectly fine with wifi, however when i'm outside and i use data to run the application, after a few hours of using the app, the java.io.ioexception error pops up. i tried allowing permissions an also tried changing the network setting for the app, but it still doesn't work. the app only starts working again only after i do a battery pull.
    Is there a particular setting i need to change in order for the Ubersocial app to run smoothly? Because currently this is giving me lots of problems as i would have to keep pulling the battery whenever the app stops working.

    Hence my question, which you haven't answered. Are you sure the files weren't corrupted in transfer? How did you do that? There are lots of ways to corrupt them, e.g. FTP in ASCII mode, Java with Readers and Writers, ... Also what JREs are running in the WIndows and Linux environments?

  • Read Binary File, Help me! Thanks!

    I want to read a binary file and tried several times,
    but failed, who can give me an example?
    Thank you !

    Thank you very much! my codes are listed below
    i want to read a binary file and convert to ASCII.
    import java.io.*;
    public class FileInputDemo
    public static void main(String args[])
    if (args.length == 1)
    try
    FileInputStream fstream = new FileInputStream(args[0]);
    DataInputStream in = new DataInputStream(fstream);
    while (in.available() != 0)
    System.out.println(in.readLine());
    in.close();
    }catch (Exception e)
    {System.err.println("File input error");}
    else
    {System.out.println("Invalid parameters");}
    }

  • Convert ByteArray to String Problem on Linux

    Hi there,
    i've got a problem converting a bytearray to string with this code:
    new String(byteArray, "ASCII");the Problem is that java doesn't print the whole string that is passed through the array because there are zeros in the byte array...
    is there any solution for this?
    as i mentioned i'm using ubuntu linux with sun java 6 1.6.0.06
    greets
    xtremliep

    it's just a bytearray which comes from a gateway to an ibm mainframe...
    on the mainframe runs a cobol programm which uses ebcdic code...
    the gateway converts it to ascii and java on win platforms shows a cube sign on the output...
    now i want to get it work on linux too...
    maybe i had to iterate trough the byte array and transform the zero into 32 which stands for space...
    but i think that is not a nice way....
    Edited by: xtremliep on May 5, 2008 10:56 PM

  • Encoding chars

    I have a basic question about Internationalization.
    We are using 1.3.1. I am writing an xml file. However there will be international characters. Instead of writing, for instance, the "A" with the 2 dots, I would like to write Ä. I see that 1.4.1 has some nice classes in the packages java.nio.*. I am assuming I would use CharsetEncoder if I could use 1.4.1. However we have to use 1.3.1. Does anyone have any suggestions on how to write encoded chars?
    thanks in advance

    (in the java source code)
    =========================
    You have two choices to type non-ascii char.
    1) Use unicode-encoded(\udddd) notation for non ascii char.
    You can find \u notation from unicode.org. If you have a native file that contains non-ascii, please use native2ascii tool that comes with J2SE. It's under [jdk]/bin dirrectory. You can find usage doc from,
    http://java.sun.com/j2se/1.3/docs/tooldocs/tools.html
    2) If your java source code editor can support non-ascii chars, just type the non-ascii and save it as native encoding. When you compile the code you should use -encoding option to correctly compile the native encoded file.
    (print out non ascii chars)
    ===========================
    Java uses "unicode" internally. When a char is printed out from the jvm, the char is converted to the underlaying platform's default enclloding. There are only few java methodes that can override this default behavior. One of them is OutputStreamWriter(OutputStream out, String encode) in jdk1.3.0. You can print out data as the encoding you passed. Since you are printing out to xml, "utf-8" would be ideal.
    thanks,
    baechul

  • Can't create received Adobe Offline Form e-mail

    Hi experts,
    My setup is as follows:
    CE 7.2 BPM System with Adobe Offline Forms, sent from system through smtp and received by imap.
    When I recieved my Adobe Form task in my personal inbox, I modify it, press submit and I send the composed email with the pdf to the default receiver with default composed text.
    Problem:
    When the system receives the email, the following error occurs in the Last 24 hours (Java) log.
    "Cannot create e-mail, because: charset=us-ascii [EXCEPTION] java.io.UnsupportedEncodingException: charset=us-ascii"
    To submit the pdf, button has to configured to submit the file as .pdf file. Standard the encoding with submit as pdf is UTF-16 (not possible to change the encoding when saving as pdf in LiveCycle Designer 2)
    Any clues where to search/fix this problem? Because the process stops at that step.
    Thx.
    Full trace & IMAGE of trace: http://i54.tinypic.com/ej7ij5.png
    Cannot create e-mail, because: charset=us-ascii
    EXCEPTION
    java.io.UnsupportedEncodingException: charset=us-ascii
    at sun.nio.cs.StreamDecoder.forInputStreamReader(StreamDecoder.java:52)
    ... (see image for complete trace)
    Category: com.sap.bpem.base.mail.MimeMessageBuilder
    Application: sap.com/tcbpembase~ear

    This just happened to me this morning during my commute. I read about 10 of my e-mails fine, but when I went to the next unread e-mail, the body of the e-mail was blank. I changed the preview to five lines and could see some of the text in the preview, but when I opened the e-mails, the body and subject lines are blank. It's been weeks since I upgraded to iOS4, so I don't know if it's related or not.
    When I get home tonight, I might try resetting the iPhone.
    ^MD

  • Can this class run fast than Hotspot ?

    My case in Sun hotspot is almost 2 times fast than jRockit. It's very strange.
    package com.telegram;
    public class byteutils {
         public final static byte[] bytea = { 48, 49, 50, 51, 52, 53, 54, 56, 57,
                   58, 65, 66, 67, 68, 69, 70 };
         public byteutils() {
              super();
         * convert length = 2L letters Hexadecimal String to length = L bytes
         * Examples: [01][23][45][67][89][AB][CD][EF]
         public static byte[] convertBytes(String hexStr) {
              byte[] a = null;
              try {
                   a = hexStr.getBytes("ASCII");
              } catch (java.io.UnsupportedEncodingException e) {
                   e.printStackTrace();
              final int len = a.length / 2;
              byte[] b = new byte[len];
              int idx = 0;
              int h = 0;
              int l = 0;
              for (int i = 0; i < len; i++) {
                   h = a[idx++];
                   l = a[idx++];
                   h = (h < 65) ? (h - 48) : (h - 55);
                   l = (l < 65) ? (l - 48) : (l - 55);
                   // if ((h < 0) || (l < 0)) return null;
                   b[i] = (byte) ((h << 4) | l);
              a = null;
              return b;
         public static String convertHex(byte[] arr_b) {
              if (arr_b == null)
                   return null;
              final int len = arr_b.length;
              byte[] byteArray = new byte[len * 2];
              int idx = 0;
              int h = 0;
              int l = 0;
              int v = 0;
              for (int i = 0; i < len; i++) {
                   v = arr_b[i] & 0xff;
                   l = v & 0xf;
                   h = v >> 4;
                   byteArray[idx++] = bytea[h];
                   byteArray[idx++] = bytea[l];
              String r = null;
              try {
                   r = new String(byteArray, "ASCII");
              } catch (java.io.UnsupportedEncodingException e) {
                   e.printStackTrace();
              } finally {
                   byteArray = null;
              return r;
         public static void main(String[] argv) {
              byte[] a = new byte[0x10000];
              for (int c = 0; c < 0x10000; c++) {
                   a[c] = (byte) (c % 256);
              String s = "";
              int LOOP = 10000;
              long l = System.currentTimeMillis();
              for (int i = 0; i < LOOP; i++) {
                   s = convertHex(a);
                   a = convertBytes(s);
              l = System.currentTimeMillis() - l;
              double d = l / (double) LOOP;
              System.out.println("" + d + "ms.");
    }

    Thanks! Your code is essentially a microbenchmark testing the performance of sun.nio.cs.US_ASCII.Decoder.decodeLoop() and encodeLoop(), with ~35% and ~30% spent in those two methods respectively. I have verified the behavior (i.e. Sun is faster than JRockit). Due to the microbenchmark nature, it may not affect a larger running program, but it may merit a closer look regardless. I have forwarded to the JRockit perf team for analysis.
    -- Henrik

  • Problem with java, ASCII and Linux

    Hi Friends,
    I has a Linux RedHat 9.0 with a jre1.5.0_04 (rpm package of Sun).
    I has a problem with ASCII , for example :
    import java.io.*;
    public class HolaMundo
    public static void main (String[] args)
    System.out.println("Hol� M�ndo");
    this programs runs ok on my windows jdk so it prints "Hol� M�ndo", but when i run the same HolaMundo.class program on my linux redhat it prints "Hol�� M��ndo"
    I think the problem is with the ASCII table that uses the linux version of jre, but i dont know how to solve this problem. I need a Spanish-European ASCII table on my application but i think it is working with a US-ASCII table.
    Then i has installed a kaffe 1.0 (rpm) java machie on this linux and this solve the problem but i has another problems of compatibility with this old version of java kaffe.
    Do you know whats happening?
    Thanks in advance.

    The problem doesn't have to do anything with Java or Linux as far as i can see. It's more likely a problem with Windows XP and IE. Be assured that normally downloading the Linux JDK in windows is not a problem.

  • Java / Linux - why ??? instead of non-ascii symbols

    Hi All,
    I've run into the problem during creating a new file from Java program on Linux gentoo.
    All non-ascii characters in the file name are converted into ??? symbols.
    I've tried to run the program with -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8, and set LC_ALL='UTF-8' - but the problem still exists.
    At the same time I can create the files with non-ascii characters in the file manager.
    Could you advise how can I fix this issue?
    Regards,
    Boris

    Is it related to File name or File content ?
    If it is file name related then Linux might have restriction on File name character !!.
    About File Content : Make one File reader program using Java and check are you able to read it back. if yes then It will be fault of your FileManager which is not correctly interpret content of your file.

  • Pevent  ASCII like • from getting coverted to special chars in Java

    Hi
    How can I prevent some ascii codes like '&#149;' from being converted to special characters like '?' in Java.
    This happens when I try to retrieve a text mesage having an ascii code from my previous page and display it on the browser.
    Can anyone of you tell me how to prevent this..?
    Thanks,
    Ravi

    Hi Gil,
    Thanks for your suggestion.
    Is there something different I need to do to read the characters correctly.
    My input html format text file has '&#149;' ascii codes in them which should actually display as a bullet on the browser.
    My java program takes this input message as a string and sets it in a java bean.I am trying to display the same bean information in the next page where I see a '?'
    instead of a bullet.Strangely when I view the source in the browser, I see it has
    converted from '&#149;' to '.' (a bullet character) in my 2nd page..I think this is the reason for appearing as '?' character.
    My input html format text file the folowing header info
    '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'
    Let me know if I need to do some encoding or escaping the ascii codes to prevent it from not getting converted to '.' in the source or '?' on the browser.
    Thanks,Ravi

  • Can Java be started in a directory that contains non ascii char

    I installed a product developed using Java in a folder whose name contains non-ascii chars, such as Japanese chars or german chars.
    This will cause error said: unable initialise java virtual machine, error code -1
    Some one said Java doesn't like being started in a directory that contains non ascii characters. There appears to be no way of passing it unicode parameters.
    Is there anyone once hit the similiar issue or know the root cause of such problem?
    Thanks

    Yes you can use your web start application console. To enter data which is required for your application it is better idea to use java application which runs in console mode althou you may try to run console of windows and then read data from its input stream.

  • Does Java 5 accept non-ascii chars as identifiers?

    I am surprised to find out that Java 5.0 accept non-ascii chars as identifiers.
    Is it true that Java 5 really accept non-ascii chars?
    Thanks.

    Here is the code:
    public class non��������name {
      private static void ��������(){
        System.out.println("this is called from a function with a chinese name!");
        int ����1 = 1, ����2 = 2;
        System.out.println("����1 = " + ����1 + ", ����2=" + ����2 + ", �� = " + (����1+����2));
      public static void main(String[] args) {
        ��������();
    }

Maybe you are looking for