Im trying convert mac adress to integer:
Result is: 2321591092112814
It should be: 255771439995918
Im trying:
String[] macAddressParts = device.getAddress().split(":");
Byte[] macAddressBytes = new Byte[6];
String macAddressString = "";
for(int i=0; i<6; i++){
Integer hex = Integer.parseInt(macAddressParts[i], 16);
macAddressString += hex.toString();
}
System.out.println(macAddressString);
// 2321591092112814
You cannot make an integer by catenating strings.
Try that :
String[] macAddressParts = "e8:9f:6d:d3:1c:0e".split(":");
Byte[] macAddressBytes = new Byte[6];
long addressAsInteger = 0;
for (int i = 0; i < 6; i++) {
Integer hex = Integer.parseInt(macAddressParts[i], 16);
addressAsInteger = addressAsInteger * 256 + hex;
}
System.out.println("Addresse as an integer : " + addressAsInteger);
It gives the right answer : 255771439995918.
Related
The title explains all, also, I have tried removing them
(because the text is there, but instead of "aldo" there is "al?do", also it seems to have a random pattern)
with (String).replace("?", ""), but with no success.
I have also used this, with a combination of UTF_8,UTF_16 and ISO-8859, with no success.
byte[] ptext = tempName.getBytes(UTF_8);
String tempName1 = new String(ptext, UTF_16);
An example of what I am getting:
Studded Regular Sweatshirt // Instead of this
S?tudde?d R?eg?ular? Sw?eats?h?irt // I get this
Could it be the website that notices the headless browser and tries to "spoof" its content? How can I overcome this?
It looks very likely that site you scrapping intent mix up the 3f and 64 characters into your result.
so you have to mask your self as a normal browser to scrapping or filter it out by replacing.
text simple
Sca???rfa???ce??? E???mbr???oi�d???ered L�e???athe
after filteration
Scarface Embroidered Leather
//Sca???rfa???ce??? E???mbr???oi�d???ered L�e???athe
//Scarface Embroidered Leathe
String hex="5363613f3f3f7266613f3f3f63653f3f3f20453f3f3f6d62723f3f3f6f69643f3f3f65726564204c653f3f3f61746865";
byte[] bytes= hexStringToBytes(hex);
//the only line you need
String res = new String(bytes,"UTF-8").replaceAll("\\\u003f","").replaceAll('�',"").replaceAll("�","");
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(new String(c));
}
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public String printHexString( byte[] b) {
String a = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
a = a+hex;
}
return a;
}
I want to implement my input reading method into my main class, I want use my code to parse. It's been fixed now. thanks.
String x;
int count = -1;=
while (str.hasMoreTokens()) {
count++;
x = str.nextToken();
word[count] = x;
System.out.println(count + ": " + word[count]);
}
System.out.println("---Frequency---");
// create unique words
for (int i = 0; i < 7; i++) {
if ((!Arrays.asList(unique).contains(word[i]))) {
unique[i] = word[i];
}
}
// measuring frequency
int[] measure = new int[10];
for (int a = 0; a < 7; a++) {
if (Arrays.asList(unique).contains(word[a])) {
measure[a] += 1;
System.out.println(unique[a] + " : " + measure[a]);
}
}
}
}
private List<String[]> termsDocsArray = new ArrayList<String[]>();
private List<String> allTerms = new ArrayList<String>(); //to hold all terms
private List<double[]> tfidfDocsVector = new ArrayList<double[]>();
/**
To start with your code
String text = "Professor, engineering, data, mining, research";
StringTokenizer str = new StringTokenizer(text);
String word[] = new String[10];
String unique[] = new String[10];
String x;
int count = -1;
while (str.hasMoreTokens()) {
count++;
x = str.nextToken();
word[count] = x;
System.out.println(count + ": " + word[count]);
}
System.out.println("---Frequency---");
// create unique words
for (int i = 0; i < 7; i++) {
if ((!Arrays.asList(unique).contains(word[i]))) {
unique[i] = word[i];
}
}
// measuring frequency
int[] measure = new int[10];
for (int a = 0; a < 7; a++) {
if (Arrays.asList(unique).contains(word[a])) {
measure[a] += 1;
System.out.println(unique[a] + " : " + measure[a]);
}
}
should be in it's own method like .
private void doSomething(){
//This variable will hold all terms of each document in an array.
String text = "Professor, engineering, data, mining, research";
StringTokenizer str = new StringTokenizer(text);
String word[] = new String[10];
String unique[] = new String[10];
String x;
int count = -1;
while (str.hasMoreTokens()) {
count++;
x = str.nextToken();
word[count] = x;
System.out.println(count + ": " + word[count]);
}
System.out.println("---Frequency---");
// create unique words
for (int i = 0; i < 7; i++) {
if ((!Arrays.asList(unique).contains(word[i]))) {
unique[i] = word[i];
}
}
// measuring frequency
int[] measure = new int[10];
for (int a = 0; a < 7; a++) {
if (Arrays.asList(unique).contains(word[a])) {
measure[a] += 1;
System.out.println(unique[a] + " : " + measure[a]);
}
}
}
Secondly in ur given code u have written like
int count = -1;=
which accounts to this error Syntax error on token "=", { expected.It should be
int count = -1;
And since all your code is simply written in class without any method so it is giving you the error saying { expected.
Please make sure you have copied the code correctly.
I'm copying my program to convert binary data to hex format and print the data in 16bytes format line by line and I'm getting StringIndexOutOfBounds Exception even after data has been printed for some extent please help me solve this problem.
byte[] buffer = new byte[16];
while((line = bis.read(buffer)) != -1)
{
for(int i = 0; i < line; i++)
{
value = Integer.toHexString(0xFF & buffer[i] | 0x100).substring(1);
sb.append(value);//I think here is the problem
}
if(a == 0)
{
incValue = sb.substring(0, 32);
System.out.println(incValue);
}
else
{
counter = counter + 32;
incValue = sb.substring(counter, counter + 32);
System.out.println(incValue);
}
a++;
Output:
5e9d094ec7a7349725b8300212a5048f
b9ce351dfb869a7db694755981f7fbd3
acad5008e54ebd80b82a9676ebd02a0f
4775a61e52c3129c4aba3af1f28c8ee0
9050718e15a8189321d626399ab2612f
212f89f4f9ff0015d03b625cfb990c8a
1c36dc8c13e636f4e74b6df4af853207
49ea39e78c727df55b6f0d5bc90a54fd
f7aba3f8f258496d0256400474236335
Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 216896
instead of this:
incValue = sb.substring(counter, counter + 32);
you can use this:
if((counter+32)< sb.length())
{
incValue = sb.substring(counter, counter + 32);
}
else
{
incValue = sb.substring(counter) //The String is shorter than counter+32 chars
}
I'm stuck with some code here and what I'm trying to do is convert a string into its ASCII value, subtract 30 from it and then convert back to a string.
E.g. Enter - hello
Convert to - 104 101 108 108 111
Subtract - 74 71 78 78 81
display - JGNNQ
Code:
import javax.swing.*;
public class practice {
public static void main (String[] args) {
String enc = "";
String encmsg = "";
String msg = JOptionPane.showInputDialog("Enter your message");
int len = msg.length();
for (int i = 0; i< len ; i++) {
char cur = msg.charAt(i);
int val = (int) cur;
val = val -32;
enc = "" + val;
encmsg = encmsg + enc;
}
JOptionPane.showMessageDialog(null, encmsg);
}
}
Thanks in advance
Couple things:
Change val = val -32; to val = val -30; to get the proper subtraction you want in the original problem statement.
Next, change
enc = "" + val; to enc = (char)val;
so that you can convert the value to a proper character. Before, you were just concatenating it to a string, which won't do any conversion. You also need to declare enc as a char at the top of your file.
The full working code should be as follows:
char enc;
String encmsg = "";
String msg = JOptionPane.showInputDialog("Enter your message");
int len = msg.length();
for (int i = 0; i < len; i++) {
char cur = msg.charAt(i);
int val = (int) cur;
val = val - 30;
enc = (char) val;
encmsg = encmsg + enc;
}
JOptionPane.showMessageDialog(null, encmsg);
is there any way to convert array list to json store input data using jsp and java for extjs.
means i need to get data for json store from jsp page and java arraylis
Here you go hope this helps you... Here im getting state values from db..
String response = "id,State#";
List stateList = new ArrayList<>();
//DB call
for (int i = 0; i < stateList.size(); i++) {
response += stateList.get(i).gets_code() + ",";
response += stateList.get(i).getS_name() + "#";
}
StringTokenizer st = new StringTokenizer(response , "|");
String finalMsg = null;
String str1 = null;
while (st.hasMoreElements()) {
String token = st.nextToken();
finalMsg = token;
}
JSONObject object = new JSONObject();
stbuffer.append("{\"root\":[");
String[] data = finalMsg.split("#");
int len = data.length;
String[] headings = data[0].split(",");
for (int x = 1; x < len; x++) {
String[] data1 = data[x].split(",");
int len1 = data1.length;
for (int y = 0; y < len1; y++) {
object.put(headings[y], data1[y]);
}
stbuffer.append(object);
stbuffer.append(",");
}
stbuffer.append("]}");
String result = stbuffer.toString();
result = result.replace(",]", "]");