Convert from int to byte in JavaScript

I have a small piece of code in Java,
int x = 216;
System.out.println((byte)x);
I'd like to convert this to JavaScript form and that means, I want to be able to get the exact answer from this code which is -40. How do I do that?

What do you mean with
I'd like to convert this to JavaScript formThere is nothing like a JavaScript form.
Within a JSP page you can write
<script type="text/javascript">
   alert("<%=String.valueOf((byte)216)%>");
</script>Which displays an alert box with the message "-40"

Similar Messages

  • Convert from int to String

    Hi, may i know how to convert from int to String??
    For instance, i've>>
    int i = 0;
    i++;
    String temp = i.toString();
    [Need to convert the int to String here. Am i doing the convertion correctly?? Pls correct me. Thanks!]

    Hi, may i know how to convert from int to String??
    For instance, i've>>
    int i = 0;
    i++;
    String temp = i.toString();
    [Need to convert the int to String here. Am i doing
    the convertion correctly?? Pls correct me. Thanks!]String temp = "" + i;

  • Convert from int to char

    Hi,
    How can I convert from an int to char?

    The challange is to convert from int to String without using any API or automatic conversionWoah, what a challenge ! Why ?
    Anyway, what about a recursive call and switch statement. Something likepublic String intToString(int n) {
        if(n<0) {
            return "-"+intToString(-n);
        if(n<10) {
            return getStringDigit(n);
        else {
            return intToString(n/10) + getStringDigit(n%10);
    private String getStringDigit(int n) {
        switch(n) {
            case 0:
                return "0";
            // etc.
    }

  • Converting from Int/Double to Wording Format

    Hi,
    Does anyone has any sample program to convert double values into wording (this functionality is used to convert currency value) up to Trillion value.
    For eg: 1000.90 One thousand And Ninety Cents
    I have written a program which can accept up to 1 Billion. This is because that 'int' can accept bits up to that level only. I am unsure how go beyond that because when I declare the type to double, the compiler gave me the same error.
    Thank you.

    Check
    http://www.rgagnon.com/javadetails/java-0426.html
    but consider keeping locale specific texts in properties
    file and access them through resource bundles.

  • Convert from int to letter

    Uhh ... i am getting a stream of int from an InputStream and I want to display them as letters ... they are ASCII values ... can I do this ? feel dumb ...
    thanks .,..

    Yup ... you sure can ... start reading the API with the String class and pay particular attention to the section on ASCII conversion.

  • How to convert from int to string

    Can anyone help me on how to convert int to string?
    Thanks,
    Q

    int i = 3
    String S = i + ""
    i will be promoted to String automatically when the above expression is evaluated

  • Converting from unsigned int / short to byte[]

    Can anybody help me, I am trying to convert from:
    - unsigned int to byte[]
    - unsigned short to byte[]
    I will appreciate your help lots. thanks.

    @Op.
    This code converts an integer to a byte[], but you have to consider the byte order:
            int value = 0x12345678;
            byte[] result = new byte[4];
            for (int i=3; i>=0; i--) {
                result[i] = (byte)(value & 0xff);
                value = value >> 8;
            }This is another option:
            int dummy = 7;
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            os.write(dummy);
            byte[] bytes = os.toByteArray();Kaj

  • Trying to understand the details of converting an int to a byte[] and back

    I found the following code to convert ints into bytes and wanted to understand it better:
    public static final byte[] intToByteArray(int value) {
    return new byte[]{
    (byte)(value >>> 24), (byte)(value >> 16 & 0xff), (byte)(value >> 8 & 0xff), (byte)(value & 0xff) };
    }I understand that an int requires 4 bytes and that each byte allows for a power of 8. But, sadly, I can't figure out how to directly translate the code above? Can someone recommend a site that explains the following:
    1. >>> and >>
    2. Oxff.
    3. Masks vs. casts.
    thanks so much.

    By the way, people often introduce this redundancy (as in your example above):
    (byte)(i >> 8 & 0xFF)When this suffices:
    (byte)(i >> 8)Since by [JLS 5.1.3|http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#25363] :
    A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T.Casting to a byte is merely keeping only the lower order 8 bits, and (anything & 0xFF) is simply clearing all but the lower order 8 bits. So it's redundant. Or I could just show you the more simple proof: try absolutely every value of int as an input and see if they ever differ:
       public static void main(String[] args) {
          for ( int i = Integer.MIN_VALUE;; i++ ) {
             if ( i % 100000000 == 0 ) {
                //report progress
                System.out.println("i=" + i);
             if ( (byte)(i >> 8 & 0xff) != (byte)(i >> 8) ) {
                System.out.println("ANOMOLY: " + i);
             if ( i == Integer.MAX_VALUE ) {
                break;
       }Needless to say, they don't ever differ. Where masking does matter is when you're widening (say from a byte to an int):
       public static void main(String[] args) {
          byte b = -1;
          int i = b;
          int j = b & 0xFF;
          System.out.println("i: " + i + " j: " + j);
       }That's because bytes are signed, and when you widen a signed negative integer to a wider integer, you get 1s in the higher bits, not 0s due to sign-extension. See [http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.2]
    Edited by: endasil on 3-Dec-2009 4:53 PM

  • Convert from String to Int

    Hi,
    I have a question.
    How do I convert from String to Integer?
    Here is my code:
    try{
    String s="000111010101001101101010";
    int q=Integer.parseInt(s);
    answerStr = String.valueOf(q);
    catch (NumberFormatException e)
    answerStr="Error";
    but, everytime when i run the code, i always get the ERROR.
    is the value that i got, cannot be converted to int?
    i've done the same thing in c, and its working fine.
    thanks..

    6kyAngel wrote:
    actually it convert the string value into decimal value.In fact what it does is convert the (binary) string into an integer value. It's the String representation of integer quantities that have a base (two, ten, hex etc): the quantities themselves don't have a base.
    To take an integer quantity (in the form of an int) and convert it to a binary String, use the handy toString() method in the Integer class - like your other conversion, use the one that takes a radix argument.

  • How to convert an int number to a byte array, and how to convert back?

    Hello, everybody,
    Just as the topic, if I have an int number(which should have 32-bit, or 4-byte), how can I convert it into a byte array. And also, how to convert it back.
    Thank you in advance!

    Alternatively you can use ByteBuffers to do this work for you
    // to a Byte Array
    int originalInt =...;
    ByteBuffer myBuffer = ByteBuffer.allocate(4);
    myBuffer.putInt( originalInt );
    byte myArray[] = myBuffer.array();
    // and back to an int
    ByteBuffer myOtherBuffer = ByteBuffer.wrap(myArray);
    int finalInt = myOtherBuffer.getInt();

  • Question on converting an int to 4 byte array

    Hello all. I apologize in advance if this information is readily available, because I haven't been able to find a definitive answer to this question. I need to convert an int to a big endian 4 byte-array. I have the following code, which I'm fairly sure works, but it's the big endian part I'm not sure about. Can anyone confirm that this code snippet will do what I expect:
    public static final byte[] intToByteArray(int value)
        return new byte[]
            (byte)(value & 0xff),
            (byte)(value >> 8 & 0xff),
            (byte)(value >> 16 & 0xff),
            (byte)(value >>> 24) };
    }Thank you very much in advance for your help.
    -Kennedy

    kook04 wrote:
    Hello all. I apologize in advance if this information is readily available, because I haven't been able to find a definitive answer to this question. I need to convert an int to a big endian 4 byte-array. I have the following code, which I'm fairly sure works, but it's the big endian part I'm not sure about. Can anyone confirm that this code snippet will do what I expect:The best way is to test it for yourself.
    int i = 0xAABBCCDD;
    byte[] bytes = intToByteArray(i);
    if (bytes[0] == (byte)0xAA && bytes[1] ... etc.) {
      System.out.println("Success!");
    else {
      System.out.println("Oops!")
    }

  • Multiplatform: how convert int into byte[]???

    Hi all,
    I need to convert an int value into an array of bytes. An int is represented by 4 bytes, but the problem is that depending on the platform, the most significant byte is the first or the last, so if you convert it directly, like:
    int i = 5;
    byte b[] = new byte[4];
    b[0] = (byte)( (i & 0xff000000) >>> 24);
    b[1] = (byte)( (i & 0x00ff0000) >>> 16);
    b[2] = (byte)( (i & 0x0000ff00) >>> 8);
    b[3] = (byte)( (i & 0x000000ff) );
    you cannot export it to another platfroms.
    Thank you in advance.

    Come on, nobody knows how to detect the byte order in a given platform?
    I've been all the day dealing with this, which means all day without producing.
    Please, it�s very urgent.
    Thank you again.

  • How to convert from java.lang.Integer to int

    Could you please show me
    how to convert from java.lang.Integer to int?
    and how to convert from java.lang.Integer to String?
    Thanks,
    Minh

    Could you please show me
    how to convert from java.lang.Integer to int?
    and how to convert from java.lang.Integer to String?Tip: always keep a browser open on the API docs; if you've got a
    couple of MBs to spare, download the docs; it's very convenient.
    kind regards,
    Jos

  • Converting the sample model type from USHORT to BYTE

    Hi,
    We're converting TIFF images into JPEGs and loading them into a database. This works except for cases where the SampleModel datatype of the image is USHORT when it throws:
    java.lang.RuntimeException: Only 1, or 3-band byte data may be written.
    at com.sun.media.jai.codecimpl.JPEGImageEncoder.encode(JPEGImageEncoder.java:145)
    The fact that it is not BYTE data is the problem I think. The image has 3 bands (see image details below this post).
    Does anyone know how to convert the USHORT to BYTES? Any other suggestions are also welcomed.
    GDAL_INFO tool tells us the following about the image
    C:\dmp>gdalinfo OsloQB1_1.tif
    Driver: GTiff/GeoTIFF
    Files: OsloQB1_1.tif
    Size is 7847, 7768
    Coordinate System is:
    PROJCS["UTM 32 V E012",
    GEOGCS["unnamed",
    DATUM["unknown",
    SPHEROID["unretrievable - using WGS84",6378137,298.257223563]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4030"]],
    PROJECTION["Transverse_Mercator"],
    PARAMETER["latitude_of_origin",0],
    PARAMETER["central_meridian",9],
    PARAMETER["scale_factor",0.9996],
    PARAMETER["false_easting",500000],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
    AUTHORITY["EPSG","9001"]]]
    Origin = (590809.199999999950000,6656298.599999999600000)
    Pixel Size = (0.600000000000000,-0.600000000000000)
    Metadata:
    AREA_OR_POINT=Area
    Corner Coordinates:
    Upper Left ( 590809.200, 6656298.600) ( 10d37'47.39"E, 60d 2'1.81"N)
    Lower Left ( 590809.200, 6651637.800) ( 10d37'39.98"E, 59d59'31.21"N)
    Upper Right ( 595517.400, 6656298.600) ( 10d42'51.44"E, 60d 1'57.97"N)
    Lower Right ( 595517.400, 6651637.800) ( 10d42'43.65"E, 59d59'27.37"N)
    Center ( 593163.300, 6653968.200) ( 10d40'15.62"E, 60d 0'44.62"N)
    Band 1 Block=7847x1 Type=UInt16, ColorInterp=Red
    Band 2 Block=7847x1 Type=UInt16, ColorInterp=Green
    Band 3 Block=7847x1 Type=UInt16, ColorInterp=Blue

    that means we are loosing high order bits Of course, you are trying to jam 10 bits into an 8 bit container
    so how come the most significant bit is carrying 1 to represent negative value.bytes in java are signed

  • How can I convert an array off byte into an Object ?

    Hi folks...
    I�m developing an application that comunicates a PDA and a computer via Wi-Fi. I�m using a DataStream ( Input and Output ) to receive / send information from / to the computer. Most off the data received from him is in the byte[] type...
    How can I convert an array off byte ( byte[] ) into an Object using MIDP 2.0 / CLDC 1.1 ?
    I found on the web 2 functions that made this... but it uses a ObjectOutputStream and ObjectInputStream classes that is not provided by the J2ME plataform...
    How can I do this ?
    Waiting answers
    Rodrigo Kerkhoff

    There are no ObjectOutputStream and ObjectInputStream classes in CLDC. You must know what you are writing to and reading from the DataStream. You should write the primitives like int, String to the DataOutputstream at one end and read those in exactly the same sequence at the outher end using readInt(), readUTF() methods.

Maybe you are looking for