convert Ascii into Hex in Java Script for comparison - javascript

I have an Ascii value (something like "#") that I want to convert into an hex in JavaSkript that I can compare this value with some other hex-values.
Are there any casting possiblities?
Best regards and thanks,
Florian

Convert Ascii strings into base64 (hex)
var b64 = btoa("##%##!##$%");
Convert base64 to Ascii:
atob(b64);
Don't know what you want to compare, beside equality.

// Convert a (UTF-8 or ASCII) string to HEX:
function stringToHex(string) {
return '0x'+[...string].map(char => char.codePointAt(0).toString(16)).join('')
}
// Convert a HEX string into a number:
function hexToNumber(hex) {
return parseInt(hex, 16)
}

Related

How to convert a bit string to base64 in node.js?

I have a bit string that I want to convert to base64 but it doesn't look like there's a native function to do this and I couldn't find a node module either. ):
Input: 100110110101000110100011011001100010110100011011001100100110100011000001100000110000011000001100001001010100111110000011001111100101010011111010011100010110001001001001100000110100111010010100111110000111001000100000110001001000101100111110011001001001101011010001011001001101001010000011000100100100110000011010011
Output: base64 representation of that equivalent binary value
Maybe a better question is how to convert a bit string into a buffer? Not sure
As you guessed, the main thing is converting the string into something easier to convert to base64 and then converting that to base64.
In the code below, we do these conversion sequences:
bit string -> BigInt -> array of byte-sized ints -> binary string -> base64
base64 -> binary string -> array of byte-sized bit strings -> bit string
const encode = bitstr => {
const bytes = [];
// convert bit string to BigInt
let value = BigInt('0b' + bitstr);
// chop it up into bytes
while (value > 0n) {
bytes.unshift(Number(value & 0xffn));
value >>= 8n;
}
// convert to binary string and encode as base64
return btoa(String.fromCharCode.apply(null, bytes));
};
const decode = b64 => {
// decode base64 to binary string
const bstr = atob(b64);
// convert binary string to bit string
return new Array(bstr.length).fill(0).map(
(_,i) => bstr.charCodeAt(i).toString(2).padStart(8, i ? '0' : '')
).join('');
};
const bitstr = '100110110101000110100011011001100010110100011011001100100110100011000001100000110000011000001100001001010100111110000011001111100101010011111010011100010110001001001001100000110100111010010100111110000111001000100000110001001000101100111110011001001001101011010001011001001101001010000011000100100100110000011010011';
const encoded = encode(bitstr);
const decoded = decode(encoded);
console.log(bitstr);
console.log(encoded);
console.log(decoded);

How to convert hex numbers to byte string?

> str = '\xae\xee'
'®î'
How to convert [0xae, 0xee] to '®î'?
You can use String.fromCharCode() to convert the hex values to their string representation with .map() and .join() to form a string:
const hex = [0xae, 0xee];
const res = hex.map(s => String.fromCharCode(s)).join('');
console.log(res);

Javascript: Error while converting Unicode string to hex

