0

I'm tackling the problem for a week. However, I couldn't find a solution. I feel I'm in a maze now.

The problem is that how to pen's coordinates in a vector-based drawing system by decoding hex input.

Thank you so much in advance.

Problem

Input Data: F0A040004000417F417FC04000400090400047684F5057384000804001C0 5F204000400001400140400040007E405B2C4000804000 enter image description here

Pen move condinates enter image description here

This is my code

//Decode Value
penDecode = (hex) => {
    let decodeResult
    let hR = hex.slice(0, 2)
    let lR = hex.slice(2)

    let hByte = parseInt(hR, 16).toString(2).padStart(7, 0)
    let lByte = parseInt(lR, 16).toString(2).padStart(7, 0)
    let binary = hByte + lByte;
    decodeResult = (parseInt(hByte + lByte, 2)) - 8192

    return decodeResult
}



//Slice input
for (let i = 0, len; i < hex.length; i += len) {

    len = hex[i] >= '8' ? 2 : 4;
    let num = hex.slice(i, i + len)
    command.push(num)

    if (num.length === 2) {
        switch (num) {
            case 'C0':
                opcode = num;
                break;

            default:
                opcode = ''
        };
        else if (num.length === 4) {
            //decode HEX to the value 
            num = penDecode(num)
            switch (opcode) {
                case 'C0':
                    if (mvCurrentArr.length <= 1) {
                        mvCurrentArr.push(num)
                    } else {
                        mvArray.push(mvCurrentArr)
                        mv += `(${mvCurrentArr[1]},${mvCurrentArr[0]})`
                        mvCurrentArr = []
                        mvCurrentArr.push(num)
                        output += mv
                    }

            }

Expected output Output become same as the picture above MV (4000, 0) (4000, -8000) (-4000, -8000) (-4000, 0) (-500, 0);

Vitim.us
  • 20,746
  • 15
  • 92
  • 109
ShinCat
  • 139
  • 3
  • 12
  • what does it have to do with javascript though ? – Noob Sep 02 '19 at 03:46
  • @Noob well it's written in javascript... – Lord Elrond Sep 02 '19 at 03:47
  • @CalebGoodman sure, but should it be in this category ? – Noob Sep 02 '19 at 03:49
  • @Noob typically you pick the category that your question is written in. So yes javascript code should be in the javascript category. – Lord Elrond Sep 02 '19 at 03:49
  • @CalebGoodman oh i see, thanks. I didn't know i could post questions like this here. – Noob Sep 02 '19 at 03:52
  • Thank you for your comment. I'm very new to be a developer, so if I use here in the wrong way. please correct me. – ShinCat Sep 02 '19 at 04:18
  • Can you elaborate what problem you're having is unclear what you're asking. I tried to understand your code, but there's not enough information. Your penDecode function does the same as `parseInt(hex, 16)-8192` – Vitim.us Sep 02 '19 at 05:02
  • Your post is a little confusing. From what I understand, you are trying to decode a hex string into a JS object? – Pablo Sep 02 '19 at 06:29
  • Why do you ask this again? See https://stackoverflow.com/questions/57720118/algorith-problem-decode-hex-to-set-output/57733803#57733803 – trincot Sep 02 '19 at 09:22
  • You did not deal with any other opcode than `C0` here. Why? You already got an answer in the other question that dealt with the other opcodes as well, so why did you not use that answer? It provides the expected result when you pass it the input string you specified here... – trincot Sep 02 '19 at 09:35

0 Answers0