English 中文(简体)
c) 直径2英寸
原标题:Convert string to array of 2 bytes in c
  • 时间:2024-02-21 23:01:50
  •  标签:
  • c

我有“12345678”。 我需要将其转换成一个没有签名的果园。 我可以采用以下方式制作转换表格:

unsigned char data[4];
data[0] = 0x12;
data[1] = 0x34;
data[2] = 0x56;
data[3] = 0x78;

我曾尝试过类似的东西。

for (int i; i < len; i++) {
    data[i] = (unsigned char)strtol(input, NULL, 16);
}

但是,这给我带来了冷静的代表权,而不是价值。

I ve还利用以下网站:。 https://www.includehelp.com/c/convert-ascii-string-to-byte-array-in-c.aspx

void string2ByteArray(char *input, unsigned char *output) {
    int loop;
    int i;

    loop = 0;
    i = 0;

    while (input[loop] !=   ) {
        output[i++] = input[loop++] - 48;
    }
}

但该编码为<01020304>。 我怎么能够这样做?

问题回答

一种办法是:

  1. extract 2 characters
  2. convert them to an integer
  3. proceed to next 2 characters
void string2ByteArray(char *input, unsigned char *output) {
    int loop;
    int i;

    loop = 0;
    i = 0;

    while (input[loop] !=    && input[loop + 1] !=   ) {
        char buf[3];
        buf[0] = input[loop++];
        buf[1] = input[loop++];
        buf[2] =   ;
        output[i++] = (unsigned char)strtol(buf, NULL, 16);
    }
}

页: 1

unsigned long ul = strtoul( s, NULL, 16 );   // "12345678" ⇒ 0x12345678

由于这一点,可以提取所希望的 by。

unsigned char data[ 4 ];                     // 0x12345678
data[ 0 ] = ( ul >> ( 3 * 8 ) ) & 0xFF;      //            ⇒ 0x12
data[ 1 ] = ( ul >> ( 2 * 8 ) ) & 0xFF;      //            ⇒ 0x34
data[ 2 ] = ( ul >> ( 1 * 8 ) ) & 0xFF;      //            ⇒ 0x56
data[ 3 ] = ( ul >> ( 0 * 8 ) ) & 0xFF;      //            ⇒ 0x78




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签