Can't get the right data I send - android

My code is :
String str= (char)255 + (char)255 +"1" ;
byte[] sendbuf = str.getBytes();
outputPacket = new DatagramPacket(sendbuf,sendbuf.length,remoteIP,2280);
logSocket.send(outputPacket);
the result I get is "0x35 0x31 0x30 0x31"
but what I want is :0xff 0xff 0x31
How to do this?

Your str is adding the two chars first, so it's basically this:
String str = (char)(255 + 255) + "1"; // 5101
What you want is (something like) this:
String str = (char) 255 + "" + (char) 255 + "1";
Or, using String.format:
String str = String.format("%c%c%d", 255, 255, 1);

Related

How to convert emoji unicode in android

In anroid emoji convert to unicode time alwasy get output U+5c but we give emoji string "\uD83D\uDE21" this method it's working
String a = emojiconEditText.getText().toString().trim();
String text = new String(
StringEscapeUtils.escapeJava(a).getBytes(),
StandardCharsets.UTF_8
);
int codepoint = text.codePointAt(0);
String yourUnicode = "U+"+Integer.toHexString(codepoint);
You can encode/decode emojis to the following unicode UTF-8, UTF-16 and U+<hex> using the below:
try {
//I am assuming you are getting unicode from an inputbox
String emoji = emojiconEditText.getText().toString().trim();
//I am also assuming you are getting emoji in hexadecimal form `U+<hexdigits>`
String unicodeHexEmoji = "U+";
StringBuilder sb = new StringBuilder();
//Firstly you want to encode emojis to unicode types by converting to byte array
byte[] utf8Bytes = emoji.getBytes("UTF-8"); // "\\uf0\\u9f\\u98\\u80"
byte[] utf16Bytes = emoji.getBytes("UTF-16"); // "\\ud83d\\ude00"
//convert emoji to hex
for (byte b : utf16Bytes ) {
sb.append(String.format("%02x", b));
}
//we are converting our current emoji to hex just for the purpose of this example
unicodeHexEmoji += sb; //yields "U+feffd83dde21";
byte[] utfHexBytes = getByteFromHex(unicodeHexEmoji.replace("U+","")); // "\\ud83d\\ude00"
//NB: we removed "U+" because its only a prefix denoting that the string is a <hex>
//Decoding our unicodes back to emoji string
String emojiUTF_8 = new String(utf8Bytes,"UTF-8");
String emojiUTF_16 = new String(utf16Bytes,"UTF-16");
String emojiUTF_hex = new String(utfHexBytes,"UTF-16");
Log.d("Tag", "emojiUTF_8 : "+ emojiUTF_8);
Log.d("Tag", "emojiUTF_16 : "+ emojiUTF_16)
Log.d("Tag", "emojiUTF_hex : "+ emojiUTF_hex)
//output
//emojiUTF-8 : 😀
//emojiUTF-16 : 😀
//emojiUTF-hex : 😡
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
...
public byte[] getByteFromHex(String hexString){
//To convert hex string to byte array, you need to first get the length
//of the given string and include it while creating a new byte array.
byte[] val = new byte[hexString.length() / 2];
for (int i = 0; i < val.length; i++) {
int index = i * 2;
int j = Integer.parseInt(hexString.substring(index, index + 2), 16);
val[i] = (byte) j;
}
return val;
}

Android How to print through Bluetooth printer with text alignment and also using table style?

I am trying to print some information through bluetooth printer. I can print plain text. But I can't add any alignment and text format. Also i want to print some data like tableview.
Text format and alignment should be like this:-
Table formatted data should be like this:-
I need some help Please.
Here i include my code which i try:-
String titleStr = "Title" + "\n\n";
StringBuilder contentSb = new StringBuilder();
contentSb.append("Item1 : 1221" + "\n");
contentSb.append("Item2 : test" + "\n");
StringBuilder content2Sb = new StringBuilder();
content2Sb.append("BANK : Us. 1.600" + "\n");
String message = "This is for test." + "\n\n";
long milis = System.currentTimeMillis();
String date = DateUtil.timeMilisToString(milis, "dd-MM-yy / HH:mm") + "\n\n\n\n";
byte[] titleByte = titleStr.getBytes();
byte[] content1Byte = contentSb.toString().getBytes();
byte[] messageByte = message.getBytes();
byte[] content2Byte = content2Sb.toString().getBytes();
byte[] dateByte = date.getBytes();
byte[] totalByte = new byte[titleByte.length + content1Byte.length + messageByte.length +
content2Byte.length + dateByte.length];
int offset = 0;
System.arraycopy(titleByte, 0, totalByte, offset, titleByte.length);
offset += titleByte.length;
System.arraycopy(content1Byte, 0, totalByte, offset, content1Byte.length);
offset += content1Byte.length;
System.arraycopy(messageByte, 0, totalByte, offset, messageByte.length);
offset += messageByte.length;
System.arraycopy(content2Byte, 0, totalByte, offset, content2Byte.length);
offset += content2Byte.length;
System.arraycopy(dateByte, 0, totalByte, offset, dateByte.length);
mmOutputStream.write(totalByte);

How to check word in android

example i have
String = "makan"
how to check last character is "n"
and character number 4 = "a"
and character number 3 = "k"
how to check third charcter of last word,
thanks
You can use the strings method as
char c= "string".charAt(index);
To get the characters in the string.
Edit
You can get the string as array of char as-
String str = "string";
char[] charArray = str.toCharArray();
It depends on the language you are using, but in general read the character by index number (-1 because they start at 0) using []. Or with charAt like Sanjeet said.
Java:
String name = "makan";
bool lastCharacterIsN = name[name.length - 1] == 'n';
bool fourthCharacterIsA = name[3] == 'a';
bool thirdCharacterIsK = name[2] == 'k';
Javascript:
var name = "makan";
var lastCharacterIsN = name[name.length - 1] === 'n';
var fourthCharacterIsA = name[3] === 'a';
var thirdCharacterIsK = name[2] === 'k';
You can do this very simply like this.
String _makan = "makan";
int _length = _makan.length();
System.out.println(" Index of n " +_makan.indexOf("n"));
System.out.println("Length of String : " + _length);
System.out.println("character at last : " + _makan.charAt(_length-1));
System.out.println("character number 3 : " + _makan.charAt(2));
System.out.println("character number 4: " + _makan.charAt(3));

HMAC SHA1 android with input string and input key is Hex String

this is my first post! So if anything is wrong please tell me, i am new to android developing! Here is my question:
I am trying to generate HMAC-SHA1 with input is Hexstring for example the input key is "2cb1b780138bc273459232edda0e4b96" and input value is HexString too but way longer than the key, all the value and the key are Hexstring not character or normal string, so how can i achieve this? I searched a lot but all the result came to use the value and the key in normal string like "The quick brown fox jumps over the lazy dog"
I tried using convertHextoString code to change my HexString into Character string and put in the function like you did but the converted String from convertHextoString appear to have many SPECIAL CHARACTERS such as "\n" or ":" or "%" and so on and i think that is the problem why i got the wrong output. Here is my convertHextoString function
public String convertHexToString(String hex){
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
//49204c6f7665204a617661 split into two characters 49, 20, 4c...
for( int i=0; i<hex.length()-1; i+=2 ){
//grab the hex in pairs
String output = hex.substring(i, (i + 2));
//convert hex to decimal
int decimal = Integer.parseInt(output, 16);
//convert the decimal to character
sb.append((char)decimal);
temp.append(decimal);
}
System.out.println("Decimal : " + temp.toString());
return sb.toString();
}
and the function to HMAC:
static String hash_hmac(String type, String value, String key) {
try { Log.i("Hien - value - hmac",value);
javax.crypto.Mac mac = javax.crypto.Mac.getInstance(type);
javax.crypto.spec.SecretKeySpec secret = new javax.crypto.spec.SecretKeySpec(key.getBytes(), type);
mac.init(secret);
byte[] digest = mac.doFinal(value.getBytes());
StringBuilder sb = new StringBuilder(digest.length*2);
String s;
for (byte b : digest){
s = Integer.toHexString(b & 0xff);
if(s.length() == 1) sb.append('0');
sb.append(s);
}
return sb.toString();} catch (Exception e) { android.util.Log.v("TAG","Exception ["+e.getMessage()+"]", e); } return ""; }
Please help!

How to Convert #RGBto #RRGGBB

I have to convert a color string in the format #RGB to #RRGGBB like #0af To #RRGGBB now can any one help out here using android
One way is:
String rgb = "#0AF";
String rrggbb = "#";
for (int i = 1; i < rgb.length(); i++) {
rrggbb += (rgb.charAt(i) + "" + rgb.charAt(i));
}
int newRgb = 17 * (((oldRgb & 0xF00) << 8) | ((oldRgb & 0xF0) << 4) | (oldRgb & 0xF));
String hexColor = String.format("#%06X", (0xFFFFFF & intColor));
thats what i was searching. It will ensure that color string always will be #RRGGBB.

Categories

Resources