I don't know how to convert a 2-byte decimal to a full-width character and output it.

Asked 2 years ago, Updated 2 years ago, 118 views

I am creating a base64 decoder in C language.In my own way, for example, when decoding the encoded hiragana "a," I converted the two bytes of data into decimal numbers once.Then I can calculate the value of 33440 (0x82a0) and compare it to SJIS's 2-byte character code table, it points to [a] in hiragana, but I don't know how to convert the 2-byte data (0x82a0) into characters and output it.

Environment
win7 64bit
vs 2013

c character-code

2022-09-30 15:51

1 Answers

Base64 is a method of representing binary data in 3-byte increments using a specific 64 to 4 characters in ASCII characters.Even if the original "binary data" represents any string, the character-by-character delimitation is not the delimitation of the process.

Consider, for example, a two-character string, such as "Aaa" as the original string.To represent this in SJIS binary data,

4182A0

3 bytes of the .When converting this to Base 64, you must convert three bytes of 4182A0 together. Even though "A" and "a" are different characters, processing on a per-character basis does not result in Base 64.

4182A0 " "QYKg"

In this way, if some binary data is based on it, then creating a string expression (like "QYKg") is called "Base64 encoding."How this binary data was created is not directly related to Base64.

Conversely, if there is a Base64 string as an input, process it separately every four characters.

Suppose that the input of Base64 is "QYKg".In this case, the total number of characters is exactly four, so you have to put them all together and convert them into binary data.

In , the result is a three-byte binary data called 4182A0.

In , the process of creating a three-byte binary data called 4182A0 has decoded Base64.Whether the three bytes actually represent a SJIS string or not, regardless of whether it is directly related to Base 64, is how to interpret binary data as a string.

(Additional)
If you need until this "how to interpret binary data as a string," you can simply add NUL to the end according to the C language string rule.For example, if binBuf arrays of sufficient size have "binary data" of len bytes (type int),

//...after your Base64 decoding process
binBuf[len] = '\0';
printf("result=%s\n", binBuf);//->A A

If you write the code, the original string will be displayed without having to convert each character.(Of course, the character code of the destination terminal must be the same as the character code of the original string.)

*If you put the NUL character at the end in the original data before encoding Base64, you don't need to terminate NUL after decoding, but I don't do it often because the result is different from the Base64 text I made in other languages.

What do you think? Please let me know if there is anything else.


2022-09-30 15:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.