2

I need to translate ASCII code in HEX to characters. I got from server numbers(as string) in ascii, like: 64656661756c74 (which means 'default'). I know about String.fromCharCode(), but first i need to split the raw message to 2-digits pieces(array). How can i split 2-digits-duration based? Thanks.

6
  • Are you sure that ASCII is correct? I believe it should be 68 69 70 65 85 76 84. Commented Jan 19, 2019 at 22:08
  • 2
    Welcome to StackOverflow! This community is focused on solving specific programming issues, so questions like "How do I ...?" are often too broad. You should try and figure it out your self, and if you run into any specific problems post those here and I'm sure you'll get help. Commented Jan 19, 2019 at 22:09
  • What kind of server returns text as hex strings?! Looks broken to me. Commented Jan 19, 2019 at 22:22
  • 1
    Sorry, I updated my question, yes, its ASCII in hex. The server that returns such an answers is 'Grass valley K2 summit'. Its AMP control protocol. Commented Jan 19, 2019 at 22:37
  • Yes, since, other than UTF-16, JavaScript doesn't have character encoding libraries intrinsically, you could rely on the ASCII encoding of the C0 Controls and Basic Latin block being the same as the UTF-16 encoding and use fromCharCode; Or rely on the ASCII encoding for that block being the same as the ASCII codepoints and the Unicode codespoints and use fromCodePoint. IMO, that's worthy of a code comment. Commented Jan 19, 2019 at 23:22

4 Answers 4

4

Since the string is hex string representation, you have also to convert in decimal number before you pass to String.fromCharCode:

const str = "64656661756c74"
              .match(/.{2}/g)
              .map(ch => String.fromCharCode(parseInt(ch, 16)))
              .join("");
              
console.log(str);
// "default"

That basically store in str the value "default", as you said.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice solution. .
2

Using replace:

let a = '64656661756c74';

let r = a.replace(/([0-9a-f]{2})/g, (m, a) => String.fromCharCode(parseInt(a, 16)))

console.log(r)

Oldschool approach:

let a = '64656661756c74', r = '';

for (let i = 0; i < a.length; i+=2)
  r += String.fromCharCode(parseInt(a.substring(i, i+2), 16))

console.log(r)

Comments

1

To split that into 2-character chunks, use a regular expression like so:

var str = '64656661756c74';
str = str.match(/.{1,2}/g);
console.log(str);

Comments

0

As noted in comments above, your ASCII sequence is probably incorrect. Maybe it is a hexadecimal stream.

Assuming the correct ASCII numbers, you can go as following,

var str = '68697065857684';
str = str.match(/.{1,2}/g);


for (index = str.length - 1; index >= 0; --index) {
  var temp = str[index];  
  str[index]= String.fromCharCode(temp);
}

console.log(str);

3 Comments

Yes, it's hex (decimal doesn't make sense), and it's lowercase!
You are right @Bergi I've seen a text stream in hex for the first time though :)
Sorry, I updated my question, yes, its ASCII in hex. The server that returns such an answers is 'Grass valley K2 summit'. Its AMP control protocol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.