Converting from Int to Hex doesnt work in Java - java

int diny6h = Integer.parseInt(Integer.valueOf(diny6).toString(), 10);
int diny7h = Integer.parseInt(Integer.valueOf(diny7).toString(), 10);
diny6h=diny6h-32;
diny7h=diny7h-32;
System.out.println(diny6h + " + " + diny7h);
}
Incoming: diny6=30 diny7=20
printed: diny6h=16 diny7h=00
What i want: diny6h=10 diny7h=00
What am i doing wrong here?
EDIT:
well.. the numbers are send as hexadezimals and received as decimals, because the other numbers in the block (not diny6 and 7, but diny1 to diny5) are needed as hexadezimals. but diny6 and 7 are needed as decimals but im not able to get them the way i want i want to send a 35(hex) it comes in as 53(dec) and should be pirnted out as 10(dec). Same issue: want to send a 20(hex) it comes as a 32(dec) and should printed as 0
In short:
I send the 35, received as 53, but i need the 35 to reduce it by 20 and get the 15... how do i do that?
EDIT:
I am sorry for my yesterdays cofusing. WHat i need is to convert my received value to a BCD-number... nothing with hex ^^ should i delete this question now?

nothing is wrong.
for diny6:
30(hex) - 32(dec) = 30(hex) - 20(hex) = 10(hex) = 16(dec)
similarly for diny7.
integers by default are printed in decimal, thats why you get 16.
if you want to print the number in hex format do something like:
System.out.println(String.format("%x",diny6));
update:
i'm afraid you don't fully understand mathematical bases. hex and dec are just representations, an int variable isn't decimal or hex - it is just a number.
1. read the string representation of the number.
2. do whatever computations you need (and dont concern your self with the base during this stage).
3. print the result either as decimal or hex using format strings.
4. read up about the subject.

Was my own fault, misunderstood the meaning of what i wanted to do and ignored some hardware relevant requirements. Question totally wrong asekd.

Related

Convert in reverse ascii to whole decimal in Java

Hi all and thank you for the help in advance.
I have scoured the webs and have not really turned up with anything concrete as to my initial question.
I have a program I am developing in JAVA thats primary purpose is to read a .DAT file and extract certain values from it and then calculate an output based on the extracted values which it then writes back to the file.
The file is made up of records that are all the same length and format and thus it should be fairly straightforward to access, currently I am using a loop and and an if statement to find the first occurrence of a record and then through user input determine the length of each record to then loop through each record.
HOWEVER! The first record of this file is a blank (Or so I thought). As it turns out this first record is the key to the rest of the file in that the first few chars are ascii and reference the record length and the number of records contained within the file respectively.
below are a list of the ascii values themselves as found in the files (Disregard the " " the ascii is contained within them)
"#¼ ä "
"#g â "
"ÇG # "
"lj ‰ "
"Çò È "
"=¼ "
A friend of mine who many years ago use to code in Basic recons the first 3 chars refer to the record length and the following 9 refer to the number of records.
Basically what I am needing to do is convert this initial string of ascii chars to two decimals in order to work out the length of each record and the number of records.
Any assistance will be greatly appreciated.
Edit...
Please find below the Basic code used to access the file in the past, perhaps this will help?
CLS
INPUT "Survey System Data File? : ", survey$
survey$ = "f:\apps\survey\" + survey$
reclen = 3004
OPEN survey$ + ".dat" FOR RANDOM AS 1 LEN = reclen
FIELD #1, 3 AS RL$, 9 AS n$
GET #1, 1
RL = CVI(RL$): n = CVI(n$)
PRINT "Record Length = "; RL
reclen = RL
PRINT "Number of Records = "; n
CLOSE #1
Basically what I am looking for is something similar but in java.
ASCII is a special way to translate a bit pattern in a byte to a character, and that gives each character a numerical value; for the letter 'A' is this 65.
In Java, you can get that numerical value by converting the char to an int (ok, this gives you the Unicode value, but as for the ASCII characters the Unicode value is the same as for ASCII, this does not matter).
But now you need to know how the length is calculated: do you have to add the values? Or multiply them? Or append them? Or multiply them with 128^p where p is the position, and add the result? And, in the latter case, is the first byte on position 0 or position 3?
Same for the number of records, of course.
Another possible interpretation of the data is that the bytes are BCD encoded numbers. In that case, each nibble (4bit set) represents a number from 0 to 9. In that case, you have to do some bit manipulation to extract the numbers and concatenate them, from left (highest) to right (lowest). At least you do not have to struggle with the sequence and further interpretation here …
But as BCD would require 8-bit, this would be not the right interpretation if the file really contains ASCII, as ASCII is 7-bit.

Making a prescriptionCode (variable) appear in specific format in java

sorry for the title but couldn't really express this in another way.
So, let's say I have a variable that represents a prescription's unique code. I already know that there a total of 400 prescriptions. So for every new prescription I would like that code to change by one. The first one I want it to be 001, the second one 002 etc. I know I can just set a static int but how can I make the 0's appear in the front so it prints 001 and not just 1? I am new to java so I might be asking a really stupid question. Thanks for your time!
You can format it with Java format specifiers.
Here would be the code to do that:
int prescriptionCode = 1;
System.out.println(String.format("%03d", prescriptionCode));
The string "%03d" is the format specifier. Going backwards, "d" indicates that the value you want here is a decimal, "3" indicates that you want the value to be 3 characters long regardless of its actual length, "0" means that you want to fill the remaining space the number doesn't take up with 0's.