I'm trying to convert a unicode string to a hexadecimal representation in javascript controller in SAPUI5 WebIDE.
I am using this function to convert the unicode data to hex. Str variable contains the Unicode data
convertToHex: function(str) {
var hex = '';
var i = 0;
while (str.length > i) {
hex += '' + str.charCodeAt(i).toString(16);
i++;
}
console.log(hex);
return hex;
},
This is first line of the result i am getting in hex variable
504b3414060800021062ee9d685e10090400130825b436f6e74656e745f54797065735d2e786d6c20a24228a002
Now when i am uploading same data to SAP Netweaver gateway, it converts the unicode data to hex as follows (First line) :
504B03041400060008000000210062EE9D685E01000090040000130008025B436F6E74656E745F54797065735D2E786D6C20A2040228A00002
This is the decoded unicode:
PK!bîh^[Content_Types].xml ¢( 
For my application to work i need both hex codes to be same but i am not able to generate the correct hex code in Javascript whereas in SAP i am getting the correct hex values.

javascript Convert string representation of hex value to hex

In Javascript, how do I convert a string representation of a hex value into it's hex representation ?
What I have returning from a checksum routine is a string value "FE". What I need is it's hex representation "\xFE"
I cannot simply do this, as it gives me an error:
var crc = "FE";
var hex = "\x" + crc;
This just gives me a new 4 character ASCII string:
var crc = "FE";
var hex = "0x" + "FE";
thxs for any guidance.
like this
var hex = parseInt("FF", 16);
For the string \xFE, escape the backslash: var hex = '\\x'+'FE'
To convert 'FE' to a Number use +('0xFE')
To show +('0xFE') as a hexadecimal, use (224).toString(16), or '0x'+((254).toString(16))

How to convert large UTF-8 strings into ASCII?

I need to convert large UTF-8 strings into ASCII. It should be reversible, and ideally a quick/lightweight algorithm.
How can I do this? I need the source code (using loops) or the JavaScript code. (should not be dependent on any platform/framework/library)
Edit: I understand that the ASCII representation will not look correct and would be larger (in terms of bytes) than its UTF-8 counterpart, since its an encoded form of the UTF-8 original.
You could use an ASCII-only version of Douglas Crockford's json2.js quote function. Which would look like this:
var escapable = /[\\\"\x00-\x1f\x7f-\uffff]/g,
meta = { // table of character substitutions
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
};
function quote(string) {
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe escape
// sequences.
escapable.lastIndex = 0;
return escapable.test(string) ?
'"' + string.replace(escapable, function (a) {
var c = meta[a];
return typeof c === 'string' ? c :
'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"' :
'"' + string + '"';
}
This will produce a valid ASCII-only, javascript-quoted of the input string
e.g. quote("Doppelgänger!") will be "Doppelg\u00e4nger!"
To revert the encoding you can just eval the result
var encoded = quote("Doppelgänger!");
var back = JSON.parse(encoded); // eval(encoded);
Any UTF-8 string that is reversibly convertible to ASCII is already ASCII.
UTF-8 can represent any unicode character - ASCII cannot.
As others have said, you can't convert UTF-8 text/plain into ASCII text/plain without dropping data.
You could convert UTF-8 text/plain into ASCII someother/format. For instance, HTML lets any character in UTF-8 be representing in an ASCII data file using character references.
If we continue with that example, in JavaScript, charCodeAt could help with converting a string to a representation of it using HTML character references.
Another approach is taken by URLs, and implemented in JS as encodeURIComponent.
Your requirement is pretty strange.
Converting UTF-8 into ASCII would loose all information about Unicode codepoints > 127 (i.e. everything that's not in ASCII).
You could, however try to encode your Unicode data (no matter what source encoding) in an ASCII-compatible encoding, such as UTF-7. This would mean that the data that is produced could legally be interpreted as ASCII, but it is really UTF-7.
If the string is encoded as UTF-8, it's not a string any more. It's binary data, and if you want to represent the binary data as ASCII, you have to format it into a string that can be represented using the limited ASCII character set.
One way is to use base-64 encoding (example in C#):
string original = "asdf";
// encode the string into UTF-8 data:
byte[] encodedUtf8 = Encoding.UTF8.GetBytes(original);
// format the data into base-64:
string base64 = Convert.ToBase64String(encodedUtf8);
If you want the string encoded as ASCII data:
// encode the base-64 string into ASCII data:
byte[] encodedAscii = Encoding.ASCII.GetBytes(base64);
It is impossible to convert an UTF-8 string into ASCII but it is possible to encode Unicode as an ASCII compatible string.
Probably you want to use Punycode - this is already a standard Unicode encoding that encodes all Unicode characters into ASCII. For JavaScript code check this question
Please edit you question title and description in order to prevent others from down-voting it - do not use term conversion, use encoding.
function utf8ToAscii(str) {
/**
* ASCII contains 127 characters.
*
* In JavaScript, strings is encoded by UTF-16, it means that
* js cannot present strings which charCode greater than 2^16. Eg:
* `String.fromCharCode(0) === String.fromCharCode(2**16)`
*
* #see https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary
*/
const reg = /[\x7f-\uffff]/g; // charCode: [127, 65535]
const replacer = (s) => {
const charCode = s.charCodeAt(0);
const unicode = charCode.toString(16).padStart(4, '0');
return `\\u${unicode}`;
};
return str.replace(reg, replacer);
}
Better way
See Uint8Array to string in Javascript also. You can use TextEncoder and Uint8Array:
function utf8ToAscii(str) {
const enc = new TextEncoder('utf-8');
const u8s = enc.encode(str);
return Array.from(u8s).map(v => String.fromCharCode(v)).join('');
}
// For ascii to string
// new TextDecoder().decode(new Uint8Array(str.split('').map(v=>v.charCodeAt(0))))
Do you want to strip all non ascii chars (slash replace them with '?', etc) or to store Unicode code points in a non unicode system?
First can be done in a loop checking for values > 128 and replacing them.
If you don't want to use "any platform/framework/library" then you will need to write your own encoder. Otherwise I'd just use JQuery's .html();
Here is a function to convert UTF8 accents to ASCII Accents (àéèî etc)
If there is an accent in the string it's converted to %239 for exemple
Then on the other side, I parse the string and I know when there is an accent and what is the ASCII char.
I used it in a javascript software to send data to a microcontroller that works in ASCII.
convertUtf8ToAscii = function (str) {
var asciiStr = "";
var refTable = { // Reference table Unicode vs ASCII
199: 128, 252: 129, 233: 130, 226: 131, 228: 132, 224: 133, 231: 135, 234: 136, 235: 137, 232: 138,
239: 139, 238: 140, 236: 141, 196: 142, 201: 144, 244: 147, 246: 148, 242: 149, 251: 150, 249: 151
};
for(var i = 0; i < str.length; i++){
var ascii = refTable[str.charCodeAt(i)];
if (ascii != undefined)
asciiStr += "%" +ascii;
else
asciiStr += str[i];
}
return asciiStr;
}
An implementation of the quote() function might do what you want.
My version can be found here
You can use eval() to reverse the encoding:
var foo = 'Hägar';
var quotedFoo = quote(foo);
var unquotedFoo = eval(quotedFoo);
alert(foo === unquotedFoo);

Categories

Resources