node.js buffer toString default utf8 but i want no encoding - node.js

var iconv = require('iconv').Iconv;
var UtfToEuckr = new iconv('utf-8', 'euckr');
var result = UtfToEuckr.convert('한글');
var str = result.toString(); <- default convert utf8
str is UTF-8 string
i wanna no encoding to string
want result : str is euckr string
please, tell me know how..
thank for reading

you always have some encoding, you should either try "ascii" or "binary" (whichever you mean in your situation)

Related

How to get the right values when I convert hex to ascii character

I'm trying to convert a hexa file to text, using javascript node js.
function hex_to_ascii(str1){
var hex = str1.toString();
var str = '';
for (var n = 0; n < hex.length; n += 2) {
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
}
return str;
}
I have a problem concening the extended ASCII charaters, so for example when I try to convert 93 I've get “ instead of ô and when I convert FF I've get ÿ instead of (nbsp) space.
I want to get the same extended charaters as this table: https://www.rapidtables.com/code/text/ascii-table.html
This problem is slightly more complex than it seems at first, since you need to specify an encoding when converting from extended ascii to a string. For example Windows-1252, ISO-8859-1 etc. Since you wish to use the linked table, I'm assuming you wish to use CP437 encoding.
To convert a buffer to string you need a module that will do this for you, converting from a buffer (in a given encoding) to string is not trivial unless the buffer is in a natively supported node.js encoding, e.g. UTF-8, ASCII (7-bit only!), Latin1 etc.
I would suggest using the iconv-lite package, this will convert many types of encoding. Once this is installed the code should look as follows (this takes each character from 0x00 to 0xFF and prints the encoded character):
const iconv = require('iconv-lite');
function hex_to_ascii(hexData, encoding) {
const buffer = Buffer.from(hexData, "hex");
return iconv.decode(buffer, encoding);
}
const testInputs = [...Array(256).keys()];
const encoding = "CP437";
console.log("Decimal\tHex\tCharacter")
for(let input of testInputs) {
console.log([input, input.toString(16), hex_to_ascii(input.toString(16), encoding)].join("\t"));
}

Node - Convert from base64 string to utf-8 byte code array

I'm currently trying to convert images from a base 64 string to a utf-8 byte code array. Below is the code I'm attempting to use with the base 64 string replaced for clarity. I'm getting what looks to be a utf-8 byte array, but have not been able to find a way to verify. Any help with a resource to help verify or if the code is incorrect?
const b64 = '...base64 string';
// convert to utf8 string
const utf8 = (Buffer.from(b64, 'base64')).toString('utf8');
// create a buffer of utf8 string
const buff = Buffer.from(utf8, 'utf8');
//
const arr = [...buff];

SWIFT: performance of uppercaseString

I have a large file (25 MB) of text. I read it into a NSString var. I want to use "uppercaseString" to convert every char to upper case. But the function in so terribly slow, it needs minutes.
Any tip to get it work much faster?
Added code:
if let path = NSBundle.mainBundle().pathForResource("GERMANU", ofType: "txt") {
var error: NSError?
if let data = NSData(contentsOfFile: path, options: NSDataReadingOptions(), error: &error) {
if let datastring = NSString(data: data, encoding: NSMacOSRomanStringEncoding) {
var upper = datastring.uppercaseString
...
That's the code which works, but is slow. Only last row needs all the time.
String::uppercaseString is instantaneous; creating the string is not.
# Long time
12> var st : String = "".join(Array(count:25000000, repeatedValue: "a"))
st: String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..."
# Short time
13> st.uppercaseString
$R8: String = "AAAAAAAAAAAAAAAAAAAAAAAAAAAA..."
Given that you are using the Roman encoding, it is possible that the conversion to uppercase is non-trivial. Perhaps you can try another encoding (if any others are appropriate)? You might try the init?(... usedEncoding ...) variant and invoke fastestEncoding on the result to explore a bit.
Note: you can create a Swift string directly from a file with a particular encoding using:
if let datastring = String(contentsOfFile: path, encoding: ... , error: &error) {
var upper = datastring.uppercaseString
}
To me it looks like a poor library implementation. Using NSString.uppercaseString() is realy fast (half a second). So I will use this, but I'm developing in Swift because I like the language. So I don't want to switch back to old stuff.

How to convert saved text file encoding to UTF8?

recently i saved a text file on my computer but when i open it again i saw some strings like:
"˜ÌÇí ÍÑÝã ÚÌíÈå¿"
now i want to know is it possible to reconvert it to the original text (UTF8)?
i try this codes but it doesn't works
string tempStr="˜ÌÇí ÍÑÝã ÚÌíÈå¿";
Encoding ANSI = Encoding.GetEncoding(1256);
byte[] ansiBytes = ANSI.GetBytes(tempStr);
byte[] utf8Bytes = Encoding.Convert(ANSI, Encoding.UTF8, ansiBytes);
String utf8String = Encoding.UTF8.GetString(utf8Bytes);
You can use something like:
string str = Encoding.GetEncoding(1256).GetString(Encoding.GetEncoding("iso-8859-1").GetBytes(tempStr))
The string wasn't really decoded... Its bytes where simply "enlarged" to char, with something like:
byte[] bytes = ...
char[] chars = new char[bytes.Length];
for (int i = 0; i < bytes.Length; i++)
{
chars[i] = bytes[i];
}
string str = new string(chars);
Now... This transformation is the same that is done by the codepage ISO-8859-1. So I could simply have done the reverse, or I could have used that codepage to do it for me, I selected the second one.
Encoding.GetEncoding("iso-8859-1").GetBytes(tempStr)
this gave me the original byte[]
Then I've done some tests and it seems that the text in the beginning wasn't UTF8, it was in codepage 1256, that is an arabic codepage. So I
string str = Encoding.GetEncoding(1256).GetString(...);
The only thing, the ˜ doesn't seem to be part of the original string.
There is another possibility:
string str = Encoding.GetEncoding(1256).GetString(Encoding.GetEncoding(1252).GetBytes(tempStr));
The codepage 1252 is the codepage used in the USA and in a big part of Europe. If you have a Windows configured to English, there is a good chance it uses the 1252 as the default codepage. The result is slightly different than using the iso-8859-1

How to convert NSString to Character in Swift?

I want to convert NSString to Character in Swift.
I am getting a String from NSTextField, where I input a single character (Example "#"), I need this in Character type.
Use Character class
var chars = [Character](str)
this should be as simple as let characterFromString = Character(textField.text).
NSString is automatically bridged to Swift's String, and the Character class has an init which accepts a single character String; see the documentation here.
This requires that the input string contain a single grapheme cluster (i.e. a single character) and so you might want to validate your input before casting. A slightly more verbose version of the above would be:
let text = textField.text as String
if countElements(text) == 1 {
let character = Character(text)
// do what you want
} else {
// bad input :-(
}
The stringValue of NSTextField actually returns a Swift string and not NSString:
let str = myTextField.stringValue // str is a String
You get the first element with
let ch = str[str.startIndex] // ch is a Character
A NSString would have to be converted to a Swift string first:
let nsString : NSString = "#"
let str = String(nsString)
let ch = str[str.startIndex]

Resources