Translate Hexadecimal transformation from Oracle SQL into Java code

In searching for an answer, I used the solution provided in the following link : How to format a Java string with leading zero?
I have the following code that needs to be translated into java:
TRIM(TO_CHAR(123,'000X'))
From what I can tell, it translates the number into hexa and adds some leading zeros.
However, if I give a big value, I get ##### as answer, e.g. for the following code:
TRIM(TO_CHAR(999999,'000X'))
In Java, my current solution is the following:
String numberAsHex = Integer.toHexString(123);
System.out.println(("0000" + numberAsHex).substring(numberAsHex.length()));
It works fine for small numbers, but for big ones, like 999999 it outputs 423f. So it does the transformation, resulting the value f423f and then it cuts a part off. So I am nowhere near the value from Oracle
Any suggestion as to how to do this? Or why are ##### displayed in the Oracle case?
Instead of Integer.toHexString I would recommend using String.format because of its greater flexibility.
int number = ...;
String numberAsHex = String.format("%06x", number);
The 06 means you get 6 digits with leading zeros, x means you get lowercase hexadecimal.
Examples:
for number = 123 you get numberAsHex = "00007b"
for number = 999999you get numberAsHex = "0f423f"

Convert string to float occurs NaN error in Processing

I meet a problem in the Processing, and when i convert the value(string) into float, the first value is good, but the rests are all NaN. I could not find a way to solve this. And i print the string value for test. And it is correct, but after i convert it into float. It will be NaN.
ps: the value is from the serial, i connected my Arduino with Proceesing.
following is a part of codes
while(myport.available() > 0)
{
myString = myport.readString(); //read the string from serial
num = float(myString); // convert the string into float
print(num); // print the num(float), but the first
// value is good, rests are all `NaN` .
//print(myString); // print string, all the values are good
print(' ');
if(myString != null)
{
//num = float(myString);
storeData(myString);
//println(myString);
//print(data[i - 1]);
//println(' ');
delay(1000);
}
}
following is the result
conversion finshed:
not convert, only print string value
following is arduino code
sum = sqrt(Xg*Xg + Yg*Yg + Zg * Zg);
sum *= 10;
sum = (map(sum, 0, 1024, 0, 5000)/10.0);
Serial.println(sum);
delay(100);
I think that the problem is not inside Arduino but inside the Processing code.
I was looking a lot and I note that there is an error that can most likely solve your problem.
You used val = myport.readString(); instead of val = myport.readStringUntil('\n'); .
The differences are few, but in your case would be substantial.
Take a look at ReadString function and ReadStringUntil function.
Anyway, it is also suggested by the sparkFun tutorial.
P.S. Of course, in your Arduino code, you have to use (well, like you were doing) Serial.println(sum); instead of Serial.print(sum) because, in the last case, that would not send to processing nothing before a line feed has been sended.
I find my problem is inside the Arduino code, so i changed the way to send the data from Arduino. I used the println() to send the data. And that's the point lead to NaN. I serached on google, and then i tested different ways to change the way to send until i finded this link:http://www.varesano.net/blog/fabio/sending-float-variables-over-serial-without-loss-precision-arduino-and-processing
And thanks fabio's blog, his blog's introudces a good way to solve this problem. If you have the same trouble, maybe you can fixed by this.

small java problem

Sorry if my question sounds dumb. But some time small things create big problem for you and take your whole time to solve it. But thanks to stackoverflow where i can get GURU advices. :)
So here is my problem. i search for a word in a string and put 0 where that word occur.
For example : search word is DOG and i have string "never ever let dog bite you" so the string
would be 000100 . Now when I try to convert this string into INT it produce result 100 :( which is bad. I also can not use int array i can only use string as i am concatinating it, also using somewhere else too in program.
Now i am sure you are wondering why i want to convert it into INT. So here my answer. I am using 3 words from each string to make this kind of binary string. So lets say i used three search queries like ( dog, dog, ever ) so all three strings would be
000100
000100
010000
Then I want to SUM them it should produce result like this "010200" while it produce result "10200" which is wrong. :(
Thanks in advance
Of course the int representation won't retain leading zeros. But you can easily convert back to a String after summing and pad the zeros on the left yourself - just store the maximum length of any string (assuming they can have different lengths). Or if you wanted to get even fancier you could use NumberFormat, but you might find this to be overkill for your needs.
Also, be careful - you will get some unexpected results with this code if any word appears in 10 or more strings.
Looks like you might want to investigate java.util.BitSet.
You could prefix your value with a '1', that would preserve your leading 0's. You can then take that prefix into account you do your sum in the end.
That all is assuming you work through your 10 overflow issue that was mentioned in another comment.
Could you store it as a character array instead? Your using an int, which is fine, but your really not wanting an int - you want each position in the int to represent words in a string, and you turn them on or off (1 or 0). Seems like storing them in a character array would make more sense.

Categories

Resources