Printing Original Byte
Nov 20, 2014Why when I do this:
System.out.println((byte) 99);
I still get: 99 ... not the hex representation of the byte!!
Why is that?
Why when I do this:
System.out.println((byte) 99);
I still get: 99 ... not the hex representation of the byte!!
Why is that?
So In my program I have a String called original and I am supposed to prompt the user for a sentence and then Make a StringBuilder instance called “originalReversed” based off of original. Then I have to do is to "Manipulate originalReversed so that it contains the value of original in reverse. This is done with a single statement."
import java.util.Scanner;
public class StringBuilderPractice {
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
String original;
System.out.println("Please enter a sentence: ");
original = input.nextLine();
}
how to create the instance but not the second part where it says manipulate the value for the first part I think it might be
StringBuilder originalReversed = new StringBuilder(original);
Inside a While Loop (While Loop begins if a condition is True) I have an If Statement, the else part of the statement assigns False to the condition I mentioned earlier.
But I've tried de-bugging and even when the code goes through the else part of the statement, firstly it returns False but then it returns to the beginning of the loop as True again.
I'm doing a software Java GUI - JFrame form like this:
1. The user wants to click on a button that opens a bunch of images that will be displayed as thumbnail in the bottom of the JFrame .
2. Then the user wants to select/click one of the thumbnail and make appear the corresponding image in it's original size on above(center) of the JFrame.
For doing this I used 3 JPanel.
One contains a JButton that opens the jfilechoser dialog window,
the second "panelPreview" is for putting the thumbnails created,
and the third "panelGrande" is for the image in it's original size.
The firs part "1." is ok.
But in the second part : I got one error when I want to put the ImageIcon in to the JLabel with the further intent of displaying it.
lblBig(imgIcoVett[i]);
In this project I'm dealing with arrays of ImageIcons and JLabels, so it's a bit advanced level for me, so I'm not sure that I wrote right the part of the MouseListener too.
The error displayed by netbeans says "cannot find symbol symbol: method lbl (ImageIcon) local variables referenced from an inner class must be final or effectively final"
Here I attach the project I did with netbeans"AAAD Unlayout 2.zip", but if you just need the highlight of the code, here it is too:
private javax.swing.JButton btnOpenfile;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel lblBig;
private javax.swing.JPanel panelGrande;
private javax.swing.JPanel panelPreview;
[Code] ....
Is there an advantage in using byte instead of int beyond the space savings? In my program, I'll never need close to the max value of a byte, let alone int, so it seems like a waste to make my primitives ints.
View Replies View RelatedI am working on a program that simulates a bug moving along a horizontal line, My code works correctly when I test it in it's own class but when I tried testing my constructor and methods in a test class I received an error saying, "package stinkBug does not exist" on lines with my methods. However, stinkbug is not a package.
Java Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
[code]....
The problem is the result of encoded message can't not restore to the original message by the decoder. Here are my three class's code
SecureMsgMain:
package securemsg.core;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import securemsg.database.*;
public class SecureMsgMain {
[Code] .....
It's probably really obvious, like it usually is, but I can't figure out why I am getting these errors on multiple functions.
if (!client.lowMem) {
for (int l = this.onDemandFetcher.getVersionCount(2), i2 = 1; i2 < l; ++i2) {
if (this.onDemandFetcher.method569(i2)) {
this.onDemandFetcher.method563(1, 2, i2); //Error
[Code] ....
The error I get on this line of code is 'Custom may not have been initialized', but no matter what I do, the error sticks.
Custom.cacheIndex = (Custom.cacheIndex + 1) % 10;
final Custom Custom = Custom.cache[Custom.cacheIndex];
//^^^^^
I need to send a byte array across a network. I know how to do this. (server->client)
byte[] myArray = new byte[]{0,1,2,3,4,5,6,7,8,9};
DataOutputStream.write(myArray);
... and I know how to receive it.
byte[] myArray = new byte[10];
DataInputStream.read(myArray);
When I send over one of these arrays, it ends up stopping storing values in the array when only about half the array is received, even though the array is sent from the server all at once. This results in the receiver's array, in this case, being something like {1,2,3,4,0,0,0,0,0,0}.
I can easily solve this - and already have - by simply adding a loop onto it and waiting for the bytes to all be received, as the method returns the amount of bytes actually read.
byte[] myArray = new byte[10];
int bytesRead = 0;
while( bytesRead < myArray.length) {
bytesRead += DataInputStream.read( myArray, bytesRead, myArray.length - bytesRead );
}
I am simply wondering if there is a better solution to this, as the current solution isn't that elegant. Did I do something wrong to cause only a part of the array to be sent first? Would it be better to use DataInputStream.readByte() to read off the bytes one by one rather than an array at once, and then store them in an array afterward? Would this cause a performance decrease as each byte is read individually? (I'm sending an array of several million bytes)
I am trying to make a function to tell me how to know if a byte is powered on or not but it must be through a mask depending on the position you send.
bool estaEncendido(char byte,int pos)
{
// byte = byte<<(7-pos) ;
//byte = byte>>(pos+1);
int mask=1;
int result =1;
for(pos=0; pos<8; pos++, mask <<=1)
[Code] ....
In the main should have been as well
System.out.print(estaEncendido(5/*000000101*/,2/*00000100*/));
/*00000101 */
/*00000100 &*/
/*00000100*/This ignition
I am getting byte stream as below. These looks like UTF 8 bytes
3C524F4F543E3C535452494E473E54455354204F4E4C5920535452494E473C2F535452494E473E3C2F524F4F543E
I want java code which will convert above bytes to string as shown below
<ROOT><STRING>TEST ONLY STRING</STRING></ROOT>
I am new to Android. I have byte array of size 10. I am passing the Decimal values (131 - 140) to byte array. But while printing I get Negative (-) values with decreasing order .
How can I get same value as positive values?
Or How can I store positive value e.g. 131 as byte array element.
Please not my requirement is array must be ByteArray only
I have a checksum function that is suppose to read IPV4 packet and return a short integer value. The IPV4 packets are stored in a byte array. I am having trouble storing the first 8 bits and second 8 bits of the short integer into the byte arrays especially when they have leading 1s. For example, if my checksum returns 5571 (binary = 0001 0101 1100 0011) The first 8 bits is suppose to represent 195 but when I try to assign a larger integer type to a btye the information gets sign extended. The 195 turns into -61. I tried using bit addition like array[10] = array[10] & 0xff, but the result remains the same.
public static short checksum(byte [] a, int length) {
short sum = 0;
long data;
int i = 0;
while(length > 1) {
data = (((a[i] << 8) & 0xff00) | ((a[i + 1]) & 0xff));
sum += data;
[code]....
I am reading a book on Java and we are at a point where it is explaining type conversion in expressions. One of the examples shared has a byte being multiplied by itself and then assigned back to itself ...
byte b;
b = 10;
b = (byte) (b * b);
this is all good and dandy (that is, the code functions properly).
However, I am confused why I need to typecast here! Without the cast, the compiler screams, "Type mismatch: cannot convert from int to byte." Yet I haven't converted to an int?? It appears there was an implicit conversion.
The final value, 100, is clearly within byte's range of -127 to +127 isn't it? So I am lost as to what is the issue here.
I'm trying to create Web Services with Eclipse (Java Runtime 7 (also tried 8) Tomcat 7 (also tried 8).Web Service with parameter "byte[]" and return value "byte[]" works fine.Web Service with parameter "myOwnClass" and return value "myOwnClass" works also fine.But if I have a "byte[]" element in "myOwnClass" and I run my Client test program I get the following error:
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
faultSubcode:
faultString: java.lang.NumberFormatException: For input string: "MTExMTExMTExMQ=="
Why? I don't have any numeric element (such as int ...) in my class members!?
I have a msg object that contains an ArrayList<Integer> collection. However, in order to send the elements in the array over the udp socket, it needs to be sent as a byte[] array. So why am I using ArrayList<Integer> over byte array in first place? Well when I receive data from socket from embedded c program, I need to get an unsigned representation of the data, and thus I need to store it in integers, since bytes in Java are unsigned and unsigned chars in c that are greater than 127 will yield incorrect values in java. But when I send an ack back over the socket, I need to send the data back as bytes. So I convert the ArrayList<Integer> to a byte array:
Java Code: byte[] data = msg.toByteArray();
DatagramPacket response = new DatagramPacket(data, data.length,
packet.getAddress(), packet.getPort());
public class Gprs {
...
public byte[] toByteArray(){
[Code] ....
The problem is I get an "Cannot cast from Integer to byte" when trying to cast the integer to byte: data[i] = (byte)m_data.get(i);
How can I resolve this?
okay so it says that java int short and byte variables are the same thing. They take whole numbers. But what is the point of byte and short to even exist if int covers it all? Is the short and byte just for fun?
View Replies View RelatedIn this simple example, I print a byte array to String:
Java Code:
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public class PrintByteArray {
public void print(){
System.out.println(Charset.defaultCharset());
byte[] arr = {1, 2, 3, 4};
[Code] ....
However, Eclipse prints out strange boxes, which I was unable to copy into this message.
It's supposed to print out the values of the bytes. What am I doing wrong?
i have I byte array ,That I was getting from the gps packets , I need to convert that into jpg file
public static void writejpegfile(byte[] someByteArray) throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("image" + new Date().getTime() + ".jpg")
try {
[Code]....
I am trying to remove a line based on user input. myFile.txt looks like:
Matt
Brian
John
However when I enter "Brian" (to remove this line), It is deleted on the temp file (myTempFile.txt), but not renamed back to the original file (myFile).
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
[code]....
If you write
byte b = 100; it works (implicit conversion of implicit int literal 100 to byte.
But if you have a methodvoid bla(byte b){}
And want to invoke it with a literal (which is an int by default):bla(8) then there is no implicit conversion.
Is the byte b = 100; just an exception in Java? And is it the rule that one has to explicitely cast (narrow) integer literals when passing to smaller-than-int types?
I am trying to write a byte array to a blob field in my database.
Below code is not working for me because the object is actually 902 bytes, however, the database tells me that it is 11 bytes once committed.
How to write to the blob field in my database?
Java Code:
protected void addFingerprint(byte[] fingerprintBytes) {
Statement stat;
try
{
updateQuery = "UPDATE " + currentTable + " SET CLERK_FINGERPRINT_DATA_BLOB = '"
[Code] ....
I've a small question relating to type promotion I can't find an answer for on the web. Basically in your code if you have :
byte b = 0;
b = b + 1;
The compiler will complain about the result being an int which cannot be assigned to a byte. That I understand, as b on the right hand side of the expression is promoted to an int and the result of the addition is an int. However the following does compile :
byte b = 0;
b++;
Does the post increment not carry out the post increment as "give me the value of b and then add 1 to b" where I would have expected 'add 1 to b' to do the same integer promotion as the previous example ? The compiler will also allow the following
byte b = 0;
b += 1;
Again , why is no type promotion happening here ?
I'm trying to save a picture from byte arrays using RandomAccessFile. The file appears but doesn't open (like its corrupted).
I'm using the bittorent protocol which gives a SHA-1 hash that I compare all the bytes with to verify the data. All the bytes pass the hash check and all the hashes are checked. So I'm pretty sure I'm getting all the bytes correctly.
Is there anything I can do that could tell what's going wrong?
public RUBTClient(final TorrentInfo2 tInfo, final String outFileName) {
...
this.outFileName = outFileName;
File destined = new File(outFileName);
try {
destined_file = new RandomAccessFile(destined, "rw");
destined_file.setLength(tInfo.file_length);
} catch (FileNotFoundException e1) {
[Code] .....
I just wrote a small but working code to parse a multipart message with two files binary encoded.
Problem is, after splitting the content of the files is reduced to normal "alphabetic" digits, and i dont know why.
I just appended my source code and a test file ( multipart ). And the result of my parsing. (part_0 = json, part_1 = file, part_2 = file)
Unfortunately, i dont know, if the Spring FW provides an easier way of doing so at all. Haven't found it yet.
Java Code:
String requestUrl = "http://localhost:8888/";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
HttpEntity<String> entity = new HttpEntity<String>("test");
ResponseEntity<byte[]> response = restTemplate.exchange(requestUrl, HttpMethod.GET, entity, byte[].class);
[Code] .....
I and a friend are working with a project to create a file system, who manages a secondary memory simulated as a byte array in Java. We want the file system to be a hierarchical tree structure like in UNIX.
We have come quite far, but the paths are not handled correct. I seem to have mistaken the relative folder ./ for the root folder, but it should mean "working directory folder", ie, where I stand now. That is, if I stand in /dir1 as my "working directory" and make mkdir ./dir2 then should dir2 end up as subfolder in dir1. But with me it appears in the root.